heisser_draht/gametest.py

306 lines
9.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#coding=utf-8
2019-08-27 14:58:49 +02:00
import pygame, signal, sys, time, os, RPi.GPIO as GPIO
from pygame.locals import *
#GPIO
startPin = 32
errorPin = 31
enableErrorTimeAddingPin = 15
shutdownPin = 3
GPIO.setmode(GPIO.BOARD)
GPIO.setup(startPin, GPIO.IN)
GPIO.setup(errorPin, GPIO.IN)
GPIO.setup(enableErrorTimeAddingPin, GPIO.IN)
GPIO.setup(shutdownPin, GPIO.IN)
#other constants
2019-08-27 15:39:35 +02:00
timePerError = 5 # every time the wire is touched, some time is added as penalty. only active when enableErrorTimeAddingPin is active
2019-08-29 14:15:30 +02:00
screenSizeX = 1920
screenSizeY = 1080
pygame.init()
#colors
2019-08-29 14:15:30 +02:00
greenColor = pygame.Color(42, 217, 13)
blackColor = pygame.Color(0, 0, 0)
2019-08-29 14:42:57 +02:00
yellowColor = pygame.Color(255, 215, 0)
grayColor = pygame.Color(196, 202, 206)
brownColor = pygame.Color(177, 86, 15)
2019-08-29 14:15:30 +02:00
mainFontColor = greenColor
#fonts
2019-08-29 09:22:27 +02:00
font1 = pygame.font.Font('freesansbold.ttf', 90)
2019-08-29 14:15:30 +02:00
font2 = pygame.font.Font('freesansbold.ttf', 65)
2019-08-29 14:48:09 +02:00
font3 = pygame.font.Font('freesansbold.ttf', 45)
2019-08-29 14:15:30 +02:00
# preset highscores
hs1_name = "Fritz"
hs2_name = "Bernd"
hs3_name = "Max"
hs1_time = 100
hs2_time = 200
hs3_time = 300
# name length for highscores
MAX_NAME_LENGTH = 10
2019-08-27 14:58:49 +02:00
def signal_handler(sig, frame):
print('Programm exiting')
exit_application()
def toggle_fullscreen():
screen = pygame.display.get_surface()
tmp = screen.convert()
caption = pygame.display.get_caption()
cursor = pygame.mouse.get_cursor() # Duoas 16-04-2007
w,h = 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((w,h),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 clearScreen():
screen.fill(blackColor)
return
def handle_events():
print("Status der Pins:")
print("Start Pin:", GPIO.input(startPin))
print("Error Pin:", GPIO.input(errorPin))
print("Stop Pin:", GPIO.input(shutdownPin))
#if GPIO.input(shutdownPin) == True:
#shutdown_raspberry()
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
shutdown_raspberry()
else:
if event.type == pygame.QUIT or event.type == KEYDOWN:
exit_application()
time.sleep(0.1)
return
def exit_application():
pygame.quit()
2019-08-27 14:58:49 +02:00
GPIO.cleanup()
sys.exit()
return
def shutdown_raspberry():
2019-08-28 08:07:47 +02:00
pygame.quit()
GPIO.cleanup()
os.system("sudo shutdown -h now")
return
2019-08-28 14:51:28 +02:00
def enterName():
clearScreen()
print('text entry started')
name = ''
while True:
clearScreen()
2019-08-29 14:15:30 +02:00
highscore_surface = font1.render('High Score!', True, mainFontColor)
highscore_rectangle = highscore_surface.get_rect()
highscore_rectangle.topleft = (700, 210)
screen.blit(highscore_surface, highscore_rectangle)
textbox_surface = font2.render('Enter name: ' + str(name), True, mainFontColor)
2019-08-28 14:51:28 +02:00
textbox_rectangle = textbox_surface.get_rect()
2019-08-29 14:15:30 +02:00
textbox_rectangle.topleft = (700, 350)
2019-08-28 14:51:28 +02:00
screen.blit(textbox_surface, textbox_rectangle)
pygame.display.flip()
for event in pygame.event.get():
if len(name) < MAX_NAME_LENGTH:
if event.type == KEYDOWN:
if event.key == pygame.K_BACKSPACE:
2019-08-29 15:05:55 +02:00
name = name[:-1]
elif event.key == pygame.K_RETURN:
clearScreen()
return name
2019-08-28 14:51:28 +02:00
else:
2019-08-29 15:05:55 +02:00
name += event.unicode
2019-08-28 14:51:28 +02:00
elif event.key == pygame.K_BACKSPACE:
name = name[:-1]
elif event.type == KEYDOWN and event.key == pygame.K_RETURN:
clearScreen()
return name
clearScreen()
name = 'Error'
return name
2019-08-29 14:15:30 +02:00
def checkHighscores(time):
global hs1_time, hs2_time, hs3_time, hs1_name, hs2_name, hs3_name
if time <= hs1_time:
print ('new high score:'+str(time))
#make the second the third
hs3_time = hs2_time
hs3_name = hs2_name
#make the first the second
hs2_time = hs1_time
hs2_name = hs1_name
#new high score
hs1_time = time
hs1_name = enterName()
return
elif time <= hs2_time:
print ('new second:'+str(time))
#make the second the third
hs3_time = hs2_time
hs3_name = hs2_name
#new second time
hs2_time = time
hs2_name = enterName()
return
elif time <= hs3_time:
print ('new third:'+str(time))
hs3_time = time
hs3_name = enterName()
return
else:
return
2019-08-27 14:58:49 +02:00
#signal handing for controlled exit via ctrl+c
signal.signal(signal.SIGINT, signal_handler)
pygame.mouse.set_visible(False) # hide mouse
screen = pygame.display.set_mode((screenSizeX, screenSizeY), pygame.FULLSCREEN)
#pygame.display.set_caption('Heisser Draht') # not required for fullscreen application
#screen = toggle_fullscreen()
2019-08-27 14:58:49 +02:00
while True: # main loop
#global label storage
2019-08-28 08:07:47 +02:00
print("New game starting")
time_surface = 0
errors_surface = 0
time_rectangle = 0
errors_rectangle = 0
2019-08-29 09:22:27 +02:00
header_surface = font1.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor)
header_rectangle = header_surface.get_rect()
2019-08-29 14:53:04 +02:00
header_rectangle.topleft = (580, 280)
2019-08-28 14:51:28 +02:00
2019-08-29 14:38:00 +02:00
highscore_header_surface = font2.render('Highscores:', True, mainFontColor)
2019-08-28 14:51:28 +02:00
highscore_header_rectangle = highscore_header_surface.get_rect()
2019-08-29 14:48:09 +02:00
highscore_header_rectangle = (680, 410)
2019-08-28 14:51:28 +02:00
2019-08-29 14:38:00 +02:00
score1_surface = font3.render(hs1_name + ": " + str(hs1_time) + 's', True, yellowColor)
2019-08-28 14:51:28 +02:00
score1_rectangle = score1_surface.get_rect()
2019-08-29 14:48:09 +02:00
score1_rectangle.topleft = (720, 520)
2019-08-28 14:51:28 +02:00
2019-08-29 14:38:00 +02:00
score2_surface = font3.render(hs2_name + ": " + str(hs2_time) + 's', True, grayColor)
2019-08-28 14:51:28 +02:00
score2_rectangle = score2_surface.get_rect()
2019-08-29 14:48:09 +02:00
score2_rectangle.topleft = (720, 610)
2019-08-28 14:51:28 +02:00
2019-08-29 14:38:00 +02:00
score3_surface = font3.render(hs3_name + ": " + str(hs3_time) + 's', True, brownColor)
2019-08-28 14:51:28 +02:00
score3_rectangle = score3_surface.get_rect()
2019-08-29 14:48:09 +02:00
score3_rectangle.topleft = (720, 700)
screen.blit(header_surface, header_rectangle)
2019-08-28 14:51:28 +02:00
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)
pygame.display.flip()
while GPIO.input(startPin) == False: #wait for the user to press start button
handle_events()
errors = 0
errorAdded = False
#justStarted = True
2019-08-29 09:22:27 +02:00
header_surface = font1.render('Spiel l' + u'ä' + 'uft!', True, mainFontColor)
time.sleep(5)
start_time = int(time.time())
while True: # game running
2019-08-28 08:07:47 +02:00
print("Game running normally")
handle_events()
if GPIO.input(errorPin) == True:
if errorAdded == False:
errors += 1
2019-08-30 09:42:37 +02:00
int_time = int(time.time()) - start_time + errors * timePerError
errorAdded = True
else:
2019-08-30 09:34:41 +02:00
errorAdded = False
int_time = int(time.time()) - start_time
2019-08-29 14:15:30 +02:00
time_surface = font2.render('Zeit: ' + str(int_time) + 's', True, mainFontColor)
time_rectangle = time_surface.get_rect()
2019-08-29 14:15:30 +02:00
time_rectangle.topleft = (640, 480)
2019-08-29 14:15:30 +02:00
errors_surface = font2.render('Fehler: ' + str(errors), True, mainFontColor)
errors_rectangle = errors_surface.get_rect()
2019-08-29 14:15:30 +02:00
errors_rectangle.topleft = (640, 560)
clearScreen()
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header
pygame.display.flip()
#if GPIO.input(startPin) == False:
# justStarted = False
#if GPIO.input(startPin) == True and justStarted == False:
2019-08-27 15:15:40 +02:00
if GPIO.input(startPin) != True:
2019-08-27 15:17:26 +02:00
print("Game ended because start pin wasnt true")
2019-08-29 14:15:30 +02:00
checkHighscores(int_time)
2019-08-27 15:15:40 +02:00
clearScreen()
2019-08-29 09:22:27 +02:00
header_surface = font1.render('Game over!', True, mainFontColor)
2019-08-27 15:15:40 +02:00
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header
2019-08-27 15:15:40 +02:00
pygame.display.flip()
2019-08-27 15:15:40 +02:00
#while GPIO.input(startPin) == False:
# handle_events()
2019-08-27 15:15:40 +02:00
#while GPIO.input(startPin) == True:
# handle_events()
2019-08-27 15:15:40 +02:00
#while GPIO.input(startPin) == False:
# handle_events()
clearScreen()
time.sleep(10)
break