python - 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()
解决方案
您仅x.forward(100)
在希望它朝多个方向使用时才使用过,每个方向都需要更多代码,即x.left(100)
推荐阅读
- java - 使用 JFileChooser 从不同目录中选择多个文件
- reactjs - 为什么不推荐使用 componentWillReceiveProps?
- java - 是否可以将地图接口与 io.vavr.collection.HashMap 一起使用?
- html - 我的网站在本地主机上看起来很完美,但在网上上传后看起来完全不同?
- c# - 一次刷新 accessToken 的最佳方法
- javascript - 在外部 JavaScript 错误中添加“异步”
- php - 长 SQL 结果后执行突然停止
- javascript - react-redux:为什么我得到 _this.props.incrementCounter is not a function?
- c - 如何限制子进程中的内存使用并在Linux上内存不足时检查它
- javascript - 如何使用 Jquery 切换元素