diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-12-22 18:14:26 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-12-22 18:17:50 +0100 |
commit | 18b02e7c9f7ada5183ee6175847a4f8967d3ba43 (patch) | |
tree | e87d9e90d602b2d5c204eed19b338330db40a9f1 | |
parent | 2abe0a6658a07260b17e1693ed7902be2f2e87f4 (diff) |
Run the game at 60 FPS
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rwxr-xr-x | cameltris.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/cameltris.py b/cameltris.py index 959ef37..5ee7984 100755 --- a/cameltris.py +++ b/cameltris.py @@ -268,12 +268,19 @@ grid = [[None for _ in range(10)] for _ in range(20)] current_piece, current_piece_position = generate_piece() next_piece, next_piece_position = generate_piece() -drop_rate = 100 +refresh_piece_preview_canvas() + +# Number of frames +frames_per_gridcell = 3 +piece_drop_frames = 0 pygame.key.set_repeat(200, 50) -pygame.time.set_timer(pygame.USEREVENT, drop_rate, True) + +clock = pygame.time.Clock() while True: + piece_drop_frames += 1 + for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() @@ -292,17 +299,20 @@ while True: if event.key == pygame.K_d: rotate_piece_counter_clockwise() if event.key in (pygame.K_DOWN, pygame.K_j): + piece_drop_frames = 0 try: move_piece_down() except WouldCollide: stick_piece() - pygame.time.set_timer(pygame.USEREVENT, drop_rate, True) - elif event.type == pygame.USEREVENT: - try: - move_piece_down() - except WouldCollide: - stick_piece() - pygame.time.set_timer(pygame.USEREVENT, drop_rate, True) + + if piece_drop_frames >= frames_per_gridcell: + piece_drop_frames = 0 + try: + move_piece_down() + except WouldCollide: + stick_piece() refresh_screen() - pygame.display.flip() + pygame.display.update() + + clock.tick(60) |