[英]Game loop in turtle python not functioning
Hi i am trying to create a simple environment/ game with turtle. 嗨,我正在尝试用乌龟创建一个简单的环境/游戏。 It is a 3x4 grid with the upper right most square being the end goal. 它是一个3x4网格,最右上角的正方形是最终目标。 as the token enters this goal I would like the token to reset to the start. 当令牌进入该目标时,我希望令牌重置为开始。 My while loop however seems to be freezing the script. 但是我的while循环似乎冻结了脚本。 I believe my logic here is wrong. 我相信我的逻辑是错误的。 the coordinates of the goal is (-25,225). 目标的坐标是(-25,225)。 I would like to check if the token's current position matches this and if so return true - this is the logic i would like to implement. 我想检查令牌的当前位置是否与此匹配,如果是,则返回true-这是我要实现的逻辑。 Thanks for your help! 谢谢你的帮助!
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.")
EDIT : 编辑 :
I have now tried the following code. 我现在尝试了以下代码。 Game doesn't freeze anymore and once i enter the square the token appears inside the square but this is where the second problem occurs. 游戏不再冻结,一旦我进入正方形,令牌就会出现在正方形内,但这是第二个问题发生的地方。 I have now entered the square which should reset me back to my original position (-175, 125). 现在,我进入了方块,该方块将使我重新回到原来的位置(-175,125)。 However I need to press any key a second time in order for that reset to happen and by this time the token would have reset and moved one space based on the key I pressed. 但是,我需要第二次按任意键才能进行该重置,并且这次令牌将根据我所按的键进行重置并移动了一个空格。 any ideas? 有任何想法吗?
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.")
Your while loop prevents anything else to happen indeed. 您的while循环可以防止其他任何事情的发生。 What you want here is to put your isGoal()
checks in the move_<XXX>
event handlers and let turtle's own main loop run. 您要在此处将isGoal()
检查放入move_<XXX>
事件处理程序中,并让turtle自己的主循环运行。
EDIT: for your second question: 编辑:对于您的第二个问题:
However I need to press any key a second time in order for that reset to happen 但是我需要再次按任意键才能进行重置
the reason is quite simple: you should call isGoal()
after you move the player's turtle, not before: 原因很简单:您应该在移动玩家的乌龟之后而不是在以下之前调用isGoal()
:
def move_left():
x = player.xcor()
x -= playerspeed
if x < -175:
x = -175
player.setx(x)
isGoal()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.