简体   繁体   English

Python onkey 不停止循环

[英]Python onkey not stopping loop

def startgame():
    start.state = False
    print start.state


def restart():
    end.state = False
    start.state = True
    print game.state, end.state


s.listen()

s.onkey(startgame, "Return")

s.onkey(restart, 'r')

# This Loop stops when you hit Enter

while start.state:
    start.enter()
s.reset()

# I tried repeating it here but it doesn't work
while end.state:
    end.enter()
s.reset()
game.state = 'playing'

Both loops are nested in a main while loop but the second one is nested in another while loop (If that helps) so it would look something like this两个循环都嵌套在主 while 循环中,但第二个循环嵌套在另一个 while 循环中(如果有帮助),因此它看起来像这样

while True:
    while start.state:
        start.flash()
    s.reset()
    while True:

        # main game code here

        while end.state:
            end.flash()
        s.reset()
        game.state = 'playing'
        break

I simply want it to show the end screen and have 'Press r to play again' flash on screen until the player hits r and then it should restart the game and go back to the start screen.我只是想让它显示结束屏幕并让“按 r 再次播放”在屏幕上闪烁,直到玩家点击 r,然后它应该重新启动游戏并返回开始屏幕。 The end.state variable wont update during the while loop, but start.state does during its while loop. end.state变量不会在 while 循环中更新,但start.state会在它的 while 循环中更新。

Your drawn out (in time) while loops have no place in an event-driven environment like turtle.您的(及时) while循环在事件驱动的环境(如乌龟)中没有立足之地。 In general, we need to control everything with events, specifically timer events .一般来说,我们需要用事件来控制一切,特别是定时器事件 Let's rewrite your code as a state machine.让我们将您的代码重写为状态机。 (Since I don't have your object classes, entities like start.state become globals like start_state in my example code.) (因为我没有你的对象类,所以像start.state这样的start_state在我的示例代码中变成了像start_state这样的全局变量。)

from turtle import Screen, Turtle

start_state = True
end_state = False

def start_game():
    global start_state

    start_state = False

def end_game():
    global end_state

    if not start_state:
        end_state = True

def restart_game():
    global end_state, start_state

    end_state = False
    start_state = True

def flash(text):
    turtle.clear()
    turtle.write(text, align="center", font=('Arial', 24, 'bold'))

    color = turtle.pencolor()
    turtle.pencolor(turtle.fillcolor())
    turtle.fillcolor(color)

def game_states():

    if start_state:
        flash("Start")
    elif end_state:
        flash("End")
    else:
        flash("Playing")

    screen.ontimer(game_states, 500)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.fillcolor(screen.bgcolor())

screen.onkey(start_game, 'Return')
screen.onkey(restart_game, 'r')
screen.onkey(end_game, 'e')
screen.listen()

game_states()

screen.mainloop()

The state machine rules are:状态机规则是:

Start via <Return> -> Playing
Playing via <e> -> End and via <r> -> Start
End via <r> -> Start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM