Turtle中的碰撞检测问题[Python]



我使用if语句来检测玩家向上移动后的坐标(y坐标增加)是否等于开放空间的坐标。

,

player_coordinate = player.pos()  # We can say that the coordinate returned is (-10.00,10.00)
space_coordinate = space.pos()  # And the coordinate returned is (-10.00,20.00)
movement = 10  # How far the player can move at once
if (player_coordinate[0], player_coordinate[1] + movement) == space_coordinate:
player can move

现在,我使用了相同的方法,但是当玩家的位置具有负y值时,语句为假。

例如:

# Works:
if (-90.0, 30.0) == (-90.00,30.00)
# Doesn't Work
if (-90.0, -10.0) == (-90.00,-10.00)

(顺便说一下,第一个元组使用前面声明的值,player_coordinate[0], player_coordinate[1] + movement,所以我不知道为什么它返回一个小数点,而不是像原来的。pos()元组中的两个,但这并不重要,因为问题只发生在y值为负的时候)

它看起来像是在说-10.0不等于-10.00。你知道为什么这可能不起作用吗?

如果有帮助的话,这里也有实际的代码。我使用'in'是因为我将所有空间坐标存储在字典中:
def player_movement(player, total, direction):
current = player.pos()
if direction == 'up':
if (current[0], current[1] + total) in space_coordinates.values():
player.shape('Player_Up.gif')  # Changes player sprite
player.goto(current[0], current[1] + total)  # Moves player

我已经检查了我需要的坐标是否在字典中,它是

海龟在浮点平面上漫游,因此要避免进行精确比较。而不是问:

turtle_1.position() == turtle_2.position()

你应该考虑问:

turtle_1.distance(turtle_2) < 10

也就是说,它们彼此是否很接近,而不是在完全相同的坐标上。