我如何在python turtle中加快按键速度

tv6aics1  于 2023-03-07  发布在  Python
关注(0)|答案(1)|浏览(143)

每当我按空格键的时候,我都试图加快我的乌龟的速度。
我有一个代码看起来像这样

import turtle

instructions = [20,30,40,20,50,5,20,30,40,200,5]
speed = 0

wn = turtle.Screen()
drone = turtle.Turtle()

speed = 1
def sped():
    global speed
    speed += 1

for instruction in instructions:
    drone.forward(instruction)
    drone.left(45)
    wn.onkey(sped, "space")
    drone.speed(speed)
    print(speed)

但是,速度总是1。我试着搜索互联网,但什么也没有出现。我该如何解决这个问题?

yzxexxkh

yzxexxkh1#

我只需要在末尾添加wn.listen()

import turtle 
instructions = [20,30,40,20,50,5,20,30,40,200,5]
speed = 0

wn = turtle.Screen()
drone = turtle.Turtle()

speed = 1
def sped():
    global speed
    speed += 1

for instruction in instructions:
    drone.forward(instruction)
    drone.left(45)
    wn.onkeypress(sped, "space")
    drone.speed(speed)
    print(speed)
    wn.listen()

相关问题