用空格键启动和停止 Python 海龟 [英] Start and stop Python turtle with space bar

查看:71
本文介绍了用空格键启动和停止 Python 海龟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,通过按空格键来启动和停止海龟.我得到了启动乌龟移动的代码,但是当我再次按下它时它并没有停止它.似乎只是提高了速度.这是我的编码要求和我输入的代码.

I am trying to code a program that starts and stops a turtle by pressing the space bar. I got the code to start the turtle moving but it doesn't stop it when I press it again. It seems to just increase the speed. Here are my coding requirements and the code I typed up.

用三个函数创建一个海龟程序来控制海龟.创建一个名为 turnLeft 的函数,当按下键盘上的右箭头时,该函数将海龟向左旋转 90 度.创建一个名为 turnRight 的函数,当按下向右箭头时,该函数将海龟向右旋转 90 度.创建第三个名为 move() 的函数,该函数在按下空格键时向前移动海龟,然后在第二次按下空格键时停止海龟.

import turtle

turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess moves in space")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()

def leftTurtle():
    tess.left(90)

def rightTurtle():
    tess.right(90)

state_num = 0

def advance_state_machine():
    global state_num
    if state_num == 0:       
        tess.penup()
        state_num = 1
    else:     
        tess.pendown()
        tess.forward(2)
        state_num = 0
    wn.ontimer(advance_state_machine, 25)

def exitWindow():
    wn.bye()

wn.onkey(advance_state_machine, "space")
wn.onkey(exitWindow, "q")
wn.onkey(leftTurtle, "Left")
wn.onkey(rightTurtle, "Right")

wn.listen()                      
wn.mainloop()

推荐答案

除了一些需要更改的小细节之外,您几乎做对了.全局变量state_numadvance_state_machine() 函数中决定海龟是否应该移动.你得到了正确的转弯逻辑,那么为什么不对移动/暂停应用相同的逻辑呢?

You got it almost right except some tiny details to change. The global variable state_num decided in the advance_state_machine() function if the turtle should move or not. You got the proper logic for the turns, so why not apply the same logic for a move/pause ?

在您的原始代码中,您只是将每个显示的帧的全局变量值从一种状态切换到另一种状态,并使用 SPACE 键启动了 advance_state_machine() 的另一个实例,这使海龟更快.海龟变得更快,因为随着每个 SPACE,在 advance_state_machine() 中实现的进一步循环开始与现有循环并行运行.

In your original code you were just switching each shown frame the global variable value from one state to another and with the SPACE key you started another instance of advance_state_machine() what made the turtle faster. The turtle got faster because with each SPACE a further loop implemented in advance_state_machine() was started to run in parallel to the existing one(s).

在下面的代码中,函数 movementControl() 将布尔值 should_move 的值更改为 SPACE 和 advance_state_machine() 上的相反值计算 should_move 以让海龟移动或停止:

In the code below the function movementControl() changes the value of boolean should_move to the opposite one on SPACE and the advance_state_machine() evaluates should_move to let the turtle move or stop:

import turtle

turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess moves in space")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()

def leftTurtle():
    tess.left(90)

def rightTurtle():
    tess.right(90)

should_move = False

def movementControl():
    global should_move
    should_move = not should_move

def advance_state_machine():
    global should_move
    if should_move:       
        tess.pendown()
        tess.forward(2)
    else:     
        tess.penup()
    wn.ontimer(advance_state_machine, 25)

def exitWindow():
    wn.bye()

wn.onkey(movementControl, "space")
wn.onkey(exitWindow, "q")
wn.onkey(leftTurtle, "Left")
wn.onkey(rightTurtle, "Right")

wn.listen()                      
advance_state_machine()

wn.mainloop()

<小时>

哇!!!在 cdlane's 的帮助下,我们将一个非常好的基本海龟示例放在一起.


Wow!!! With cdlane's help we put here a really nice basic turtle example together.

现在我已经将 HIS 代码修改为简约版本,并且也去掉了 moveControl() 函数.

Now I have modified HIS code a bit more towards a minimalistic version and got rid of the movementControl() function, too.

我个人不喜欢使用 from turtle import * 类型的导入语句,因为它们提供了大量不可见"的可用方法和变量,因为您无法直接看到在哪里它们来自,但是……将所有代码放在这么短的块中难道不值得吗?

I personally don't like to use from turtle import * kind of import statements as they provide a huge number of available methods and variables which are "invisible" because you can't directly see where they come from, BUT ... having ALL the code in a so short block isn't it worth it?

from turtle import *
setup(400, 500); title('Turtle moves in space')
bgcolor('lightgreen'); up()
def advance_state_machine():
    if isdown(): fd(2) 
    ontimer(advance_state_machine, 25)
onkey(lambda: (pd, pu)[isdown()](), 'space')
onkey(bye, 'q')
onkey(lambda: lt(90), 'Left')
onkey(lambda: rt(90), 'Right')
listen(); advance_state_machine(); done()

这篇关于用空格键启动和停止 Python 海龟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆