From f4e3c5e10adc9f612d91282911b4c9cbd2921943 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 21 Dec 2020 23:50:04 +0100 Subject: Drop pieces automatically and enable keyboard repeat Signed-off-by: Olivier Gayot --- cameltris.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'cameltris.py') diff --git a/cameltris.py b/cameltris.py index 1246b4e..f17c872 100755 --- a/cameltris.py +++ b/cameltris.py @@ -229,6 +229,11 @@ grid = [[None for _ in range(10)] for _ in range(20)] current_piece, current_piece_position = generate_piece() +drop_rate = 100 + +pygame.key.set_repeat(200, 50) +pygame.time.set_timer(pygame.USEREVENT, drop_rate, True) + while True: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -247,12 +252,18 @@ while True: rotate_piece_clockwise() if event.key == pygame.K_d: rotate_piece_counter_clockwise() - try: if event.key in (pygame.K_DOWN, pygame.K_j): - move_piece_down() + 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) refresh_screen() pygame.display.flip() - time.sleep(.1) -- cgit v1.2.3