#!/usr/bin/python import os os.chdir(os.path.dirname(os.path.realpath(__file__))) import subprocess subprocess.call('(date; pwd) >> /tmp/log', shell=True) import random, math, pygame from pygame.locals import * from virtualKeyboard import VirtualKeyboard import threading from math import cos import time import RPi.GPIO as GPIO pygame.init() #gameplay #wynik w snake, ktory wygrywa SNAKE_SCORE = 2 #predkosc gry w snake - w zakresie od 1 do 10 TICK_SNAKE = 10 #ilosc segmentow dodanych do snake po zlapaniu jablka SNAKE_ADDSIZE = 10 #hasla klawiatury ekranowej dla obu zagadek password1 = 'one' password2 = 'two' #ilosc powtorzen mp3 MP3_LOOPS = 4 #layout #font i komunikat do snake FONT_SNAKE = pygame.font.SysFont("Courier", 28) MSG_SNAKE = 'Koniec gry! Twoj wynik: ' #font klawiatury FONT_KEYBOARD = pygame.font.SysFont("Purisa", 28) #kolor klawiatury w RGB COLOR_KEYBOARD = [0, 0, 127] #kolor tekstu na klawiaturze (na klawiszach, guzikach i wpisywanego hasla) w RGB COLOR_KEYBOARD_TEXT = (0, 255, 0) #tekst na przyciskach klawiatury MSG_BTN1 = "Wpisz ja" MSG_BTN2 = "Kasuj" #grafika - jablko do snake, segmenty snake IMG_APPLE = pygame.image.load("media/apple.gif") IMG_SNAKEBODY = pygame.image.load("media/snake.gif") #obrazki poszczegolnych slajdow IMG_HELLO = pygame.image.load('media/hello_background.png') IMG_KEY = pygame.image.load('media/key_background.gif') IMG_WIN = pygame.image.load('media/congratulation_background.gif') IMG_POT = pygame.image.load('media/pot_background.png') IMG_FOAMBTN = pygame.image.load('media/foambtn_background.png') IMG_KEYPAD = pygame.image.load('media/keypad_background.png') IMG_SNAKE = pygame.image.load('media/snake_background.png') IMG_MAZE = pygame.image.load('media/maze_background.png') IMG_MP3 = pygame.image.load('media/sound_background.jpg') IMG_KEYBOARD = pygame.image.load('media/keyboard_background.png') IMG_DYNAMO = pygame.image.load('media/dynamo_background.jpg') wall_a = 0 #czy to RPi jest na scianie A? #pinologia #przypisania pinow - fizyczne przyciski PIN_BTNY = 17 PIN_BTNG = 27 #przypisania pinow - piny z RPi do Ardu zapalajace diode PIN_SUC4 = 2 PIN_SUC9 = 3 PIN_SUC10 = 4 #przypisania pinow - piny z Ardu do Rpi wywolujace slajd PIN_SET1 = 25 PIN_SET2 = 8 PIN_SET3 = 7 PIN_SET5 = 12 PIN_SET6 = 16 PIN_SET_WIN = 20 #przypisania pinow - zworka do GND na tym pinie ustawia, ze ta Pi jest na scianie A (uruchamia dzwiek i obrotowa lampe) PIN_WALL_A = 26 #przypisania pinow - na tym pinie jest przekaznik wlaczajacy lampe obrotowa i otwierajacy szuflade #5 PIN_RELAY = 21 #ustawianie stanow i trybow pracy pinow GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering GPIO.setup(PIN_BTNY, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_BTNG, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_SET1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_SET2, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_SET3, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_SET5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_SET6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) GPIO.setup(PIN_WALL_A, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button) if GPIO.input(PIN_WALL_A) == 0: print "this RPi is on wall A." wall_a = 1 if(wall_a): GPIO.setup(PIN_SET_WIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # ten pin jest dzielony miedzy wszystkie rpi i arduina - pullup robi tylko RPi A else: GPIO.setup(PIN_SET_WIN, GPIO.IN) print "this RPi is." GPIO.setup(PIN_SUC4, GPIO.OUT) GPIO.output(PIN_SUC4, 1) GPIO.setup(PIN_SUC9, GPIO.OUT) GPIO.setup(PIN_SUC10, GPIO.OUT) GPIO.setup(PIN_RELAY, GPIO.OUT) GPIO.output(PIN_SUC9, 1) GPIO.output(PIN_SUC10, 1) GPIO.output(PIN_RELAY, 1) WINSIZEX, WINSIZEY = 900, 680 counter = 0 timeSec = 0 UP = 1 DOWN = 3 RIGHT = 2 LEFT = 4 lastJoyState = RIGHT score = 0 dynamoValSPI = [0,0,0] #numery slajdow SLIDE_START = 0 SLIDE_KEY = 1 SLIDE_POT = 2 SLIDE_FOAMBTN = 3 SLIDE_SNAKE = 4 SLIDE_DYNAMO = 5 SLIDE_KEYPAD = 6 SLIDE_MAZE = 7 SLIDE_MP3 = 8 SLIDE_KEYBOARD = 9 SLIDE_CONGRATS = 11 SLIDE_SNAKE_DEAD = 10 #to nie slajd, tylko wartosc potrzebna do resetowania snake po przegranej global slide #slajd poczatkowy slide = SLIDE_START slides = [SLIDE_START,SLIDE_KEY,SLIDE_POT,SLIDE_FOAMBTN,SLIDE_SNAKE,SLIDE_KEYPAD,SLIDE_MAZE,SLIDE_MP3,SLIDE_KEYBOARD,SLIDE_CONGRATS] def win(): print "GAME OVER - YOU WIN" GPIO.output(PIN_RELAY, 0) def signalWin(success): pin = 0 if success == 4: pin = PIN_SUC4 elif success == 9: pin = PIN_SUC9 elif success == 10: pin = PIN_SUC10 print("signalling win for task: ") print(success) print("using GPIO pin: ") print(pin) GPIO.output(pin, 0) pygame.time.wait(200) GPIO.output(pin, 1) print("done signalling.") def waitForEvents(): global slide print("waiting for events") while 1: if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY: print "Ardu said go to key slide" slide = SLIDE_KEY return if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT: print "Ardu said go to pot slide" slide = SLIDE_POT return if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN: print "Ardu said go to foam button slide" slide = SLIDE_FOAMBTN return if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO: print "Ardu said go to dynamo slide" slide = SLIDE_DYNAMO return if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD: print "Ardu said go to keypad slide" slide = SLIDE_KEYPAD return if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS: print "Ardu said win" slide = SLIDE_CONGRATS return if GPIO.input(PIN_BTNY) == 0 and slide != SLIDE_MAZE: print "yellow button pressed - go to maze slide" slide = SLIDE_MAZE return if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3: print "green button pressed - go to mp3 slide" slide = SLIDE_MP3 return for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: slide = SLIDE_KEYBOARD print("mouse event") return elif event.type == pygame.KEYDOWN: if slide != SLIDE_SNAKE: slide = SLIDE_SNAKE else: slide = SLIDE_SNAKE_DEAD print("keydown event") return def checkEvents(): global slide if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY: print "Ardu said go to key slide" slide = SLIDE_KEY return if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT: print "Ardu said go to pot slide" slide = SLIDE_POT return if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN: print "Ardu said go to foam button slide" slide = SLIDE_FOAMBTN return if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO: print "Ardu said go to dynamo slide" slide = SLIDE_DYNAMO return if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD: print "Ardu said go to keypad slide" slide = SLIDE_KEYPAD return if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS: print "Ardu said win" slide = SLIDE_KEYPAD return if GPIO.input(PIN_BTNY) == 0 and slide != SLIDE_MAZE: print "yellow button pressed - go to maze slide" slide = SLIDE_MAZE return if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3: print "green button pressed - go to mp3 slide" slide = SLIDE_MP3 return for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN and slide != SLIDE_KEYBOARD: slide = SLIDE_KEYBOARD print("mouse event") return 1 elif event.type == pygame.KEYDOWN: slide = SLIDE_SNAKE print("keydown event") return else: return 0 def drawSlide(n): print("drawing slide:") print(n) screen.fill(WHITE) screen.blit(slides[n], (0, 0)) pygame.display.flip() waitForEvents() def collide(x1, x2, y1, y2, w1, w2, h1, h2): if x1+w1>x2 and x1y2 and y1= SNAKE_SCORE): signalWin(4) screen.blit(t, (10, 270)) pygame.display.update() pygame.time.wait(2000) waitForEvents() def main(): global slide startonce = 0 showstartscreen = 0 while startonce == 0: startonce = 0 ######## CONSTANTS WINSIZE = [WINSIZEX,WINSIZEY] BLOCKSIZE = [18,18] UP = 1 DOWN = 3 RIGHT = 2 LEFT = 4 WHITE = [255, 255, 255] MINX = 100 MAXX = WINSIZEX - MINX MINY = 100 MAXY = WINSIZEY - MINY SNAKESTEP = 20 TRUE = 1 FALSE = 0 ######## VARIABLES direction = RIGHT # 1=up,2=right,3=down50,4=left snakexy = [300,400] snakelist = [[300,400],[280,400],[260,400]] counter = 0 score = 0 appleonscreen = 0 applexy = [0,0] newdirection = RIGHT snakedead = FALSE gameregulator = 6 gamepaused = 0 growsnake = 0 # added to grow tail by two each time snakegrowunit = 2 # added to grow tail by two each time clock = pygame.time.Clock() screen = pygame.display.set_mode(WINSIZE)#FULLSCREEN) #screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) pygame.display.set_caption('PozySejf') pygame.FULLSCREEN #screen = display.set_mode([1024,768], FULLSCREEN) #screen.fill(BLACK) dynamoValSPI = [0,0,0] dynamoRectY = [50, 50, 50] dynamoRectX = 50 dynamoXpos = [-100, 0, 100] distanceDynamoRect = 300 textInputDotNumber = 0 pushButtonNumber = 100 valueSound = 100 passwordLetter = '' #### show initial start screen ### every while loop for each slide while slide == SLIDE_START: print("showing slide 0") screen.fill(WHITE) screen.blit(IMG_HELLO, (0, 0)) pygame.display.flip() pygame.time.wait(5000) waitForEvents() break while slide == SLIDE_KEY: print("showing slide 1 - key") screen.fill(WHITE) screen.blit(IMG_KEY, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_POT: print("showing slide 2 - pot") screen.fill(WHITE) screen.blit(IMG_POT, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_FOAMBTN: print("showing slide 3 - foam button") screen.fill(WHITE) screen.blit(IMG_FOAMBTN, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_SNAKE: ## snake print("snake is starting...") xs = [290, 290, 290, 290, 290]; ys = [290, 270, 250, 230, 210]; dirs = 0 score = 0 applepos = (random.randint(0, WINSIZEX), random.randint(0, WINSIZEY)) pygame.init() #s=pygame.display.set_mode((600, 600)) appleimage = IMG_APPLE img = IMG_SNAKEBODY#pygame.Surface((20, 20)) #img.fill((255, 0, 0)) f = FONT_SNAKE clock = pygame.time.Clock() while slide == SLIDE_SNAKE: clock.tick(TICK_SNAKE) if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY: print "Ardu said go to key slide" slide = SLIDE_KEY break if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT: print "Ardu said go to pot slide" slide = SLIDE_POT break if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN: print "Ardu said go to foam button slide" slide = SLIDE_FOAMBTN break if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO: print "Ardu said go to dynamo slide" slide = SLIDE_DYNAMO break if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD: print "Ardu said go to keypad slide" slide = SLIDE_KEYPAD break if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS: print "Ardu said win" slide = SLIDE_KEYPAD break if GPIO.input(PIN_BTNY) == 0: print "yellow button pressed - go to maze slide" slide = SLIDE_MAZE break if GPIO.input(PIN_BTNG) == 0: print "green button pressed - go to mp3 slide" slide = SLIDE_MP3 break for e in pygame.event.get(): #if e.type == QUIT: # sys.exit(0) #quit - nie chcemy miec mozliwosci wyjscia z aplikacji if e.type == KEYDOWN: if e.key == K_UP and dirs != 0:dirs = 2 elif e.key == K_DOWN and dirs != 2:dirs = 0 elif e.key == K_LEFT and dirs != 1:dirs = 3 elif e.key == K_RIGHT and dirs != 3:dirs = 1 elif e.type == pygame.MOUSEBUTTONDOWN: slide = SLIDE_KEYBOARD print("mouse event") break i = len(xs)-1 while i >= 2: if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20): die(screen, score) score = 0 break i-= 1 if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10): score+=1 for i in range(0, SNAKE_ADDSIZE): xs.append(700) ys.append(700) applepos=(random.randint(0,WINSIZEX),random.randint(0,WINSIZEY)) if xs[0] < 0 or xs[0] > WINSIZEX or ys[0] < 0 or ys[0] > WINSIZEY: die(screen, score) score = 0 break i = len(xs)-1 while i >= 1: xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1 if dirs==0:ys[0] += 20 elif dirs==1:xs[0] += 20 elif dirs==2:ys[0] -= 20 elif dirs==3:xs[0] -= 20 #rysuj tlo screen.fill(WHITE) screen.blit(IMG_SNAKE, (0, 0)) print("drawing snake...") if(score > SNAKE_SCORE): die(screen, score) break for i in range(0, len(xs)): screen.blit(img, (xs[i], ys[i])) screen.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));screen.blit(t, (10, 10));pygame.display.update() while slide == SLIDE_SNAKE_DEAD: print("resetting snake.") slide = SLIDE_SNAKE while slide == SLIDE_DYNAMO: print("showing slide 5 - dynamo") screen.fill(WHITE) screen.blit(IMG_DYNAMO, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_KEYPAD: print("showing slide 6 - drawer keypad") screen.fill(WHITE) screen.blit(IMG_KEYPAD, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_MAZE: print("showing slide 7 - maze") screen.fill(WHITE) screen.blit(IMG_MAZE, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_MP3: print("showing slide 8 - mp3 player") screen.fill(WHITE) screen.blit(IMG_MP3, (0, 0)) pygame.display.flip() waitForEvents() break while slide == SLIDE_KEYBOARD: ## keyboard #print("checking events") checkEvents() #render backdrop screen.fill(WHITE) screen.blit(IMG_KEYBOARD, (0, 0)) font = FONT_KEYBOARD #render keys height1 = 480-40 height2 = 560-40 height3 = 640-40 letterMeaning = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ' '] letterQM = [] for i in range(10): letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([i*80 +120,height1],[70 , 70]))) tt = font.render(letterMeaning[i], True, COLOR_KEYBOARD_TEXT) #tt = font.render(str(i), True, (0, 0, 0)) screen.blit(tt, (i*80 +120+20,height1+15)) for j in range(9): letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([j*80 +160,height2],[70 , 70]))) tt = font.render(letterMeaning[j+10], True, COLOR_KEYBOARD_TEXT) #tt = font.render(str(j+10), True, (0, 0, 0)) screen.blit(tt, (j*80 +160+20,height2+15)) for k in range(8): letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([k*80 +200,height3],[70 , 70]))) tt = font.render(letterMeaning[k+19], True, COLOR_KEYBOARD_TEXT) #tt = font.render(str(k+19), True, (0, 0, 0)) screen.blit(tt, (k*80 +200+20,height3+15)) #render buttons enterButton = pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([WINSIZEX/2 - 100 - 70,300],[140 , 70])) tt = font.render(MSG_BTN1, True, COLOR_KEYBOARD_TEXT) screen.blit(tt, [WINSIZEX/2 - 100 - 70,300]) clearButton = pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([WINSIZEX/2 + 100 - 70,300],[140 , 70])) tt = font.render(MSG_BTN2, True, COLOR_KEYBOARD_TEXT) screen.blit(tt, [WINSIZEX/2 + 100 - 70,300]) #render password as it is typed tt = font.render(passwordLetter, True, COLOR_KEYBOARD_TEXT) screen.blit(tt, [WINSIZEX/2 - 70,380]) #interactive features for event in pygame.event.get(): #if event.type == pygame.QUIT: # exit() if event.type == pygame.MOUSEBUTTONDOWN: # Set the x, y postions of the mouse click x, y = event.pos if enterButton.collidepoint(x, y): if passwordLetter == password1: print 'password correct for task 9.' signalWin(9) if passwordLetter == password2: print 'password correct for task 10.' signalWin(10) passwordLetter = '' if clearButton.collidepoint(x, y): passwordLetter = '' for i in range(10+9+8): if letterQM[i].collidepoint(x, y): passwordLetter = passwordLetter + letterMeaning[i] print passwordLetter pygame.display.flip() while slide == SLIDE_CONGRATS: ## congratulation print("showing slide 10 - win") screen.fill(WHITE) screen.blit(IMG_WIN, (0, 0)) pygame.display.flip() if(wall_a): win() pygame.mixer.init() pygame.mixer.music.load("media/win.mp3") pygame.mixer.music.play(MP3_LOOPS) pygame.mixer.music.set_volume(1.0) while pygame.mixer.music.get_busy() == True: continue while 1: print "you win." waitForEvents() if __name__ == '__main__': main()