lordmauve / wasabi2d

Cutting-edge 2D game framework for Python
https://wasabi2d.readthedocs.io/
GNU Lesser General Public License v3.0
156 stars 24 forks source link

Coroutines #27

Closed lordmauve closed 4 years ago

lordmauve commented 4 years ago

It should be possible to use coroutines in games. This could be a feature of the clock:

async def move_to(dest):
    delta = player.pos - dest
    async for dt in clock.async_frames(seconds=1):
        player.pos += delta * dt

clock.run(move_to(x, y))

or

async def show_game_over():
     msg = scene.layers[99].add_label("Game over")
     await clock.async_sleep()
     msg.delete()

clock.run(show_game_over())
lordmauve commented 4 years ago

Added something in 5d297a3. Needs a couple more tests, and also documentation, but looks very promising.

I think it would also be useful to be able to await an animation, eg.


await animate(bullet, pos=(400, 400))
await animate(bullet, scale=20)
bullet.delete()
lordmauve commented 4 years ago

You can now await animations.

lordmauve commented 4 years ago

It occurred to me that simply basing this on clock events might be a mistake. There are other things that you might want to await, including input.

With a unified clock/input loop you could write

msg = show_msg("game over, press any key to restart")
await key_down()
msg.delete()
game.reset()