turtle python 中的游戏循环无法运行

Game loop in turtle python not functioning

您好,我正在尝试用乌龟创建一个简单的环境/游戏。它是一个 3x4 网格,最右上角的正方形是最终目标。当令牌进入这个目标时,我希望令牌重置为开始。然而,我的 while 循环似乎冻结了脚本。我相信我的逻辑是错误的。目标的坐标是(-25,225)。我想检查令牌的当前位置是否与此匹配,如果匹配 return true - 这就是我想要实现的逻辑。感谢您的帮助!

import turtle

wn = turtle.Screen()
wn.bgcolor("white")
wn.title("test")

""" Create the Grid """

greg = turtle.Turtle()
greg.speed(0)

def create_square(size,color="black"):
    greg.color(color)
    greg.pd()
    for i in range(4):
        greg.fd(size)
        greg.lt(90)
    greg.pu()
    greg.fd(size)

def row(size,color="black"):
    for i in range(4):
        create_square(size)

def board(size,color="black"):
    greg.pu()
    greg.goto(-(size*4),(size*4))
    for i in range(3):
        row(size)
        greg.bk(size*4)
        greg.rt(90)
        greg.fd(size)
        greg.lt(90)

def color_square(start_pos,distance_sq, sq_width, color):
    greg.pu()
    greg.goto(start_pos)
    greg.fd(distance_sq)
    greg.color(color)
    greg.begin_fill()
    for i in range(4):
        greg.fd(sq_width)
        greg.lt(90)
    greg.end_fill()
    greg.pu()

def initiate_grid(): 
    board(50)
    color_square((-200,200),150, 50,color="green")
    color_square((-200,150),50, 50,color="black")
    color_square((-200,150),150, 50,color="red")
    greg.hideturtle()

initiate_grid()



""" Create the token object """

player = turtle.Turtle()
player.color("blue")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(-175,125)
player.setheading(90)

""" Player Movement """

playerspeed = 50

#Move the player left and right
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -175:
        x = -175
    player.setx(x)

def move_right():
    x = player.xcor()
    x += playerspeed
    if x > -25:
        x = -25
    player.setx(x)

def move_up():
    y = player.ycor()
    y += playerspeed
    if y > 225:
        y = 225
    player.sety(y)

def move_down():
    y = player.ycor()
    y -= playerspeed
    if y < 125:
        y = 125
    player.sety(y)

#Create Keyboard Bindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")



def isGoal(player_pos):
    if player_pos.xcor() == -25 and player_pos.ycor() == 225:
        return True
    else:
        return False

#Main Game loop
while True:

    #check for collision between player and goal
    if isGoal(player):
        #reset player 
        player.setposition(-175,125)



delay = input("Press enter to finish.")

编辑

我现在已经尝试了以下代码。游戏不再冻结,一旦我进入广场,令牌就会出现在广场内,但这是第二个问题发生的地方。我现在已经进入应该将我重置回原始位置 (-175, 125) 的正方形。但是,我需要再次按下任何键才能进行重置,此时令牌将根据我按下的键重置并移动一个 space 。有任何想法吗?

def isGoal():
    if player.xcor() == -25 and player.ycor() == 225:
        player.goto(-175,125)
    else:
        pass

    """ Player Movement """

playerspeed = 50

#Move the player left and right
def move_left():
    isGoal()
    x = player.xcor()
    x -= playerspeed
    if x < -175:
        x = -175
    player.setx(x)

def move_right():
    isGoal()
    x = player.xcor()
    x += playerspeed
    if x > -25:
        x = -25
    player.setx(x)

def move_up():
    isGoal()
    y = player.ycor()
    y += playerspeed
    if y > 225:
        y = 225
    player.sety(y)

def move_down():
    isGoal()
    y = player.ycor()
    y -= playerspeed
    if y < 125:
        y = 125
    player.sety(y)

#Create Keyboard Bindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")

delay = input("Press enter to finish.")

你的 while 循环确实阻止了任何其他事情的发生。你在这里想要的是将你的 isGoal() 检查放在 move_<XXX> 事件处理程序中,并让 turtle 自己的主循环 运行.

编辑:对于你的第二个问题:

However I need to press any key a second time in order for that reset to happen

原因很简单:您应该在isGoal() 移动玩家的海龟之后调用 isGoal() ,而不是在:

之前
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -175:
        x = -175
    player.setx(x)
    isGoal()