""" 左右键移动火箭飞行交互动画。 本程序会生成一枚火箭,用左右键可以改变它的方向。 按空格键来切换它是否要移动。 """ import turtle screen = turtle.getscreen() screen.setup(400,500) screen.title("左右键移动火箭飞行交互动画") screen.bgcolor("black") rocket = turtle.Turtle() rocket.color('cyan') should_move = False def move_control(): global should_move should_move = not should_move def move_rocket(): global should_move if should_move: rocket.pendown() rocket.forward(2) else: rocket.penup() screen.ontimer(move_rocket, 25) def close_window(): screen.bye() screen.onkey(move_control, "space") screen.onkey(close_window, "q") screen.onkey(lambda:rocket.left(90), "Left") screen.onkey(lambda:rocket.right(90), "Right") screen.listen() move_rocket() screen.mainloop()