一键无限循环
Endless loop at the touch of a button
我需要按 Space(例如)开始无限循环,然后再次按 Space 结束循环
@win.event
def on_draw():
(some code of drawing)
@win.event
def on_key_press(symbol, modifiers):
global x,y,z,t
elif symbol == key.SPACE:
t += 0.05
x = ((1 - t) ** 3) * (-180) + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (0) + (t ** 3) * (200)
y = ((1 - t) ** 3) * 70 + 3 * t * ((1 - t) ** 2) * (140) + 3 * (t ** 2) * (1 - t) * (140) + (t ** 3) * (
70)
z = ((1 - t) ** 3) * 0 + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (100) + (t ** 3) * (
0)
(here should be a function of endless moving (drawing) while Space haven't pressed again)
这不是正确的选择。你已经有了一个循环,游戏循环。用它!
创建 2 个状态变量 start_loop
和 run_loop
:
start_loop = False
run_loop = False
如果按下 space,那么您必须决定要做什么。
如果循环不是运行ning(not run_loop
),则通过设置start_loop = True.
开始循环
如果循环是 运行ning,则按 run_loop = False
.
停止循环
def on_key_press(symbol, modifiers):
if symbol == key.SPACE:
if not run_loop:
start_loop = True
else:
run_loop = False
在主循环中,您必须区分 3 种情况。
start_loop
是 True
。进行初始化并将状态设置为 运行 下一帧的循环 (start_loop = False
, run_loop = True
)
run_loop
是 True
。在循环中执行代码
- 默认大小写,它会做一些绘图
def on_draw():
global x,y,z,t
if start_loop:
start_loop = False
run_loop = True
t += 0.05
x = ((1 - t) ** 3) * (-180) + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (0) + (t ** 3) * (200)
y = ((1 - t) ** 3) * 70 + 3 * t * ((1 - t) ** 2) * (140) + 3 * (t ** 2) * (1 - t) * (140) + (t ** 3) * (70)
z = ((1 - t) ** 3) * 0 + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (100) + (t ** 3) * (0)
elif run_loop:
# this is the loop
# [...] (here should be a function of endless moving (drawing) while Space haven't pressed again)
pass
else:
# this is default
# [...] (some code of drawing)
当然你必须通过 [schedule_interval
] 添加一个调度函数,它 运行 在指定的时间间隔内循环。
def run_loop(dt):
on_draw()
pyglet.clock.schedule_interval(run_loop, 0.1) # run every tenth of a second
pyglet.app.run()
我需要按 Space(例如)开始无限循环,然后再次按 Space 结束循环
@win.event
def on_draw():
(some code of drawing)
@win.event
def on_key_press(symbol, modifiers):
global x,y,z,t
elif symbol == key.SPACE:
t += 0.05
x = ((1 - t) ** 3) * (-180) + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (0) + (t ** 3) * (200)
y = ((1 - t) ** 3) * 70 + 3 * t * ((1 - t) ** 2) * (140) + 3 * (t ** 2) * (1 - t) * (140) + (t ** 3) * (
70)
z = ((1 - t) ** 3) * 0 + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (100) + (t ** 3) * (
0)
(here should be a function of endless moving (drawing) while Space haven't pressed again)
这不是正确的选择。你已经有了一个循环,游戏循环。用它!
创建 2 个状态变量 start_loop
和 run_loop
:
start_loop = False
run_loop = False
如果按下 space,那么您必须决定要做什么。
如果循环不是运行ning(not run_loop
),则通过设置start_loop = True.
开始循环
如果循环是 运行ning,则按 run_loop = False
.
def on_key_press(symbol, modifiers):
if symbol == key.SPACE:
if not run_loop:
start_loop = True
else:
run_loop = False
在主循环中,您必须区分 3 种情况。
start_loop
是True
。进行初始化并将状态设置为 运行 下一帧的循环 (start_loop = False
,run_loop = True
)run_loop
是True
。在循环中执行代码- 默认大小写,它会做一些绘图
def on_draw():
global x,y,z,t
if start_loop:
start_loop = False
run_loop = True
t += 0.05
x = ((1 - t) ** 3) * (-180) + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (0) + (t ** 3) * (200)
y = ((1 - t) ** 3) * 70 + 3 * t * ((1 - t) ** 2) * (140) + 3 * (t ** 2) * (1 - t) * (140) + (t ** 3) * (70)
z = ((1 - t) ** 3) * 0 + 3 * t * ((1 - t) ** 2) * (100) + 3 * (t ** 2) * (1 - t) * (100) + (t ** 3) * (0)
elif run_loop:
# this is the loop
# [...] (here should be a function of endless moving (drawing) while Space haven't pressed again)
pass
else:
# this is default
# [...] (some code of drawing)
当然你必须通过 [schedule_interval
] 添加一个调度函数,它 运行 在指定的时间间隔内循环。
def run_loop(dt):
on_draw()
pyglet.clock.schedule_interval(run_loop, 0.1) # run every tenth of a second
pyglet.app.run()