added sound effects. more cleanup. removed toggle fullscreen function (unused). added more documentation
This commit is contained in:
		
							
								
								
									
										180
									
								
								gametest_rev2.py
									
									
									
									
									
								
							
							
						
						
									
										180
									
								
								gametest_rev2.py
									
									
									
									
									
								
							@@ -12,16 +12,22 @@ The idea of the game is to follow a wire with a ring, without touching it.
 | 
				
			|||||||
The ones with the fastest times to reach the end (and with the least errors)
 | 
					The ones with the fastest times to reach the end (and with the least errors)
 | 
				
			||||||
are written onto the highscore table. Said table is temporary, and not saved on exit!
 | 
					are written onto the highscore table. Said table is temporary, and not saved on exit!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The main game loop is running in 4 states:
 | 
					The main game loop is running in 4 states which are changed depending on what inputs the raspberry receives.
 | 
				
			||||||
 | 
					In each game state, a different part of the main game loop is run repeatedly, checking for signals and taking
 | 
				
			||||||
 | 
					corresponding actions.
 | 
				
			||||||
 | 
					If the right signals are detected, the state will change accordingly.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    * The game is not running and not ending (State 0)
 | 
					    * The game is not running and not ending (State 0)
 | 
				
			||||||
        This state is the default "starting" page of the game.
 | 
					        This state is the default "starting" page of the game.
 | 
				
			||||||
        The screen shows the highscore table, and the game waits for a start signal.
 | 
					        The screen shows the highscore table, and the game waits for a start signal.
 | 
				
			||||||
 | 
					        If the raspberry detects a signal on the start pin, the game state changes to State 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    * The game is running and not ending (State 1)
 | 
					    * The game is running and not ending (State 1)
 | 
				
			||||||
        This is the game running. A timer is displayed on screen.
 | 
					        This is the game running. A timer is displayed on screen.
 | 
				
			||||||
        Errors are shown on screen aswell.
 | 
					        Errors are shown on screen aswell.
 | 
				
			||||||
        The game is waiting for error signals and stop signals
 | 
					        The game is waiting for error signals and stop signals.
 | 
				
			||||||
 | 
					        If the raspberry detects a signal on the error pin, errors are added to the time.
 | 
				
			||||||
 | 
					        If the raspberry detects a signal on the stop pin, the game state changes to State 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    * The game is not running and ending (State 2)
 | 
					    * The game is not running and ending (State 2)
 | 
				
			||||||
        Once the stop signal has been reached in the running game, this state is reached.
 | 
					        Once the stop signal has been reached in the running game, this state is reached.
 | 
				
			||||||
@@ -29,10 +35,12 @@ The main game loop is running in 4 states:
 | 
				
			|||||||
        This state automatically ends after a few seconds and returns the game to State 0.
 | 
					        This state automatically ends after a few seconds and returns the game to State 0.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    * The game is neither running nor ending (State 3)
 | 
					    * The game is neither running nor ending (State 3)
 | 
				
			||||||
        This state is currently invalid and does nothing. it should never be reached.
 | 
					        This state is currently invalid and does nothing. It should never be reached.
 | 
				
			||||||
        there's no functionality assigned to this, and this state is never run, but included
 | 
					        There's no functionality assigned to this, and this state is never run, but included
 | 
				
			||||||
        for completeness sake.
 | 
					        for completeness sake.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If during any of these states the shutdown pin is detected, the raspberry will shutdown entirely.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The game also features a (not entirely interactive) LED lighting control. The LEDs are supposed
 | 
					The game also features a (not entirely interactive) LED lighting control. The LEDs are supposed
 | 
				
			||||||
to be driven by a small MOSFET driver circuit. DO NOT ATTACH LEDS DIRECTLY!
 | 
					to be driven by a small MOSFET driver circuit. DO NOT ATTACH LEDS DIRECTLY!
 | 
				
			||||||
The correct type of LEDs are the ones that have a common 12V rail per segment, and are switched
 | 
					The correct type of LEDs are the ones that have a common 12V rail per segment, and are switched
 | 
				
			||||||
@@ -92,35 +100,133 @@ GPIO Pinout for assembly:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Default state of pins:
 | 
					Default state of pins:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
32: pull up
 | 
					P_32: pull up
 | 
				
			||||||
33: pull up
 | 
					P_33: pull up
 | 
				
			||||||
36: pull up
 | 
					P_36: pull up
 | 
				
			||||||
37: pull up
 | 
					P_37: pull up
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Make sure leds are on pull-up default pins
 | 
					Make sure leds are on pull-up default pins
 | 
				
			||||||
so when their state is undefined, the led
 | 
					so when their state is undefined, the led
 | 
				
			||||||
strips are switched off!
 | 
					strips are switched off!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
22: pull up
 | 
					P_22: pull up
 | 
				
			||||||
18: pull up
 | 
					P_18: pull up
 | 
				
			||||||
16: pull up
 | 
					P_16: pull up
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Circuit diagrams for the contact and button electronics:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					All buttons are close-contact. 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					+3.3V DC ---+-----------+-----------+-----------+-------->
 | 
				
			||||||
 | 
					            |           |           |           |
 | 
				
			||||||
 | 
					            |           |           |           |
 | 
				
			||||||
 | 
					      start \      stop \  shutdown \     error \
 | 
				
			||||||
 | 
					     contact \   contact \    button \   contact \
 | 
				
			||||||
 | 
					              \           \           \           \
 | 
				
			||||||
 | 
					            *           *           *           *
 | 
				
			||||||
 | 
					            |           |           |           |
 | 
				
			||||||
 | 
					           R1          R3          R5          R7
 | 
				
			||||||
 | 
					            |           |           |           |
 | 
				
			||||||
 | 
					            +-----+     +-----+     +-----+     +-----+
 | 
				
			||||||
 | 
					            |     |     |     |     |     |     |     |
 | 
				
			||||||
 | 
					           R2   P_32   R4   P_37   R6   P_36   R8   P_33
 | 
				
			||||||
 | 
					            |           |           |           |
 | 
				
			||||||
 | 
					GND --------+-----------+-----------+-----------+-------->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					LED Strip controller and driver:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					A power MOSFET per channel is driven by the GPIO output of the raspberry to 
 | 
				
			||||||
 | 
					control the flow from the LED Strip contact on the connector to the ground rail.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					P_22 ---+   LED_STRIP_RED-----+
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					       R9            Q1  | |--+ D
 | 
				
			||||||
 | 
					        |                |
 | 
				
			||||||
 | 
					        +----------------| |<-+
 | 
				
			||||||
 | 
					        |              G |    |
 | 
				
			||||||
 | 
					       R10               | |--+ S
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					GND ----+---------------------+----->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					P_18 ---+   LED_STRIP_GRN-----+
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					       R11           Q2  | |--+ D
 | 
				
			||||||
 | 
					        |                |
 | 
				
			||||||
 | 
					        +----------------| |<-+
 | 
				
			||||||
 | 
					        |              G |    |
 | 
				
			||||||
 | 
					       R12               | |--+ S
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					GND ----+---------------------+----->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					P_16 ---+   LED_STRIP_BLU-----+
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					       R13           Q3  | |--+ D
 | 
				
			||||||
 | 
					        |                |
 | 
				
			||||||
 | 
					        +----------------| |<-+
 | 
				
			||||||
 | 
					        |              G |    |
 | 
				
			||||||
 | 
					       R14               | |--+ S
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					        |                     |
 | 
				
			||||||
 | 
					GND ----+---------------------+----->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					LED Strip:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The LED Strip has a 4 contact connector, one being the 12V supply rail
 | 
				
			||||||
 | 
					and the other 3 the ground connections for the corresponding LED colour.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					+12V DC ----+---------------------------->
 | 
				
			||||||
 | 
					            |
 | 
				
			||||||
 | 
					            |   a series of LEDs and
 | 
				
			||||||
 | 
					            |   resistors, as determined
 | 
				
			||||||
 | 
					            |   by the LED strip.
 | 
				
			||||||
 | 
					            |
 | 
				
			||||||
 | 
					            +--( ->|- -[|||]- ->|- )--+
 | 
				
			||||||
 | 
					                                      |
 | 
				
			||||||
 | 
					                                      |
 | 
				
			||||||
 | 
					                           LED_STRIP_RED/GRN/BLU
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Part list:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Resistors:
 | 
				
			||||||
 | 
					R1............1,800 Ohm
 | 
				
			||||||
 | 
					R2...........10,000 Ohm
 | 
				
			||||||
 | 
					R3............1,800 Ohm
 | 
				
			||||||
 | 
					R4...........10,000 Ohm
 | 
				
			||||||
 | 
					R5............1,800 Ohm
 | 
				
			||||||
 | 
					R6...........10,000 Ohm
 | 
				
			||||||
 | 
					R7............1,800 Ohm
 | 
				
			||||||
 | 
					R8...........10,000 Ohm
 | 
				
			||||||
 | 
					R9............1,000 Ohm
 | 
				
			||||||
 | 
					R10..........10,000 Ohm
 | 
				
			||||||
 | 
					R11...........1,000 Ohm
 | 
				
			||||||
 | 
					R12..........10,000 Ohm
 | 
				
			||||||
 | 
					R13...........1,000 Ohm
 | 
				
			||||||
 | 
					R14..........10,000 Ohm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Transistors:
 | 
				
			||||||
 | 
					Q1...........IRLZ34N Enhancement Mode n-channel MOSFET
 | 
				
			||||||
 | 
					Q2...........IRLZ34N Enhancement Mode n-channel MOSFET
 | 
				
			||||||
 | 
					Q3...........IRLZ34N Enhancement Mode n-channel MOSFET
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Credits:
 | 
					Credits:
 | 
				
			||||||
Based on a script made by TODO: source, although it has been heavily altered
 | 
					Based on a script made by TODO: source, although it has been heavily altered
 | 
				
			||||||
Changes made by: Clima Philip, Krajnc Moris, Glantschnig Raphael
 | 
					Changes made by: Clima Philip, Krajnc Moris, Cooke Thomas, Glantschnig Raphael
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Hosted on:
 | 
					Hosted on:
 | 
				
			||||||
https://git.wolfsberg.local/philip.clima/heisser_draht
 | 
					https://git.wolfsberg.local/philipp.clima/heisser_draht
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
############################################
 | 
					############################################
 | 
				
			||||||
############# START OF IMPORTS #############
 | 
					############# START OF IMPORTS #############
 | 
				
			||||||
############################################
 | 
					############################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import signal
 | 
					import signal
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
import pygame
 | 
					import pygame
 | 
				
			||||||
from PIL import Image
 | 
					from PIL import Image
 | 
				
			||||||
from pygame.locals import *
 | 
					from pygame.locals import *
 | 
				
			||||||
@@ -154,10 +260,6 @@ pin_blue = 16
 | 
				
			|||||||
# number is in ms
 | 
					# number is in ms
 | 
				
			||||||
time_per_error = 5000
 | 
					time_per_error = 5000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# screen settings
 | 
					 | 
				
			||||||
screen_size_x = 1920
 | 
					 | 
				
			||||||
screen_size_y = 1080
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# name length for highscores
 | 
					# name length for highscores
 | 
				
			||||||
max_name_length = 10
 | 
					max_name_length = 10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -183,6 +285,14 @@ hs3_time = 300000
 | 
				
			|||||||
# initialise the game
 | 
					# initialise the game
 | 
				
			||||||
pygame.init()
 | 
					pygame.init()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# screen settings - autodetermined based on the initialised pygame video instance
 | 
				
			||||||
 | 
					screen_size_x = pygame.display.Info().current_w
 | 
				
			||||||
 | 
					screen_size_y = pygame.display.Info().current_h
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# load sound effects
 | 
				
			||||||
 | 
					error_sound = pygame.mixer.Sound('snd/buzz.wav')
 | 
				
			||||||
 | 
					logoff_sound = pygame.mixer.Sound('snd/winxplogoff.wav')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# fonts
 | 
					# fonts
 | 
				
			||||||
pygame_font_1 = pygame.font.Font('freesansbold.ttf', 90)
 | 
					pygame_font_1 = pygame.font.Font('freesansbold.ttf', 90)
 | 
				
			||||||
pygame_font_2 = pygame.font.Font('freesansbold.ttf', 65)
 | 
					pygame_font_2 = pygame.font.Font('freesansbold.ttf', 65)
 | 
				
			||||||
@@ -333,29 +443,6 @@ def change_led_colour(red_amount, green_amount, blue_amount):
 | 
				
			|||||||
    change_green(green_amount)
 | 
					    change_green(green_amount)
 | 
				
			||||||
    return True
 | 
					    return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def toggle_fullscreen():
 | 
					 | 
				
			||||||
    """switches the game to fullscreen"""
 | 
					 | 
				
			||||||
    screen = pygame.display.get_surface()
 | 
					 | 
				
			||||||
    tmp = screen.convert()
 | 
					 | 
				
			||||||
    caption = pygame.display.get_caption()
 | 
					 | 
				
			||||||
    cursor = pygame.mouse.get_cursor() # Duoas 16-04-2007
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    screen_width, screen_height = screen.get_width(), screen.get_height()
 | 
					 | 
				
			||||||
    flags = screen.get_flags()
 | 
					 | 
				
			||||||
    bits = screen.get_bitsize()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pygame.display.quit()
 | 
					 | 
				
			||||||
    pygame.display.init()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    screen = pygame.display.set_mode((screen_width, screen_height), flags^FULLSCREEN, bits)
 | 
					 | 
				
			||||||
    screen.blit(tmp, (0, 0))
 | 
					 | 
				
			||||||
    pygame.display.set_caption(*caption)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pygame.key.set_mods(0) # HACK: work-a-round for a SDL bug??
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pygame.mouse.set_cursor(*cursor) # Duoas 16-04-2007
 | 
					 | 
				
			||||||
    return screen
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def clear_screen():
 | 
					def clear_screen():
 | 
				
			||||||
    """fills the screen with a colour"""
 | 
					    """fills the screen with a colour"""
 | 
				
			||||||
    screen.fill(pygame_color_white)
 | 
					    screen.fill(pygame_color_white)
 | 
				
			||||||
@@ -372,12 +459,16 @@ def handle_events():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def exit_application():
 | 
					def exit_application():
 | 
				
			||||||
    """ exit the application with proper cleanup"""
 | 
					    """ exit the application with proper cleanup"""
 | 
				
			||||||
 | 
					    logoff_sound.play()
 | 
				
			||||||
 | 
					    time.sleep(3)
 | 
				
			||||||
    pygame.quit()
 | 
					    pygame.quit()
 | 
				
			||||||
    GPIO.cleanup()
 | 
					    GPIO.cleanup()
 | 
				
			||||||
    sys.exit()
 | 
					    sys.exit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def shutdown_raspberry():
 | 
					def shutdown_raspberry():
 | 
				
			||||||
    """shutdown the system with proper cleanup"""
 | 
					    """shutdown the system with proper cleanup"""
 | 
				
			||||||
 | 
					    logoff_sound.play()
 | 
				
			||||||
 | 
					    time.sleep(3)
 | 
				
			||||||
    pygame.quit()
 | 
					    pygame.quit()
 | 
				
			||||||
    GPIO.cleanup()
 | 
					    GPIO.cleanup()
 | 
				
			||||||
    os.system("sudo shutdown -h now")
 | 
					    os.system("sudo shutdown -h now")
 | 
				
			||||||
@@ -510,10 +601,14 @@ def show_debug():
 | 
				
			|||||||
    print('#############')
 | 
					    print('#############')
 | 
				
			||||||
    print('Game running: ' + str(game_running))
 | 
					    print('Game running: ' + str(game_running))
 | 
				
			||||||
    print('Game ending: ' + str(game_ending))
 | 
					    print('Game ending: ' + str(game_ending))
 | 
				
			||||||
 | 
					    print('-------------')
 | 
				
			||||||
    print('Pin status: ')
 | 
					    print('Pin status: ')
 | 
				
			||||||
    print('Start pin: ' + str(GPIO.input(pin_start)))
 | 
					    print('Start pin: ' + str(GPIO.input(pin_start)))
 | 
				
			||||||
 | 
					    print('Stop pin: ' + str(GPIO.input(pin_stop)))
 | 
				
			||||||
    print('Error pin: ' + str(GPIO.input(pin_error)))
 | 
					    print('Error pin: ' + str(GPIO.input(pin_error)))
 | 
				
			||||||
    print('Shutdown pin: ' + str(GPIO.input(pin_shutdown)))
 | 
					    print('Shutdown pin: ' + str(GPIO.input(pin_shutdown)))
 | 
				
			||||||
 | 
					    print('-------------')
 | 
				
			||||||
 | 
					    print('Tick: ' + str(pygame.time.get_ticks()))
 | 
				
			||||||
    print('#############')
 | 
					    print('#############')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
############################################
 | 
					############################################
 | 
				
			||||||
@@ -569,13 +664,11 @@ pin_start_inhibit = False
 | 
				
			|||||||
############################################
 | 
					############################################
 | 
				
			||||||
######### START OF MAIN GAME LOOP ##########
 | 
					######### START OF MAIN GAME LOOP ##########
 | 
				
			||||||
############################################
 | 
					############################################
 | 
				
			||||||
 | 
					 | 
				
			||||||
while True:
 | 
					while True:
 | 
				
			||||||
    # default actions to be done every cycle
 | 
					    # default actions to be done every cycle
 | 
				
			||||||
    clear_screen()
 | 
					    clear_screen()
 | 
				
			||||||
    handle_events()
 | 
					    handle_events()
 | 
				
			||||||
    show_debug()
 | 
					    show_debug()
 | 
				
			||||||
    print(str(pygame.time.get_ticks()))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # State 0
 | 
					    # State 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -665,6 +758,7 @@ while True:
 | 
				
			|||||||
                errors += 1
 | 
					                errors += 1
 | 
				
			||||||
                error_added = True
 | 
					                error_added = True
 | 
				
			||||||
                error_cooldown_start = pygame.time.get_ticks()
 | 
					                error_cooldown_start = pygame.time.get_ticks()
 | 
				
			||||||
 | 
					                error_sound.play()
 | 
				
			||||||
                change_led_colour(200, 0, 0)
 | 
					                change_led_colour(200, 0, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # if an error happened, error_added is set to true - which prohibits adding another error.
 | 
					        # if an error happened, error_added is set to true - which prohibits adding another error.
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								snd/buzz.wav
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								snd/buzz.wav
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								snd/winxplogoff.wav
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								snd/winxplogoff.wav
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user