def FlashingDot(colour,animationSpeed = 100): for rows in range(0,8): for columns in range(0,8): flashColour = colour rectangle = buttons[rows,columns] origSurf = DISPLAYSURF.copy() flashSurf = pygame.Surface((LEDSIZE, LEDSIZE)) flashSurf = flashSurf.convert_alpha() r, g, b = flashColour for start, end, step in ((0, 255, 1), (255, 0, -1)): for alpha in range(start, end, animationSpeed * step): checkForQuit() DISPLAYSURF.blit(origSurf, (0, 0)) flashSurf.fill((r, g, b, alpha)) DISPLAYSURF.blit(flashSurf, rectangle.topleft) pygame.display.update() FPSCLOCK.tick(FPS) bci.turnOnLed(colour,columns,rows) bci.turnOffAll() DISPLAYSURF.blit(origSurf, (0, 0))
def main(): global FPSCLOCK, yellowArray, redArray, greenArray, bus, piano1, \ piano2, piano3, piano4, piano5, piano6, piano7, piano8, \ DISPLAYSURF, CLEAR_SURF, CLEAR_BUTTON, NONE_SURF, \ NONE_BUTTON, BASICFONT, LEDInitialColour, GREEN_SURF, \ GREEN_BUTTON, YELLOW_SURF, YELLOW_BUTTON, RED_SURF, \ RED_BUTTON, SEE_SURF, SEE_BUTTON, BASICTEXTFONT, \ DEMO_SURF, DEMO_BUTTON, stateOfSelection, previousSelection, \ GREENMODE, REDMODE, YELLOWMODE, NONEMODE # For the LED matrix - we need 3 arrays - one for each colour, # where we will keep what values need to be send to the matrix to # lit up a pattern yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] # Initialising the game state pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption('Pi Light') BASICFONT = pygame.font.Font('freesansbold.ttf', 16) BASICTEXTFONT = pygame.font.Font('freesansbold.ttf', 30) infoSurfOne = BASICFONT.render('Choose your pattern. On your left you can select colours.', 2, WHITE) infoSurfTwo = BASICFONT.render('To unselect a button press NONE. When you are finished press SEE.', 2, WHITE) infoSurfThree = BASICFONT.render('To clear all selection press CLEAR. To see some demos press DEMO.', 2, WHITE) infoRectOne = infoSurfOne.get_rect() infoRectOne.topleft = (10, WINDOWHEIGHT - 60) infoRectTwo = infoSurfTwo.get_rect() infoRectTwo.topleft = (10, WINDOWHEIGHT - 40) infoRectThree = infoSurfThree.get_rect() infoRectThree.topleft = (10, WINDOWHEIGHT - 20) # Sounds to play when a button is pressed piano1 = pygame.mixer.Sound('piano-a.wav') piano2 = pygame.mixer.Sound('piano-b.wav') piano3 = pygame.mixer.Sound('piano-c.wav') piano4 = pygame.mixer.Sound('piano-d.wav') piano5 = pygame.mixer.Sound('piano-e.wav') piano6 = pygame.mixer.Sound('piano-f.wav') piano7 = pygame.mixer.Sound('piano-#f.wav') piano8 = pygame.mixer.Sound('piano-g.wav') # Create the change LED colour buttons GREEN_SURF,GREEN_BUTTON = makeText(' GREEN ',TEXTColour,GREEN, XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 1*(LEDSIZE + BUTTONGAPSIZE)) YELLOW_SURF,YELLOW_BUTTON = makeText('YELLOW',TEXTColour,YELLOW, XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 2*(LEDSIZE + BUTTONGAPSIZE)) RED_SURF,RED_BUTTON = makeText(' RED ',TEXTColour,RED, XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 3*(LEDSIZE + BUTTONGAPSIZE)) # Create a button to unselect a single LED NONE_SURF,NONE_BUTTON = makeText(' NONE ',TEXTColour,DARKGRAY, XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 4*(LEDSIZE + BUTTONGAPSIZE)) # Create a button to clear the entire board CLEAR_SURF,CLEAR_BUTTON = makeText(' CLEAR ',TEXTColour,GRAY, XMARGIN - 2*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 5*(LEDSIZE + BUTTONGAPSIZE)) # Create a button, which will display the pattern, # selected by the user SEE_SURF, SEE_BUTTON = makeText(' SEE ',TEXTColour,BLUE, XMARGIN + 1.5*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 8*(LEDSIZE + BUTTONGAPSIZE)) # Create a button to dispaly some demos DEMO_SURF, DEMO_BUTTON = makeText(' DEMO ',TEXTColour,PURPLE, XMARGIN + 4.5*(LEDSIZE + BUTTONGAPSIZE), YMARGIN + 8*(LEDSIZE + BUTTONGAPSIZE)) # Initialize some variables for a new game pattern = [] # stores the pattern of LEDs clicked # Set the Colour of all the LEDs to be the LEDColour initialColour(pattern) # When false it indicates that the player cannot click on the gray buttons drawOnBoard = True # Light green LEDs GREENMODE = 1 # Light red LEDs REDMODE = 2 # Light yellow LEDs YELLOWMODE = 3 # Unselect a LED NONEMODE = 4 # Which of the above is selected, initially green stateOfSelection = GREENMODE # Which mode was previously selected previousSelection = 0 while True: # main game loop bci.turnOffAll() # Initialise the chip bci.initialise() clickedButton = None # button that was clicked DISPLAYSURF.fill(backgroundColour) drawAllButtons() # If something is drawn it is shown on the board for button in pattern: drawButtonWithColour(button, getFlashColour(button)) DISPLAYSURF.blit(infoSurfOne, infoRectOne) DISPLAYSURF.blit(infoSurfTwo, infoRectTwo) DISPLAYSURF.blit(infoSurfThree, infoRectThree) checkForQuit() for event in pygame.event.get(): # event handling loop if event.type == MOUSEBUTTONUP and event.button == 1: mousex, mousey = event.pos clickedButton = getButtonClicked(mousex, mousey) # If the pressed button was GREEN - allows user to turn on # green LEDs if clickedButton == GREEN: flashButtonAnimationBig(clickedButton) stateOfSelection = GREENMODE # If the pressed button was GREEN - allows user to turn on # yellow LEDs elif clickedButton == YELLOW: flashButtonAnimationBig(clickedButton) stateOfSelection = YELLOWMODE # If the pressed button was GREEN - allows user to turn on # red LEDs elif clickedButton == RED: flashButtonAnimationBig(clickedButton) stateOfSelection = REDMODE # If the pressed button was GREEN - allows user to turn off # all coloured LEDs elif clickedButton == DARKGRAY: flashButtonAnimationBig(clickedButton) previousSelection = stateOfSelection stateOfSelection = NONEMODE # Clears everything elif clickedButton == GRAY: flashButtonAnimationBig(clickedButton) if stateOfSelection == NONEMODE: stateOfSelection = previousSelection pattern = [] initialColour(pattern) yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] #If the pressed button was SEE - display the pattern elif clickedButton == BLUE: drawOnBoard = False # If nothing has been selected - skip and allow the # to select again if pattern == []: drawOnBoard = True else: pygame.display.update() pygame.time.wait(1000) for button in pattern: drawButtonWithColour(button, getButtonColour(button)) flashButtonAnimationBig(clickedButton) for button in pattern: flashColour(button) if button != None: bci.turnOnLed(getFlashColour(button), getButtonRow(button), getButtonColumn(button)) pygame.time.wait(FLASHDELAY) bci.multiplexing(greenArray,redArray, yellowArray,300) # Turn off all the Leds and clear all arrays bci.turnOffAll() pattern = [] yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] for rows in range(0,8): for columns in range(0,8): buttonsColour[rows,columns] = DARKGRAY pygame.display.update() drawOnBoard = True normalMode = True # If the pressed button was DEMO - play the demo elif clickedButton == PURPLE: flashButtonAnimationBig(clickedButton) for index in range(0,5): FlashDisplay(BRIGHTYELLOW) drawAllButtonsWithColour(DARKGRAY) # Turn on the Leds bci.turnOffAll() pygame.display.update() FlashDisplay(BRIGHTRED) drawAllButtonsWithColour(DARKGRAY) # Turn off the Leds bci.turnOffAll() pygame.display.update() FlashDisplay(BRIGHTGREEN) drawAllButtonsWithColour(DARKGRAY) # Turn on the Leds bci.turnOffAll() pygame.display.update() index = index + 1 # Show some random shapes RaspberryPi() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) Hi() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) FlashingDot(BRIGHTYELLOW) bci.turnOffAll() ChristmasTree() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) pygame.display.update() FlashingDot(BRIGHTGREEN) bci.turnOffAll() Sun() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) pygame.display.update() FlashingDot(BRIGHTRED) bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) pygame.display.update() Present() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) Candle() bci.turnOffAll() yellowArray = [0,0,0,0,0,0,0,0] greenArray = [0,0,0,0,0,0,0,0] redArray = [0,0,0,0,0,0,0,0] drawAllButtonsWithColour(DARKGRAY) ChasingLights() bci.turnOffAll() # Deal with buttons from the matrix pressed else: if drawOnBoard: # Save the current colour for later use previousColour = getButtonColour(clickedButton) if clickedButton in pattern: changeButtonColour(clickedButton,pattern) arrayChangeColour(clickedButton,previousColour) else: if stateOfSelection != NONEMODE: changeButtonColour(clickedButton,pattern) pattern.append(clickedButton) arraysAdd(clickedButton) pygame.display.update() FPSCLOCK.tick(FPS)
def ChasingLights(): for rows in range(0,8): if rows % 2 == 0: for columns in range(0,10): if columns >= 0 and columns < 8: drawButtonWithColour(buttons[columns,rows], BRIGHTYELLOW) bci.turnOnLed(BRIGHTYELLOW,rows,columns) pygame.display.update() if columns > 0 and columns < 9: drawButtonWithColour(buttons[columns - 1,rows], BRIGHTGREEN) bci.turnOnLed(BRIGHTGREEN,rows,columns - 1) pygame.display.update() if columns > 1: drawButtonWithColour(buttons[columns - 2,rows], BRIGHTRED) bci.turnOnLed(BRIGHTRED,rows,columns - 2) pygame.display.update() else: for columns in range(8,-3, -1): if columns < 8 and columns >= 0: drawButtonWithColour(buttons[columns,rows], BRIGHTYELLOW) bci.turnOnLed(BRIGHTYELLOW,rows,columns) pygame.display.update() if columns < 7 and columns >= -1 : drawButtonWithColour(buttons[columns + 1,rows], BRIGHTGREEN) bci.turnOnLed(BRIGHTGREEN,rows,columns + 1) pygame.display.update() if columns < 6 and columns >= -2: drawButtonWithColour(buttons[columns + 2,rows], BRIGHTRED) bci.turnOnLed(BRIGHTRED,rows,columns + 2) pygame.display.update()