我的python乌龟处在正确的坐标上,我的if语句返回为true,但我的if语句未返回为true

时间:2020-07-15 15:50:00

标签: python if-statement turtle-graphics

我所做的是,当乌龟位于坐标(0,-285)上时,我按空格键,它将打印'yay'和坐标,否则应打印'no'和坐标。但是,每当我按空格键时,即使我在正确的坐标上,它也会打印“否”。我可以帮忙吗?

import turtle
import time
import random
t = turtle.Pen()
t.speed(0)
check = (0,0)

x = turtle.window_width()
y = turtle.window_height()
#defining functions to move
def turn_left():
    t.left(90)
def turn_right():
    t.right(90)
def forward():
    t.forward(15)
def backward():
    t.backward(15)
def check_pos():
    check = t.pos()
    print(check)
    if check == (0,-285):
        print('yay')
    else:
        print('no')
turtle.onkeypress(turn_left, "Left")
turtle.onkeypress(turn_right, "Right")
turtle.onkeypress(forward, "Up")
turtle.onkeypress(backward, "Down")
turtle.onkeypress(check_pos, "space")
turtle.listen()

Python Shell

1 个答案:

答案 0 :(得分:0)

像这样设置边界:

def check_pos():
    check = t.pos()
    print(check)
    if t.distance(0,-285) < 10:
        print('yay')
    else:
        print('no')