Python海龟碰撞检测通过;不在「;列表为什么';这不管用吗?
此测试代码的目标是使玩家能够使用W、a、S、D移动,并使用ENTER键开始或停止构建墙。如果有人能告诉我为什么他只是偶尔撞到墙壁,我将不胜感激。在一般意义上也可以随意评论我的代码!提前谢谢Python海龟碰撞检测通过;不在「;列表为什么';这不管用吗?,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,此测试代码的目标是使玩家能够使用W、a、S、D移动,并使用ENTER键开始或停止构建墙。如果有人能告诉我为什么他只是偶尔撞到墙壁,我将不胜感激。在一般意义上也可以随意评论我的代码!提前谢谢 import turtle grid_size = 10 t1 = turtle.Pen() t1.width(grid_size) t1.up() walls = [[0,0]] walls.clear() def toggle_building(): if t1.isdown():
import turtle
grid_size = 10
t1 = turtle.Pen()
t1.width(grid_size)
t1.up()
walls = [[0,0]]
walls.clear()
def toggle_building():
if t1.isdown():
t1.up()
else:
t1.down()
def lay_brick():
if t1.isdown() and t1.pos() not in walls:
walls.append(t1.pos())
print("Brick layed.")
def print_pos():
print(t1.pos())
def move_up():
t1.setheading(90)
if t1.pos() + [0, grid_size] not in walls:
t1.forward(grid_size)
lay_brick()
else:
print("wall")
print_pos()
def move_left():
t1.setheading(180)
if t1.pos() - [grid_size, 0] not in walls:
t1.forward(grid_size)
lay_brick()
else:
print("wall")
print_pos()
def move_down():
t1.setheading(270)
if t1.pos() - [0, grid_size] not in walls:
t1.forward(grid_size)
lay_brick()
else:
print("wall")
print_pos()
def move_right():
t1.setheading(0)
if t1.pos() + [grid_size, 0] not in walls:
t1.forward(grid_size)
lay_brick()
else:
print("wall")
print_pos()
turtle.onkeypress(move_up, "w")
turtle.onkeypress(move_left, "a")
turtle.onkeypress(move_down, "s")
turtle.onkeypress(move_right, "d")
turtle.onkeypress(toggle_building, "Return")
turtle.listen()
正如在你的问题的评论中所提到的,海龟在浮点平面上游荡,即使你认为它已经回到了与以前完全相同的位置,但始终会有一点浮点噪声添加到该位置。将位置转换为
int
进行比较肯定有帮助,但可能还不够
下面是我的尝试,通过改变坐标系本身,使其变得更加健壮,这样看起来我们移动的是1,而不是10。它还试图减少浮点到整数转换所需的位数:
from turtle import Screen, Turtle
GRID_SIZE = 10
def toggle_building():
if turtle.isdown():
turtle.penup()
else:
turtle.pendown()
def lay_brick(position):
if turtle.isdown():
walls.add(position)
def move_up():
turtle.setheading(90)
position = int(turtle.xcor()), int(turtle.ycor()) + 1
if position not in walls:
turtle.goto(position)
lay_brick(position)
def move_left():
turtle.setheading(180)
position = int(turtle.xcor()) - 1, int(turtle.ycor())
if position not in walls:
turtle.goto(position)
lay_brick(position)
def move_down():
turtle.setheading(270)
position = int(turtle.xcor()), int(turtle.ycor()) - 1
if position not in walls:
turtle.goto(position)
lay_brick(position)
def move_right():
turtle.setheading(0)
position = int(turtle.xcor()) + 1, int(turtle.ycor())
if position not in walls:
turtle.goto(position)
lay_brick(position)
screen = Screen()
WIDTH, HEIGHT = (screen.window_width() / 2) // GRID_SIZE, (screen.window_height() / 2) // GRID_SIZE
screen.setworldcoordinates(-WIDTH, -HEIGHT, WIDTH, HEIGHT)
turtle = Turtle()
turtle.width(GRID_SIZE)
turtle.fillcolor('red')
turtle.penup()
walls = set()
screen.onkeypress(move_up, "w")
screen.onkeypress(move_left, "a")
screen.onkeypress(move_down, "s")
screen.onkeypress(move_right, "d")
screen.onkeypress(toggle_building, "Return")
screen.listen()
screen.mainloop()
它还使用
集
来包含墙。由于顺序无关紧要,这将使测试更快。是不是pos()
是一个浮点数?如果我没有弄错的话,pos()返回一个带有两个浮点数的Vec2D,如下所示:[20.00,10.00]@RobertHarveyFloating point number无法使用equals进行可靠比较。诚然,如果您直接存储pos()
,这并不重要,但您正在那里做一些算术,所以。谢谢@RobertHarvey!我添加了一些代码将浮点转换为整数,然后将它们附加到列表中,再添加一些代码以确保整数始终是10的倍数。