From 72a08a23b6b5e0a7a5b3d6f76c19856e51168eea Mon Sep 17 00:00:00 2001 From: Patrick Tschuchnig Date: Wed, 11 Sep 2019 11:14:59 +0200 Subject: [PATCH] changed timing system for the game\'s end to be tick based --- gametest_rev1.py | 55 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/gametest_rev1.py b/gametest_rev1.py index d72a0c2..ae87008 100644 --- a/gametest_rev1.py +++ b/gametest_rev1.py @@ -361,6 +361,14 @@ def check_highscores(time): else: return +def show_debug(): + print('Game running: ' + str(game_running)) + print('Game ending: ' + str(game_ending)) + print('Pin status: ') + print('Start pin: ' + str(GPIO.input(pin_start))) + print('Error pin: ' + str(GPIO.input(pin_error))) + print('Shutdown pin: ' + str(GPIO.input(pin_shutdown))) + ############################################ ####### END OF FUNCTION DEFINITIONS ######## ############################################ @@ -393,7 +401,9 @@ led_init() game_running = False game_just_started = True +game_ending == False +highscore_checked = False pin_start_inhibit = False ############################################ @@ -408,6 +418,7 @@ while True: clear_screen() handle_events() + show_debug() if game_running == False: @@ -459,7 +470,7 @@ while True: # set an inhibitor bit to stop the contact from possibly vibrating and # causing a stop of the game immediately after start pin_start_inhibit = True - pin_start_inhibit_timer_start = pygame.time.get_ticks() + timer_pin_start_inhibit_starttime = pygame.time.get_ticks() errors = 0 error_added = False @@ -477,7 +488,7 @@ while True: # if the start pin is inhibited and at least 3 seconds have passed, # stop blocking the pin if pin_start_inhibit == True: - if ((pygame.time.get_ticks()-pin_start_inhibit_timer_start)/1000) > 3: + if ((pygame.time.get_ticks()-timer_pin_start_inhibit_starttime)/1000) > 3: pin_start_inhibit = False @@ -508,30 +519,38 @@ while True: pygame.display.flip() # if the start input is gone and the start pin isnt inhibited - if GPIO.input(pin_start) == True and pin_start_inhibit == False: + if game_running == True and GPIO.input(pin_start) == True and pin_start_inhibit == False and game_ending != True: + print("Game ended because start pin was detected again") - # change led colour to red - change_led_colour(100,10,10) + # change led colour to red + change_led_colour(100,10,10) - print("Game ended because start pin wasnt true") + game_ending == True + timer_game_ending_timout = pygame.time.get_ticks() - check_highscores(int_time) + if game_ending == True: + print("game is ending") + if highscore_checked == False: + check_highscores(int_time) + highscore_checked = True - clear_screen() - header_surface = pygame_font_1.render('Game over!', True, pygame_font_main_color) + clear_screen() + header_surface = pygame_font_1.render('Game over!', True, pygame_font_main_color) - screen.blit(errors_surface, errors_rectangle) #errors - screen.blit(time_surface, time_rectangle) #time - screen.blit(header_surface, header_rectangle) #header + screen.blit(errors_surface, errors_rectangle) #errors + screen.blit(time_surface, time_rectangle) #time + screen.blit(header_surface, header_rectangle) #header - pygame.display.flip() + pygame.display.flip() - time.sleep(5) + #time.sleep(5) - # nach dem spiel ist vor dem spiel - game_running = False - game_just_started = True - clear_screen() + # nach dem spiel ist vor dem spiel + if ((pygame.time.get_ticks()-timer_game_ending_timout)/1000) > 5: + game_running = False + game_just_started = True + game_ending = False + highscore_checked = False pygame.display.update() pygame_clock.tick(pygame_fps)