简体   繁体   English

转入 Python 海龟模块

[英]Switch in Python Turtle Module

I'm trying to make a simple switch that changes a variable (in this case switchvalue) when I hit a key.我正在尝试做一个简单的开关,当我按下一个键时会改变一个变量(在这种情况下是开关值)。 My approach doesn't seem to be working, the key detection is working as far as I can tell.我的方法似乎不起作用,据我所知,关键检测正在起作用。

import turtle
from turtle import Turtle, Screen

screen = Screen()

jack = Turtle("turtle")
jack.color("red", "green")
jack.pensize(10)
jack.speed(0)
switchvalue = 1


def switch():
    global switchvalue
    if switchvalue == 1:
        switchvalue = 0
    if switchvalue == 0:
        switchvalue = 1




turtle.listen()

turtle.onkey(switch,"s")

screen.mainloop()

if switchvalue == 0:
    jack.forward(100)

You got your logic wrong at the function switch() .您在 function switch()处弄错了逻辑。 See what happens in the beggining when switchvalue is 1看看当switchvalue1时会发生什么

def switch():
    global switchvalue
    if switchvalue == 1: # True
        switchvalue = 0 # change it to 0
    if switchvalue == 0: # Whoops, True again, because you switched it to 0 before
        switchvalue = 1

As you can see you are changing switchvalue to 0 then checking if it is 0 and then it gets changed back to 1 , in other words both if statements are executed.如您所见,您将switchvalue更改为0然后检查它是否为0 ,然后将其更改回1 ,换句话说,两个if语句都已执行。 You should instead use elif or else so that if one succeds the " if loop" (metaphorically speaking) will break aka the other if s will not be checked.您应该改用elifelse ,这样如果一个成功,“ if循环”(比喻地说)将打破另一个if s 不会被检查。

def switch():
    global switchvalue
    # IF one if succeds all the others will not be accounted
    if switchvalue == 1:
        switchvalue = 0
    elif switchvalue == 0:
        switchvalue = 1

Even if you fix the elif issue that @Countour-Integral points out, this code isn't going to work.即使您解决了@Countour-Integral 指出的elif问题,此代码也无法正常工作。 Code after the mainloop() call isn't executed until after turtle shuts down, at which time forward() is meaningless: mainloop()调用之后的代码直到 turtle 关闭后才会执行,此时forward()是没有意义的:

screen.mainloop()

if switchvalue == 0:
    jack.forward(100)

You're mixing the functional API of turtle with its object-oriented API which is why you need your double import of turtle:您正在将turtle 的功能API 与其面向对象的API 混合,这就是您需要双重import turtle 的原因:

import turtle
from turtle import Turtle, Screen

This only leads to trouble.这只会带来麻烦。 Let's rewrite the program to use just the object-oriented API and actually work:让我们重写程序以仅使用面向对象的 API 并实际工作:

from turtle import Turtle, Screen

switchvalue = True

def switch():
    global switchvalue

    switchvalue = not switchvalue

def move():
    delay = 100  # if not moving, slow for human intervention

    if switchvalue:
        jack.forward(1)
        delay = 0  # moving, check back right away

    screen.ontimer(move, delay)  # delay in milliseconds

screen = Screen()

jack = Turtle('turtle')
jack.color('red', 'green')
jack.speed('fastest')
jack.pensize(10)

screen.onkey(switch, 's')
screen.listen()

move()

screen.mainloop()

When the program starts, the turtle will be wandering off to the right.当程序启动时,乌龟会向右游荡。 By pressing 's', you can stop and restart this motion.通过按“s”,您可以停止和重新开始该动作。

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

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