diff --git a/raspi/pySejf.py b/raspi/pySejf.py index fc48c9b..5ba3927 100644 --- a/raspi/pySejf.py +++ b/raspi/pySejf.py @@ -5,14 +5,10 @@ os.environ['SDL_VIDEO_CENTERED'] = '1' import serial import webapp -for c in ['/dev/ttyUSB0', '/dev/ttyACM0']: - try: - ser = serial.Serial(c, 115200) - except: - continue - print('Using ', c) - webapp.init(ser) +import threading +import logging +logging.basicConfig(level=logging.INFO) import random, math, pygame from pygame.locals import * from virtualKeyboard import VirtualKeyboard @@ -32,6 +28,12 @@ class Slide(object): def handle(self, ev): pass + def enter(self, prev_slide): + return True + + def exit(self, next_slide): + return True + class ImageSlide(Slide): def __init__(self, image): @@ -41,9 +43,91 @@ class ImageSlide(Slide): screen.fill(WHITE) screen.blit(self.image, (0, 0)) +class FinalSlide(ImageSlide): + def enter(self, prev_slide): + global wall_a + if(wall_a): + win() + pygame.mixer.music.load("media/win.mp3") + pygame.mixer.music.play(MP3_LOOPS) + pygame.mixer.music.set_volume(1.0) + def exit(self, next_slide): + return False +class SnakeSlide(Slide): + def enter(self, prev_slide=None): + self.xs = [290, 290, 290, 290, 290]; + self.ys = [290, 270, 250, 230, 210]; + self.dirs = 0 + self.score = 0 + self.applepos = (random.randint(0, WINSIZEX), random.randint(0, WINSIZEY)) + #s=pygame.display.set_mode((600, 600)) + self.appleimage = IMG_APPLE + self.img = IMG_SNAKEBODY#pygame.Surface((20, 20)) + #img.fill((255, 0, 0)) + self.f = FONT_SNAKE + self.clock = pygame.time.Clock() + + return True + + def handle(self, e): + print(e) + if e.type == KEYDOWN: + if e.key == K_UP and self.dirs != 0:self.dirs = 2 + elif e.key == K_DOWN and self.dirs != 2:self.dirs = 0 + elif e.key == K_LEFT and self.dirs != 1:self.dirs = 3 + elif e.key == K_RIGHT and self.dirs != 3:self.dirs = 1 + + print(self.dirs) + + def render(self, screen): + self.clock.tick(TICK_SNAKE) + + i = len(self.xs)-1 + while i >= 2: + if collide(self.xs[0], self.xs[i], self.ys[0], self.ys[i], 20, 20, 20, 20): + die(screen, self.score) + self.score = 0 + self.enter() + + i-= 1 + if collide(self.xs[0], self.applepos[0], self.ys[0], self.applepos[1], 20, 10, 20, 10): + self.score+=1 + for i in range(0, SNAKE_ADDSIZE): + self.xs.append(700) + self.ys.append(700) + self.applepos=(random.randint(0,WINSIZEX),random.randint(0,WINSIZEY)) + + if self.xs[0] < 0 or self.xs[0] > WINSIZEX or self.ys[0] < 0 or self.ys[0] > WINSIZEY: + die(screen, self.score) + score = 0 + self.enter() + + self.xs[1:] = self.xs[:-1] + self.ys[1:] = self.ys[:-1] + + if self.dirs==0:self.ys[0] += 20 + elif self.dirs==1:self.xs[0] += 20 + elif self.dirs==2:self.ys[0] -= 20 + elif self.dirs==3:self.xs[0] -= 20 + + #rysuj tlo + screen.fill(WHITE) + screen.blit(IMG_SNAKE, (0, 0)) + print("drawing snake...") + if(self.score > SNAKE_SCORE): + die(screen, self.score) + self.enter() + + for i in range(0, len(self.xs)): + screen.blit(self.img, (self.xs[i], self.ys[i])) + + screen.blit(self.appleimage, self.applepos); + t=self.f.render(str(self.score), True, (0, 0, 0)); + screen.blit(t, (10, 10)); pygame.init() +pygame.mixer.init() #gameplay #wynik w snake, ktory wygrywa @@ -168,28 +252,77 @@ SLIDE_DYNAMO = 5 SLIDE_KEYPAD = 6 SLIDE_MAZE = 7 SLIDE_MP3 = 8 -SLIDE_KEYBOARD = 9 -SLIDE_CONGRATS = 11 +SLIDE_KEYBOARD = 11 +SLIDE_CONGRATS = 9 SLIDE_SNAKE_DEAD = 10 #to nie slajd, tylko wartosc potrzebna do resetowania snake po przegranej global slide #slajd poczatkowy -slide = SLIDE_START +slide = SLIDE_START slides = { SLIDE_START: ImageSlide(IMG_HELLO), SLIDE_KEY: ImageSlide(IMG_KEY), SLIDE_POT: ImageSlide(IMG_POT), SLIDE_FOAMBTN: ImageSlide(IMG_FOAMBTN), - #SLIDE_SNAKE, SLIDE_DYNAMO: ImageSlide(IMG_DYNAMO), SLIDE_KEYPAD: ImageSlide(IMG_KEYPAD), - SLIDE_MAZE: ImageSlide(IMG_KEYPAD), + SLIDE_MAZE: ImageSlide(IMG_MAZE), SLIDE_MP3: ImageSlide(IMG_MP3), - #SLIDE_KEYBOARD, - #SLIDE_CONGRATS: ImageSlide(IMG_WIN) + SLIDE_CONGRATS: FinalSlide(IMG_WIN), + SLIDE_SNAKE: SnakeSlide(), } +def go_to_slide(new_slide): + global slide + + if new_slide == slide: + logging.warning('Same slide...') + return False + + if slides.get(slide) and not slides[slide].exit(new_slide): + logging.warning('exit rejected') + return False + + if slides.get(new_slide) and not slides[new_slide].enter(slide): + logging.warning('enter rejected') + return False + + slide = new_slide + return True + +class SerialThread(threading.Thread): + def run(self): + self.logger = logging.getLogger(self.__class__.__name__) + + for line in self.serial: + try: + self.handle(line) + except: + self.logger.exception('Oops...') + + def handle(self, line): + print('Arduino said:', line) + + command, _, args = line.partition(':') + + if command == 'SLIDE': + global slide + go_to_slide(int(args)) + print('Setting slide to: ', slide) + +for c in ['/dev/ttyUSB0', '/dev/ttyACM0']: + try: + ser = serial.Serial(c, 115200) + except: + continue + print('Using ', c) + webapp.init(ser) + th = SerialThread() + th.serial = ser + th.daemon = True + th.start() + def win(): print "GAME OVER - YOU WIN" @@ -216,106 +349,38 @@ def signalWin(success): def waitForEvents(): global slide print("waiting for events") - while 1: - time.sleep(0.01) - 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 + while checkEvents(): + clock.tick(30) -def checkEvents(): +def checkEvents(handler=None): 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 + go_to_slide(SLIDE_MAZE) return - if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3: + + if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3: print "green button pressed - go to mp3 slide" - slide = SLIDE_MP3 + go_to_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 + if event.type == pygame.MOUSEBUTTONDOWN: + go_to_slide(SLIDE_KEYBOARD) + print("mouse event") + return + + elif event.type == pygame.KEYDOWN: + if slide != SLIDE_SNAKE: + go_to_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() + if handler: + handler(event) + + return True def collide(x1, x2, y1, y2, w1, w2, h1, h2): if x1+w1>x2 and x1y2 and y1= 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() + clock.tick(15) while slide == SLIDE_SNAKE_DEAD: print("resetting snake.") @@ -561,30 +528,13 @@ def main(): 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()