Example #1
0
def drawBoard(invert=False):
    # handle board inversion
    redOffset = 9 if invert else 0
    greenOffset = 0 if invert else 9

    led.allOff()
    for i in range(0, 9):
        if Board[i] == RED:
            led.ledOn(i + redOffset)
        elif Board[i] == GREEN:
            led.ledOn(i + greenOffset)
Example #2
0
def resetAnimation():
    # flash existing board
    for i in range(0, 2):
        led.allOff()
        led.wiringpi.delay(SHORT_PAUSE)

        drawBoard()
        led.wiringpi.delay(SHORT_PAUSE)

    led.allOff()
    led.wiringpi.delay(LONG_PAUSE)
Example #3
0
def playGame():
    # set initial conditions
    global Turn, Board
    Board = [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
    moveButtonState = selectButtonState = NOT_PRESSED

    nextTurn()

    GameState = READY
    curSquare = flashCount = readyCount = 0
    curPin = squareToPin(curSquare)
    led.allOff()

    # the game loop
    while GameState != GAME_OVER:
        flashCount += 1  # increment the flashing led "timer"

        if GameState == ANIMATING:
            # every READY_ANIMATION_SPEED flashCounts, draw one frame of READY_ANIMATION
            if flashCount % ANIMATION_SPEED == 0:
                curLeds = ANIMATION[(flashCount / ANIMATION_SPEED) - 1]
                if curLeds[0] == END:
                    # last frame of ANIMATION: reset and back to READY
                    flashCount = 0
                    led.allOff()
                    GameState = READY
                    wiringpi.delay(MED_PAUSE)
                else:
                    if curLeds[0] == OFF:
                        # turn these leds off
                        led.ledsOff(curLeds[1:])
                    else:
                        # turn these leds on
                        led.ledsOn(curLeds)
        else:
            # flash current square
            if flashCount == 1:
                led.ledOn(curPin)
            elif flashCount == FLASH_ON:
                led.ledOff(curPin)
            elif flashCount > FLASH_ON + FLASH_OFF:
                flashCount = 0
                if GameState == READY:
                    # each flash brings us closer to ANIMATION
                    readyCount += 1

        # get button inputs
        moveButton = wiringpi.digitalRead(LEFT_BUTTON_PIN)
        selectButton = wiringpi.digitalRead(RIGHT_BUTTON_PIN)

        # --- handle select button ---
        if selectButton == DOWN:
            if selectButtonState == NOT_PRESSED:
                # act right away on button press
                selectButtonState = PRESSED
                flashCount = readyCount = 0

                if GameState == ANIMATING:
                    # stop the animation
                    GameState = READY
                    led.allOff()

                else:
                    GameState = PLAYING
                    # flash selected square and turn it on
                    for i in range(0, 3):
                        led.flashOne(curPin, SHORT_PAUSE)
                    led.ledOn(curPin)

                    # update Board and check for game over
                    Board[curSquare] = Turn
                    winningLine = checkForWin()

                    if winningLine:
                        # we have a winner!
                        GameState = GAME_OVER

                        # flash winning line
                        winningAnimation(winningLine)

                    else:
                        # no winner, continue on
                        nextTurn()
                        curSquare, curPin = nextEmpty(8)

                        if curSquare < 0:
                            # no more empty squares? it's a tie!
                            GameState = GAME_OVER
                            print " + It's a tie!"
                            gameTiedAnimation()
                            nextTurn(False)  # flip first player on ties

                # clear the other button's state
                moveButton = UP
                moveButtonState = NOT_PRESSED

        else:
            if selectButtonState == PRESSED:
                selectButtonState = NOT_PRESSED
                # pause to avoid button flooding
                wiringpi.delay(SHORT_PAUSE)

        # --- handle move button ---
        if moveButton == DOWN:
            if moveButtonState == NOT_PRESSED:
                # act right away on button press
                moveButtonState = PRESSED
                flashCount = readyCount = 0

                if GameState == ANIMATING:
                    # stop the animation
                    GameState = READY
                    led.allOff()

                else:
                    # advance the flashing led to the next empty square
                    led.ledOff(squareToPin(curSquare))
                    curSquare, curPin = nextEmpty(curSquare)

                # clear the other button's state
                selectButtonState = NOT_PRESSED

        else:
            if moveButtonState == PRESSED:
                moveButtonState = NOT_PRESSED
                # pause to avoid button flooding
                wiringpi.delay(SHORT_PAUSE)

        if GameState == READY and readyCount > READY_FLASHES:
            # too long in READY state? play the ANIMATION for attention!
            GameState = ANIMATING
            readyCount = 0

    # GAME_OVER
    led.wiringpi.delay(LONG_PAUSE * 8)
    resetAnimation()