cleaned up the game states to be a 2 bit system with game running and game ending states.

This commit is contained in:
Patrick Tschuchnig 2019-09-11 13:08:12 +02:00
parent 84a6e58ba3
commit ad94b1b601

View File

@ -421,23 +421,25 @@ timer_game_ending_started = False
############################################
while True:
# default actions to be done every cycle
clear_screen()
handle_events()
show_debug()
if game_running == False and game_ending == False:
# one shot for changing the led colour
if game_just_started == True:
print("New game starting")
change_led_colour(10,140,10)
game_just_started = False
# reset all the surfaces to be empty
time_surface = 0
errors_surface = 0
time_rectangle = 0
errors_rectangle = 0
# fill all surfaces again to contain the proper text
header_surface = pygame_font_1.render('Dr' + u'ü' + 'cken Sie Start!', True, pygame_font_main_color)
header_rectangle = header_surface.get_rect()
header_rectangle.topleft = (560, 250)
@ -460,54 +462,67 @@ while True:
pygame.draw.rect(screen,pygame_color_black, (500, 200, (screen_size_x-500*2), (screen_size_y-200*2)), 5)
# add headline, highscore headline, the 3 scores, as well as the logos to the screen
screen.blit(header_surface, header_rectangle)
screen.blit(highscore_header_surface, highscore_header_rectangle)
screen.blit(score1_surface, score1_rectangle)
screen.blit(score2_surface, score2_rectangle)
screen.blit(score3_surface, score3_rectangle)
screen.blit(img_itlablogo_image, (screen_size_x-img_itlablogo_imagex-20, screen_size_y-img_itlablogo_imagey-20))
# TODO: Metallic Logo
# display everything
pygame.display.flip()
if GPIO.input(pin_start) == True and pin_start_inhibit == False and game_running == False and game_ending != True: #wait for the user to press start button
print("start pin has been detected, starting game")
game_running = True
# event handling for if the start button is pushed
if GPIO.input(pin_start) == True and pin_start_inhibit == False: #wait for the user to press start button
print("start pin has been detected, starting game")
# 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
timer_pin_start_inhibit_starttime = pygame.time.get_ticks()
# change game state to running and not ending
game_running = True
game_ending = False
errors = 0
error_added = False
# 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
timer_pin_start_inhibit_starttime = pygame.time.get_ticks()
header_surface = pygame_font_1.render('Spiel l' + u'ä' + 'uft!', True, pygame_font_main_color)
# reset errors
errors = 0
error_added = False # for the 2 tick error counter system, see blow
start_time = int(time.time())
# change the headline
header_surface = pygame_font_1.render('Spiel l' + u'ä' + 'uft!', True, pygame_font_main_color)
# change led colour to blue
change_led_colour(10,10,100)
# start the timer
start_time = int(time.time())
if game_running == True: # game running
# change led colour to blue
change_led_colour(10,10,100)
if game_running == True and game_ending == False:
print("Game running normally")
# if the start pin is inhibited and at least 3 seconds have passed,
# stop blocking the pin
# one shot: stop blocking the pin
if pin_start_inhibit == True:
if ((pygame.time.get_ticks()-timer_pin_start_inhibit_starttime)/1000) > 3:
pin_start_inhibit = False
# add errors if error pin is detected, only once per 2 ticks
# TODO: change system to be time based (only 1 error per 500ms or something)
if GPIO.input(pin_error) == True:
if error_added == False:
errors += 1
int_time = int(time.time()) - start_time + errors * time_per_error
error_added = True
error_added = True # for the 2 tick system
else:
error_added = False
error_added = False # for the 2 tick system
# calculate the current time
int_time = int(time.time()) - start_time
# fill the surface with the current time and errors
time_surface = pygame_font_2.render('Zeit: ' + str(int_time) + 's', True, pygame_font_main_color)
time_rectangle = time_surface.get_rect()
time_rectangle.topleft = (640, 480)
@ -518,24 +533,31 @@ while True:
clear_screen()
# add errors, time and headline to the screen
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header
# display everything
pygame.display.flip()
# if the start input is gone and the start pin isnt inhibited
# if another push of start is detected (i.e. the game is ending!)
if GPIO.input(pin_start) == True and pin_start_inhibit == False:
print("Game ended because start pin was detected again")
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)
# reset the one shot latch for the end of game timer
timer_game_ending_started = False
timer_game_ending_started = False
game_ending = True
game_running = False
pin_start_inhibit = True
# change the state to not running and ending
game_running = False
game_ending = True
# set the start inhibitor to prevent an instant restart
# TODO: maybe unneeded due to the strict game state
pin_start_inhibit = True
if game_running == False and game_ending == True:
print("game is ending")
@ -550,26 +572,45 @@ while True:
timer_game_ending_timout = pygame.time.get_ticks()
timer_game_ending_started = True
clear_screen()
# change the headline to game over
header_surface = pygame_font_1.render('Game over!', True, pygame_font_main_color)
# add errors, time, and new headline to the screen
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header
# display everything
pygame.display.flip()
# unneeded timer, replaced by tick based system
#time.sleep(5)
# nach dem spiel ist vor dem spiel
# if 5 seconds have passed since the game state changed to ending (see: timer_game_ending_timeout)
if ((pygame.time.get_ticks()-timer_game_ending_timout)/1000) > 5:
# reset game_just_started flag to enable the one shot led change in the first state
game_just_started = True
# change state
game_running = False
game_ending = False
# reset highscore one shot
highscore_checked = False
# reset timer started one shot
timer_game_ending_started = False
# unblock the start pin
pin_start_inhibit = False
if game_running == True and game_ending == True:
# something went horribly wrong...
# this state is never supposed to be reached
pass
pygame.display.update()
pygame_clock.tick(pygame_fps)