From 18b02e7c9f7ada5183ee6175847a4f8967d3ba43 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Tue, 22 Dec 2020 18:14:26 +0100 Subject: Run the game at 60 FPS Signed-off-by: Olivier Gayot --- cameltris.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'cameltris.py') 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) -- cgit v1.2.3