首页 > 解决方案 > Python“onkeypress”实现

问题描述

我正在尝试使用“onkeypress”这是一个简单的代码,它创建一个向前和向后的球,但它只会向前。我在 Visual Studio Code 上执行此操作。

import turtle
import time
global  i
i=False
wn=turtle.Screen()
wn.bgcolor("white")
wn.setup(width=600,height=600)
x=turtle.Turtle()
x.left(90)
x.shape("square")
x.color("black")
x.penup()


def start():
   i=True


wn.onkeypress(start,"w")
wn.listen()
wn.update()
if i==True:
  x.forward(100) 
wn.mainloop()

标签: python

解决方案


您仅x.forward(100)在希望它朝多个方向使用时才使用过,每个方向都需要更多代码,即x.left(100)


推荐阅读