heisser_draht/highscore-test.py
2019-09-09 08:20:40 +02:00

148 lines
3.5 KiB
Python

import pygame, signal, sys, time, os
from pygame.locals import *
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
MAX_NAME_LENGTH = 10
hs1_name = "Fritz"
hs2_name = "Bernd"
hs3_name = "Max"
hs1_time = 100
hs2_time = 200
hs3_time = 300
#fonts
font1 = pygame.font.Font('freesansbold.ttf', 16)
pygame.mouse.set_visible(True) # show mouse
screen = pygame.display.set_mode((640, 480), pygame.RESIZABLE)
def clearScreen():
screen.fill(blackColor)
return
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
def enterName():
clearScreen()
print('text entry started')
name = ''
while True:
clearScreen()
textbox_surface = font1.render('Enter name: ' + str(name), True, mainFontColor)
textbox_rectangle = textbox_surface.get_rect()
textbox_rectangle.topleft = (10, 30)
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:
name = name[:-1]
elif event.key == pygame.K_RETURN:
return name
else:
name += event.unicode
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
while True:
clearScreen()
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_BACKSPACE:
print ('bs pressed')
checkHighscores(50)
header_surface = font1.render('Dr' + u'ü' + 'cken Sie Start!', True, mainFontColor)
header_rectangle = header_surface.get_rect()
header_rectangle.topleft = (10, 10)
screen.blit(header_surface, header_rectangle)
score1_surface = font1.render(hs1_name + ": " + str(hs1_time) + 's', True, mainFontColor)
score1_rectangle = score1_surface.get_rect()
score1_rectangle.topleft = (10, 30)
score2_surface = font1.render(hs2_name + ": " + str(hs2_time) + 's', True, mainFontColor)
score2_rectangle = score2_surface.get_rect()
score2_rectangle.topleft = (10, 50)
score3_surface = font1.render(hs3_name + ": " + str(hs3_time) + 's', True, mainFontColor)
score3_rectangle = score3_surface.get_rect()
score3_rectangle.topleft = (10, 70)
screen.blit(score1_surface, score1_rectangle)
screen.blit(score2_surface, score2_rectangle)
screen.blit(score3_surface, score3_rectangle)
pygame.display.flip()
time.sleep(1/60)