I need to start endless loop by pressing Space (for example) and end it pressing Space again
@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)
This is not the right choice to do it. You already have a loop, the game loop. Use it!
Create 2 state variables start_loop
and run_loop
:
start_loop = False
run_loop = False
If space is pressed then you've to decide what to do.
If the loop is not running (not run_loop
), then start the loop by setting start_loop = True.
If the loop is running, then stop the loop by run_loop = False
.
def on_key_press(symbol, modifiers):
if symbol == key.SPACE:
if not run_loop:
start_loop = True
else:
run_loop = False
In the main loop you can have to distinguish 3 cased.
start_loop
is True
. Do the initializations and set the states to run the loop in the next frame (start_loop = False
, run_loop = True
)run_loop
is True
. Do the code in the loopdef 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)
Of course you've to add a schedule function by [schedule_interval
], which runs the loop in an specified interval.
def run_loop(dt):
on_draw()
pyglet.clock.schedule_interval(run_loop, 0.1) # run every tenth of a second
pyglet.app.run()