highscore list changes

This commit is contained in:
Moris Krajnc 2019-08-29 14:15:30 +02:00
parent 78cf06d039
commit eb6e421473
2 changed files with 349 additions and 31 deletions

View File

@ -18,22 +18,36 @@ GPIO.setup(shutdownPin, GPIO.IN)
#other constants #other constants
timePerError = 5 # every time the wire is touched, some time is added as penalty. only active when enableErrorTimeAddingPin is active timePerError = 5 # every time the wire is touched, some time is added as penalty. only active when enableErrorTimeAddingPin is active
screenSizeX = 900 screenSizeX = 1920
screenSizeY = 900 screenSizeY = 1080
pygame.init() pygame.init()
#colors #colors
redColor = pygame.Color(255, 0, 0) greenColor = pygame.Color(42, 217, 13)
blackColor = pygame.Color(0, 0, 0) blackColor = pygame.Color(0, 0, 0)
whiteColor = pygame.Color(255, 255, 255) yellowColor = pygame.Color(230, 198, 4)
grayColor = pygame.Color(128, 128, 128) grayColor = pygame.Color(128, 128, 128)
mainFontColor = redColor brownColor = pygame.Color(228, 141, 26)
mainFontColor = greenColor
#fonts #fonts
font1 = pygame.font.Font('freesansbold.ttf', 90) font1 = pygame.font.Font('freesansbold.ttf', 90)
font2 = pygame.font.Font('freesansbold.ttf', 65)
# 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
def signal_handler(sig, frame): def signal_handler(sig, frame):
print('Programm exiting') print('Programm exiting')
@ -101,9 +115,17 @@ def enterName():
name = '' name = ''
while True: while True:
clearScreen() clearScreen()
textbox_surface = font1.render('Enter name: ' + str(name), True, mainFontColor)
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)
textbox_rectangle = textbox_surface.get_rect() textbox_rectangle = textbox_surface.get_rect()
textbox_rectangle.topleft = (10, 30) textbox_rectangle.topleft = (700, 350)
screen.blit(textbox_surface, textbox_rectangle) screen.blit(textbox_surface, textbox_rectangle)
pygame.display.flip() pygame.display.flip()
for event in pygame.event.get(): for event in pygame.event.get():
@ -123,6 +145,42 @@ def enterName():
name = 'Error' name = 'Error'
return name return name
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
#signal handing for controlled exit via ctrl+c #signal handing for controlled exit via ctrl+c
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
@ -133,16 +191,6 @@ screen = pygame.display.set_mode((screenSizeX, screenSizeY), pygame.FULLSCREEN)
#screen = toggle_fullscreen() #screen = toggle_fullscreen()
# preset highscores
hs1_name = "Fritz"
hs2_name = "Bernd"
hs3_name = "Max"
hs1_time = 100
hs2_time = 200
hs3_time = 300
while True: # main loop while True: # main loop
#global label storage #global label storage
@ -154,23 +202,23 @@ while True: # main loop
header_surface = font1.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor) header_surface = font1.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor)
header_rectangle = header_surface.get_rect() header_rectangle = header_surface.get_rect()
header_rectangle.topleft = (10, 100) header_rectangle.topleft = (600, 280)
highscore_header_surface = font1.render('Highscores:', True, mainFontColor) highscore_header_surface = font1.render('Highscores:', True, mainFontColor)
highscore_header_rectangle = highscore_header_surface.get_rect() highscore_header_rectangle = highscore_header_surface.get_rect()
highscore_header_rectangle = (10,190) highscore_header_rectangle = (670, 410)
score1_surface = font1.render(hs1_name + ": " + str(hs1_time) + 's', True, mainFontColor) score1_surface = font2.render(hs1_name + ": " + str(hs1_time) + 's', True, yellowColor)
score1_rectangle = score1_surface.get_rect() score1_rectangle = score1_surface.get_rect()
score1_rectangle.topleft = (10, 270) score1_rectangle.topleft = (670, 500)
score2_surface = font1.render(hs2_name + ": " + str(hs2_time) + 's', True, mainFontColor) score2_surface = font2.render(hs2_name + ": " + str(hs2_time) + 's', True, grayColor)
score2_rectangle = score2_surface.get_rect() score2_rectangle = score2_surface.get_rect()
score2_rectangle.topleft = (10, 360) score2_rectangle.topleft = (670, 590)
score3_surface = font1.render(hs3_name + ": " + str(hs3_time) + 's', True, mainFontColor) score3_surface = font2.render(hs3_name + ": " + str(hs3_time) + 's', True, brownColor)
score3_rectangle = score3_surface.get_rect() score3_rectangle = score3_surface.get_rect()
score3_rectangle.topleft = (10, 450) score3_rectangle.topleft = (670, 680)
screen.blit(header_surface, header_rectangle) screen.blit(header_surface, header_rectangle)
screen.blit(highscore_header_surface, highscore_header_rectangle) screen.blit(highscore_header_surface, highscore_header_rectangle)
@ -207,13 +255,13 @@ while True: # main loop
int_time = int(time.time()) - start_time int_time = int(time.time()) - start_time
time_surface = font1.render('Zeit: ' + str(int_time) + 's', True, mainFontColor) time_surface = font2.render('Zeit: ' + str(int_time) + 's', True, mainFontColor)
time_rectangle = time_surface.get_rect() time_rectangle = time_surface.get_rect()
time_rectangle.topleft = (10, 300) time_rectangle.topleft = (640, 480)
errors_surface = font1.render('Fehler: ' + str(errors), True, mainFontColor) errors_surface = font2.render('Fehler: ' + str(errors), True, mainFontColor)
errors_rectangle = errors_surface.get_rect() errors_rectangle = errors_surface.get_rect()
errors_rectangle.topleft = (10, 400) errors_rectangle.topleft = (640, 560)
clearScreen() clearScreen()
screen.blit(errors_surface, errors_rectangle) #errors screen.blit(errors_surface, errors_rectangle) #errors
@ -229,6 +277,8 @@ while True: # main loop
print("Game ended because start pin wasnt true") print("Game ended because start pin wasnt true")
checkHighscores(int_time)
clearScreen() clearScreen()
header_surface = font1.render('Game over!', True, mainFontColor) header_surface = font1.render('Game over!', True, mainFontColor)

268
gametest.py.save Normal file
View File

@ -0,0 +1,268 @@
#!/usr/bin/env python
#coding=utf-8
import pygame, 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
timePerError = 5
screenSizeX = 900
screenSizeY = 900
pygame.init()
#colors
redColor = pygame.Color(255, 0, 0)
blackColor = pygame.Color(0, 0, 0)
whiteColor = pygame.Color(255, 255, 255)
grayColor = pygame.Color(128, 128, 128)
mainFontColor = redColor
#fonts
font = pygame.font.Font('freesansbold.ttf', 90)
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()
return
def exit_application():
pygame.quit()
sys.exit()
return
def shutdown_raspberry():
os.system("sudo shutdown -h now")
return
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()
while True: # main loop
#global label storage
time_surface = 0
errors_surface = 0
time_rectangle = 0
errors_rectangle = 0
header_surface = font.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor)
header_rectangle = header_surface.get_rect()
header_rectangle.topleft = (0, 100)
screen.blit(header_surface, header_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
header_surface = font.render('Spiel startet', True, mainFontColor)
time.sleep(5)
start_time = int(time.time())
while True: # game running
handle_events()
if GPIO.input(errorPin) == True:
if errorAdded == False:
errors += 1
errorAdded = True
else:
errorAdded = False
if GPIO.input(enableErrorTimeAddingPin) == True:
int_time = int(time.time()) - start_time + errors * timePerError
int_time = int(time.time()) - start_time
time_surface = font.render('Zeit: ' + str(int_time) + 's', True, mainFontColor)
time_rectangle = time_surface.get_rect()
time_rectangle.topleft = (0, 300)
errors_surface = font.render('Fehler: ' + str(errors), True, mainFontColor)
errors_rectangle = errors_surface.get_rect()
errors_rectangle.topleft = (0, 400)
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:
if GPIO.input(startPin) == True:
#break
continue #skip the rest
clearScreen()
header_surface = font.render('Game over!', True, mainFontColor)
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header
pygame.display.flip()
#while GPIO.input(startPin) == False:
# handle_events()
#while GPIO.input(startPin) == True:
# handle_events()
#while GPIO.input(startPin) == False:
# handle_events()
clearScreen()
time.sleep(10)
break
#!/usr/bin/env python
#coding=utf-8
import pygame, 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
timePerError = 5
screenSizeX = 900
screenSizeY = 900
pygame.init()
#colors
redColor = pygame.Color(255, 0, 0)
blackColor = pygame.Color(0, 0, 0)
whiteColor = pygame.Color(255, 255, 255)
grayColor = pygame.Color(128, 128, 128)
mainFontColor = redColor
#fonts
font = pygame.font.Font('freesansbold.ttf', 90)
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()
return
def exit_application():
pygame.quit()
sys.exit()
return
def shutdown_raspberry():
os.system("sudo shutdown -h now")
return
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()
while True: # main loop
#global label storage
time_surface = 0
errors_surface = 0
time_rectangle = 0
errors_rectangle = 0
header_surface = font.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor)
header_rectangle = header_surface.get_rect()
header_rectangle.topleft = (0, 100)
screen.blit(header_surface, header_rectangle)
pygame.display.flip()
while GPIO.input(startPin) == False: #wait for the user to press start button
handle_events()
errors = 0
errorAdded = False