The show_outcome() function will be created to, as the name implies, show the outcome of the game. The first parameter will be the player who's button was pressed. The second parameter is a boolean to indicate if the button press was a winning press (true) or a misdraw (false).

By generalizing the outcome code, to be able to show the proper NeoPixels for either player and for either outcome, we make our code more compact. Otherwise we would need to duplicate a lot of code in more than one place. It also makes it easier to change the behavior of the code in the future.

The complete code for the show_outcome() function will be shown at the end. Here, we'll go through it in pieces. First, we need to declare it:

def show_outcome(player, winner):

We take in two parameters: the player who's button was pressed and whether this was a winning game or not.

Then we turn off all of the NeoPixels, just to insure a known state.

    # Turn them all off
    cpx.pixels.fill(0)

Then we set the pixel color depending on the game outcome, green for a winning game, red for a misdraw.

    # Set pixel color
    if winner:
        color = 0x00FF00
    else:
        color = 0xFF0000

Now we have a color for the NeoPixels, so turn them on for the correct player.

    # Show which player won/lost
    for p in PLAYER_PIXELS[player]:
        cpx.pixels[p] = color

And why not play a little tune. A happy one if this was a winning game, a more error sounding one if it was a misdraw.

    # Play a little tune
    if winner:
        cpx.play_tone(800, 0.2)
        cpx.play_tone(900, 0.2)
        cpx.play_tone(1400, 0.2)
        cpx.play_tone(1100, 0.2)
    else:
        cpx.play_tone(200, 1)

And we are done with the game, so we'll just sit here forever until the reset button is pressed to start a new game.

    # Sit here forever
    while True:
        pass 

OK. Let's put it all together.

This guide was first published on Dec 29, 2016. It was last updated on Dec 29, 2016.

This page (Show Outcome) was last updated on Nov 29, 2017.

Text editor powered by tinymce.