Example for interactively controlling an animation/games
See original GitHub issueI have managed to create smooth physics animations within jupyter using ipcanvas. I have also managed to successfully use ipyevents on ipycanvas to trigger events.
However I am struggling to combine events within animation loop. This would be required to run a game on ipycanvas, for example when pressing keys to change the direction of a spaceship flying across the canvas.
When the animation loop is running, it appears to block the events from being processed.
I can run my animation like this:
def run_game():
for i in range(5000):
with hold_canvas(space):
space.clear()
space.fill_style = 'black'
space.fill_rect(0,0, width, height)
ship.update()
space.fill_style = 'white'
space.fill_arc(ship.position.x, ship.position.y, ship.size, 0, math.pi * 2)
And I can specify an event changing the ship’s velocity like this:
from ipyevents import Event
d = Event(source=space, watched_events=['keydown'])
d.on_dom_event(ship.thrusting)
Each one works on their own, but the event does not fire while the run_game() is running because it is blocking.
Is there a way to run this asynchronously?
Could you perhaps provide an example, which shows how one would write a game for ipycanvas?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Martin, you are amazing. Thanks a lot for this example! And for you work on ipycanvas. I want to use it for teaching kids. and the interactivity and graphics within a notebook make a real difference there.
I agree. It should be added to the documentation then 😃