【发布时间】:2021-11-23 00:58:40
【问题描述】:
我儿子向我展示了他用 python 编写的一些代码(他正在通过示例学习,并且正在开发一个基本的块弹跳应用程序)。
他让我在他的游戏中添加“重力”,所以我添加了一个 2 秒计时器,在按下“空格”后将 y 坐标重置为 0。
执行时我收到“主线程不在主循环中”错误消息,并且我阅读了一些线程,这些线程解释了我正在使用的计时器可能不在正确的线程上。我不确定如何实现排队,想知道是否有更简单的解决方案。
谢谢!
import turtle
from threading import Timer
wn = turtle.Screen()
wn.title("my game")
wn.bgcolor("blue")
wn.setup(width=1000, height=800)
wn.tracer(0)
#person
person = turtle.Turtle()
person.speed(0)
person.shape("square")
person.color("white")
person.shapesize(stretch_wid=1, stretch_len=1)
person.penup()
person.goto(-350, 0)
def personXX():
x = person.xcor()
x += 20
person.setx(x)
def personXY():
x = person.xcor()
x -= 20
person.setx(x)
def personYY():
y = person.ycor()
y += 20
person.sety(y)
r = Timer(2.0, fallToFloor )
r.start()
def fallToFloor():
y = person.ycor()
y -= 20
person.sety(y)
wn.listen()
wn.onkeypress(personXY, "a")
wn.onkeypress(personXX, "d")
wn.onkeypress(personYY, "space")
while 1 == 1 :
wn.update()
【问题讨论】:
标签: python python-turtle