Python龟蛇游戏中的碰撞

2024-04-26 21:06:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个代码,里面有一个迷宫,海龟必须穿过。唯一的问题是我不知道如何创建与所有墙的碰撞。我一直无法找到一种方法,在不限制墙的每一个坐标的情况下,创造障碍。我只能访问海龟和随机模块。有办法做到这一点吗?这是我的密码:

import turtle

screen = turtle.Screen()
tr=turtle.Turtle()
screen.tracer(0)

mazeWidth=150
tr = turtle.Turtle()
tr.width(5)
tr.speed(0)
tr.penup()
tr.goto(-mazeWidth,190)
def drawMazeSection(color):
  tr.pencolor("lightblue")
  tr.penup()
  tr.goto(-20,200)
  tr.pendown()
  tr.goto(20,200)
  tr.penup()
  tr.goto(-20,-200)
  tr.pendown()
  tr.goto(20,-200)
  tr.penup()
  tr.pencolor("black")
  tr.goto(-200,200)
  tr.pendown()
  tr.goto(-200,-160)
  tr.begin_fill()
  tr.goto(-180,-160)
  tr.goto(-180,-200)
  tr.goto(-200,-200)
  tr.end_fill()
  tr.goto(-20,-200)
  tr.penup()
  tr.goto(20,-200)
  tr.pendown()
  tr.begin_fill()
  tr.goto(200,-200)
  tr.goto(200,200)
  tr.goto(20,200)
  tr.goto(20,140)
  tr.goto(80,140)
  tr.goto(80,180)
  tr.goto(180,180)
  tr.goto(180,-80)
  tr.goto(100,-80)
  tr.goto(100,-140)
  tr.goto(20,-140)
  tr.goto(20,-200)
  tr.end_fill()
  tr.penup()
  tr.goto(20,160)
  tr.pendown()
  tr.begin_fill()
  tr.goto(-80,160)
  tr.goto(-80,140)
  tr.goto(-60,140)
  tr.goto(-60,160)
  tr.end_fill()
  tr.goto(-60,120)
  tr.goto(-20,120)
  tr.goto(-20,100)
  tr.goto(40,100)
  tr.penup()
  tr.goto(120,140)
  tr.pendown()
  tr.begin_fill()
  tr.goto(140,140)
  tr.goto(140,100)
  tr.goto(120,100)
  tr.goto(120,120)
  tr.goto(120,140)
  tr.end_fill()
  tr.goto(140,100)
  tr.goto(80,100)
  tr.goto(80,80)
  tr.goto(80,60)
  tr.begin_fill()
  tr.goto(140,60)
  tr.goto(140,20)
  tr.goto(100,20)
  tr.goto(100,60)
  tr.end_fill()
  tr.goto(20,60)
  tr.goto(20,-100)
  tr.goto(20,-40)
  tr.goto(-20,-40)
  tr.goto(-20,-100)
  tr.goto(-60,-100)
  tr.penup()
  tr.goto(-60,-140)
  tr.pendown()
  tr.goto(-60,-160)
  tr.goto(-140,-160)
  tr.goto(-140,-120)
  tr.goto(-160,-120)
  tr.goto(-160,-40)
  tr.penup()
  tr.goto(-60,-60)
  tr.pendown()
  tr.goto(-60,0)
  tr.goto(-20,0)
  tr.goto(-20,40)
  tr.goto(-60,40)
  tr.goto(-60,80)
  tr.goto(-120,80)
  tr.goto(-120,200)
  tr.penup()
  tr.goto(-160,160)
  tr.pendown()
  tr.goto(-160,40)
  tr.goto(-40,40)
  tr.penup()
  tr.goto(60,20)
  tr.pendown()
  tr.goto(60,-80)
  tr.begin_fill()
  tr.goto(60,-40)
  tr.goto(140,-40)
  tr.goto(140,-20)
  tr.goto(60,-20)
  tr.end_fill()
  tr.penup()
  tr.goto(-200,0)
  tr.pendown()
  tr.goto(-100,0)
  tr.begin_fill()
  tr.goto(-100,-120)
  tr.goto(-100,-80)
  tr.goto(-120,-80)
  tr.goto(-120,0)
  tr.end_fill()
  tr.penup()
  tr.goto(-20,-140)
  tr.pendown()
  tr.goto(-20,-200)
  
for color in ["#FF0000","#0000FF","#00FF00"]:
  drawMazeSection(color)
  

screen.tracer(1)    
from turtle import Turtle, Screen

wn = Screen()
wn.bgcolor('darkgreen')

tr.color('red')
tr.penup()

tr.speed(40)
speed = 1.5

wn.mainloop()
tr.penup()
tr.goto(20,-180)
tr.pendown()
tr.shape('turtle')
tr.color("#DB148E")
tr.width(5)
tr.left(90)

tr.penup()
tr.goto(0,-200)
def travel():
    tr.forward(speed)
    wn.ontimer(travel, 10)

wn.onkey(lambda: tr.setheading(90), 'w')
wn.onkey(lambda: tr.setheading(180), 'a')
wn.onkey(lambda: tr.setheading(0), 'd')
wn.onkey(lambda: tr.setheading(270), 's')

wn.listen()
travel()

1条回答
网友
1楼 · 发布于 2024-04-26 21:06:39

I think you need the turtle.window_width() and turtle.window_height functions

获取程序的宽度和高度

width, height = turtle.window_width(), turtle.window_height()

## Gets the boundaries for the x coordinates 
minX, maxX = -width/2, width/2

## Sets the boundaries for the y coordinates
minY, maxY = -height/2, height / 2

## Checks if the turtle hits the boundaries
def check():
    if tr.xcor() <= maxX:
       tr.setheading(0)
    if tr.xcor() >= minX:
       tr.setheading(180)
    if tr.ycor() >= minY:
       tr.setheading(270)
    if tr.ycor() <= maxY:
       tr.setheading(90)

相关问题 更多 >