kk logo

developed by Timo Denk (Jan. 2015)

#!/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
buzzerPin = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setup(startPin, GPIO.IN)
GPIO.setup(errorPin, GPIO.IN)
GPIO.setup(enableErrorTimeAddingPin, GPIO.IN)
GPIO.setup(shutdownPin, GPIO.IN)
GPIO.setup(buzzerPin, GPIO.OUT)

#other constants
timePerError = 5
screenSizeX = 640
screenSizeY = 400
startstop_delay = 3000
error_delay = 1000
game_over_screen_duration = 15
text_margin_top = 0
text_margin_left = 10
line_height = 150
buzzer_high = 2000

last_error_detection = 0
last_error_increment = 0

pygame.init()


#colors
redColor = pygame.Color(255, 255, 255)
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', 100)

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():
time.sleep(0.04)
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()

#buzzer
GPIO.output(buzzerPin, not(last_error_increment < get_millis() - buzzer_high))
#print str(not(last_error_increment < get_millis() - buzzer_high))
return

def exit_application():
pygame.quit()
sys.exit()
return

def shutdown_raspberry():
os.system("sudo shutdown -h now")
return

def getUnixTime():
return int(time.time())

def get_millis():
return int(round(time.time() * 1000))


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
last_error_detection = 0
last_error_increment = 0


header_surface = font.render('Let\'s play!', True, mainFontColor)
header_rectangle = header_surface.get_rect()
header_rectangle.midleft = (text_margin_left, (int)(screenSizeY / 2))

screen.blit(header_surface, header_rectangle)
pygame.display.flip()

while GPIO.input(startPin) == False: #wait for the user to press start button
handle_events()

start_time = getUnixTime()

errors = 0
errorAdded = False

justStarted = True


header_surface = font.render('', True, mainFontColor)

game_started = get_millis()

while True: # game running
handle_events()


if GPIO.input(errorPin) == True:
if errorAdded == False and last_error_increment < get_millis() - error_delay:
errors += 1
last_error_increment = get_millis()
else:
last_error_detection = get_millis()
errorAdded = True
else:
errorAdded = False
int_time = getUnixTime() - start_time
if GPIO.input(enableErrorTimeAddingPin) == True:
int_time = getUnixTime() - start_time + errors * timePerError
time_surface = font.render('Time: ' + str(int_time) + 's', True, mainFontColor)
time_rectangle = time_surface.get_rect()
time_rectangle.topleft = (text_margin_left, text_margin_top + line_height * 0.5)

errors_surface = font.render('Mistakes: ' + str(errors), True, mainFontColor)
errors_rectangle = errors_surface.get_rect()
errors_rectangle.topleft = (text_margin_left, text_margin_top + line_height * 1.5)


clearScreen()
screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time

header_rectangle.midleft = (text_margin_left, text_margin_top)
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 and game_started < get_millis() - startstop_delay:
break

clearScreen()


header_surface = font.render('Let\'s play!', True, mainFontColor)
#reposition for game over screen
header_rectangle.topleft = (text_margin_left, text_margin_top)
time_rectangle.topleft = (text_margin_left, text_margin_top + line_height * 1)
errors_rectangle.topleft = (text_margin_left, text_margin_top + line_height * 2)

screen.blit(errors_surface, errors_rectangle) #errors
screen.blit(time_surface, time_rectangle) #time
screen.blit(header_surface, header_rectangle) #header

pygame.display.flip()

gos_shown = get_millis()

while gos_shown > get_millis() - 15000:
handle_events()
if GPIO.input(startPin) == True and gos_shown < get_millis() - 1000:
gos_shown = 0

#while GPIO.input(startPin) == False:
# handle_events()

#while GPIO.input(startPin) == True:
# handle_events()

#while GPIO.input(startPin) == False:
# handle_events()*/

clearScreen()
time.sleep(1)

CODE (alt)

#!/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():
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()

start_time = int(time.time())

errors = 0
errorAdded = False

justStarted = True


header_surface = font.render('Spiel l' + u'ä' + 'uft!', True, mainFontColor)


while True: # game running
handle_events()


if GPIO.input(errorPin) == True:
if errorAdded == False:
errors += 1
errorAdded = True
else:
errorAdded = False
int_time = int(time.time()) - start_time
if GPIO.input(enableErrorTimeAddingPin) == True:
int_time = int(time.time()) - start_time + errors * timePerError
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:
break

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(1)

King of my heart

Let the King of my heart
Be the mountain where I run
The fountain I drink from
Oh, He is my song

Ronnie

schwäbischer tüftler und bastler, kraftsportler, ADHS power, 43 Jahre, 1 Frau, 5 Kinder und 1003 Ideen. 

ronnie berzins at mee