[英]Game loop in turtle python not functioning
嗨,我正在嘗試用烏龜創建一個簡單的環境/游戲。 它是一個3x4網格,最右上角的正方形是最終目標。 當令牌進入該目標時,我希望令牌重置為開始。 但是我的while循環似乎凍結了腳本。 我相信我的邏輯是錯誤的。 目標的坐標是(-25,225)。 我想檢查令牌的當前位置是否與此匹配,如果是,則返回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)。 但是,我需要第二次按任意鍵才能進行該重置,並且這次令牌將根據我所按的鍵進行重置並移動了一個空格。 有任何想法嗎?
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自己的主循環運行。
編輯:對於您的第二個問題:
但是我需要再次按任意鍵才能進行重置
原因很簡單:您應該在移動玩家的烏龜之后而不是在以下之前調用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.