海龟python中的游戏循环不起作用
嗨,我正在尝试用海龟创造一个简单的环境/游戏。这是一个3x4的网格,最右上方的正方形是最终目标。当令牌进入此目标时,我希望令牌重置为开始。然而,我的while循环似乎冻结了脚本。我相信我的逻辑是错误的。目标的坐标是-25225。我想检查令牌的当前位置是否与此匹配,如果匹配,则返回true-这是我想实现的逻辑。谢谢你的帮助海龟python中的游戏循环不起作用,python,for-loop,while-loop,logic,turtle-graphics,Python,For Loop,While Loop,Logic,Turtle Graphics,嗨,我正在尝试用海龟创造一个简单的环境/游戏。这是一个3x4的网格,最右上方的正方形是最终目标。当令牌进入此目标时,我希望令牌重置为开始。然而,我的while循环似乎冻结了脚本。我相信我的逻辑是错误的。目标的坐标是-25225。我想检查令牌的当前位置是否与此匹配,如果匹配,则返回true-这是我想实现的逻辑。谢谢你的帮助 import turtle wn = turtle.Screen() wn.bgcolor("white") wn.title("test") """ Create the
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.")
编辑:
我现在已经尝试了以下代码。游戏不再冻结,一旦我进入广场,令牌就会出现在广场内,但这就是第二个问题发生的地方。我现在已经进入了广场,这将使我重新回到我原来的位置——175125。但是,我需要第二次按任意键才能进行重置,此时令牌将重置并根据我按下的键移动一个空格。有什么想法吗
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_u事件处理程序中,并让turtle自己的主循环运行 编辑:对于第二个问题: 但是,我需要第二次按任意键才能进行重置 原因很简单:你应该在移动玩家的乌龟后调用isGoal,而不是之前:
def move_left():
x = player.xcor()
x -= playerspeed
if x < -175:
x = -175
player.setx(x)
isGoal()
谢谢你的回复。小问题我稍微调整了isGoal函数:def isGoal:if player.xcor>-50和player.ycor==225:player.setposition-175125 elif player.xcor=-25和player.ycor>200:player.setposition-175125 else:pass我还在事件处理程序的开头添加了isGoal。仍然给我带来了一些问题。@HimansuOdedra请不要在评论中发布代码,它是不可读的。编辑您的帖子添加更新的代码,而不是保留原始问题和代码,只需添加到问题,或发布新问题。另外,仍然给我带来一些问题是对你的新问题的完全无用的描述,你必须在这里再次解释编辑你的帖子或问一个新问题到底出了什么问题。