I'm working on a Tetris-clone type game and I'm wondering what would be the best way to handle the logic that moves the blocks down every N seconds. I initially did it the easy way:
pygame.time.set_timer(USEREVENT+1, 300)
This will of course add a PyGame event to the queue every 300 milliseconds. In the main loop I can test for this event, and drop the block down when it occurs. The problem with this approach is that the player can easily flood the queue with keyboard generated events. I noticed that player can essentially keep the block at the same height indefinitely by simply rapidly pressing the up-arrow which flips it around. It doesn't seem to matter that the code testing for timer event is evaluated before the test for keyboard events. Somehow keyboard always gets higher priority than the timer. Any idea how to prevent this from happening?
And if this is unavoidable quirk of pygame.time what would be a better way to handle the timed events?
- Check elapsed time on each loop iteration and advance blocks if it is greater than a certain value?
- Use sched or something similar running in a separate thread and signaling back via some shared resource?
Any ideas?