Пример #1
0
def winningAnimation(winningLine):
    # convert line to pins
    winningPins = winningLine
    if Turn == GREEN:
        for i in range(0, 3):
            winningPins[i] = winningLine[i] + 9

    wiringpi.delay(LONG_PAUSE)
    led.ledsOff(winningPins, MED_PAUSE)

    # flash all three
    for i in range(0, 2):
        led.ledsOnOff(winningPins, MED_PAUSE)

    led.ledsOn(winningPins, LONG_PAUSE)

    # flash one at a time
    for i in range(0, 2):
        led.ledsOff(winningPins, SHORT_PAUSE)
        for j in range(0, 3):
            led.ledOn(winningPins[j], MED_PAUSE)
        wiringpi.delay(SHORT_PAUSE)

    led.ledsOff(winningPins, SHORT_PAUSE)

    # flash all three again
    for i in range(0, 2):
        led.ledsOnOff(winningPins, SHORT_PAUSE)

    # turn back on for final board
    led.ledsOn(winningPins)
Пример #2
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()