为什么我的乌龟和另一个物体的坐标不一样,即使它们在同一个地方?

Why are the coordinates of my turtle and another object not the same even though they are in the same spot?

当蛇的 head/turtle 与苹果处于同一坐标时,我的 while 循环没有返回 false 时遇到问题。我试图通过打印出这些值来解决问题,这些值与海龟位置相同,在末尾有一个额外的零:苹果的输出为 (160.0, 160.0) 而海龟的输出为 (160.00 , 160.00).当蛇 head/turtle 和苹果在相同位置 snake.pos() != fruit 的输出仍然是真实的; snake.pos() 应该等于 fruit 并结束 while 循环。

#imports
import turtle
import random
import keyboard
import time
#apple
apple = turtle
a = 455.00
b = 455.00 - 65.00
c = 455.00 - 65.00 * 2.00
d = 455.00 - 65.00 * 3.00
e = 455.00 - 65.00 * 4.00
f = 455.00 - 65.00 * 5.00
g = 455.00 - 65.00 * 6.00
h = 455.00 - 65.00 * 7.00
i = 455.00 - 65.00* 8.00
j = 455.00 - 65.00 * 9.00
k = 455.00 - 65.00 * 10.00
l = 455.00 - 65.00 * 11.00
m = 455.00 - 65.00 * 12.00
n = 455.00 - 65.00 * 13.00
o = 455.00 - 65.00 * 14.00
poll1 = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o]
poll2 = [a, b, c, d, e, f, g, h, i, j, k, l, m, n]
x = random.choice(poll1)
y = random.choice(poll2)
print(x)
print(y)
apple.shape("square")
apple.turtlesize(stretch_wid=3, stretch_len=3, outline=3)
apple.tracer(n=1, delay=0)
apple.penup()
apple.colormode(255)
apple.pencolor(255, 0, 0)
apple.fillcolor(255, 0, 0)
apple.goto(x, y)
apple.forward(30)
apple.pendown()
fruit = x,y
apple.begin_fill()
apple.right(90)
apple.forward(30)
for p in range(0, 3):
    apple.right(90)
    apple.forward(60)
apple.right(90)
apple.forward(30)
apple.end_fill()
apple.pencolor(0, 0, 0)
apple.fillcolor(0, 0, 0)
print(apple)
#snake
snake = turtle
snake.penup()
snake.setup(width=1000, height=1000, startx=0, starty=0)
snake.shape("square")
snake.turtlesize(stretch_wid=3, stretch_len=3, outline=3)
snake.tracer(n=1, delay=0)
snake.setpos(260, 65)
snake.seth(180)
print(fruit)
for q in range(0, 3):
    snake.forward(65)
    snake.stamp()
def go():
    while snake.pos() != fruit:
        time.sleep(0.5)
        if keyboard.is_pressed('w' or 'W'):
            snake.seth(90)
        if keyboard.is_pressed('a' or 'A'):
            snake.seth(180)
        if keyboard.is_pressed('s' or 'S'):
            snake.seth(270)
        if keyboard.is_pressed('d' or 'D'):
            snake.seth(0)
        snake.forward(65)
        snake.stamp()
        snake.clearstamps(1)
        print(snake.pos() == fruit)
go()
snake.exitonclick()

独立于海龟,将两个浮点数与==进行比较是一种失败的策略。尽管非常相似,但它们不一定==

这对于在浮点平面上游荡的海龟来说是正确的——它们可能永远不会回到它们之前占据的完全相同的位置。解决方案是使用 distance() 方法并将结果与​​您在比较中愿意接受的 slop 的数量进行比较。

以下是对代码的修改和简化:

from turtle import Screen, Turtle
from random import randrange

def go():
    snake.forward(60)

    if snake.distance(apple) < 45:
        apple.hideturtle()
        x, y = randrange(-330, 330, 60), randrange(-300, 300, 60)
        apple.goto(x, y)
        apple.showturtle()

    screen.ontimer(go, 500)

screen = Screen()
screen.setup(width=720, height=720, startx=0, starty=0)

apple = Turtle()
apple.hideturtle()
apple.shape('square')
apple.turtlesize(3)
apple.color('red')
apple.penup()

x, y = randrange(-330, 330, 60), randrange(-330, 330, 60)
apple.goto(x, y)
apple.showturtle()

snake = Turtle()
snake.shape('square')
snake.turtlesize(3)
snake.penup()

screen.onkey(lambda: snake.seth(90), 'w')
screen.onkey(lambda: snake.seth(90), 'W')
screen.onkey(lambda: snake.seth(180), 'a')
screen.onkey(lambda: snake.seth(180), 'A')
screen.onkey(lambda: snake.seth(270), 's')
screen.onkey(lambda: snake.seth(270), 'S')
screen.onkey(lambda: snake.seth(0), 'd')
screen.onkey(lambda: snake.seth(0), 'D')

screen.listen()

go()

screen.exitonclick()

我需要看看蛇是否在苹果附近。我试图寻找蛇何时正好在苹果上。