pxt-calliope/docs/getting-started/coin-flipper.md
2016-06-24 13:27:34 -07:00

1.7 KiB

The amazing coin flipper

~avatar avatar

Are you trying to choose whether to play soccer or go to the movies instead, or which toppings to have on your pizza? Build a coin flipping machine with the BBC micro:bit to choose for you!

~

Here are the blocks to make your coin flipper. When you press button B, the coin flipper will show either H for heads or T for tails on the LED screen.

input.onButtonPressed(Button.B, () => {
    if (Math.randomBoolean()) {
        basic.showString("H");
    } else {
        basic.showString("T");
    }
});

~hint

The pick random true or false block randomly tells the if block true or false. If the pick block picked true, the if block shows the letter H. Otherwise, it shows the letter T.

That's it!

~

Keeping score

~avatar

To keep track out of how many guesses you've won, add these blocks to your coin flipper:

~

input.onButtonPressed(Button.A, () => {
    game.addScore(1);
});
input.onButtonPressed(Button.AB, () => {
    basic.showNumber(game.score());
});

These blocks mean that if you press button A, you will add 1 to your score, and if you press A and B together, the micro:bit will show your score.

When you're done, your coin flipping program should look like this:

input.onButtonPressed(Button.B, () => {
    if (Math.randomBoolean()) {
        basic.showString("H");
    } else {
        basic.showString("T");
    }
});
input.onButtonPressed(Button.A, () => {
    game.addScore(1);
});
input.onButtonPressed(Button.AB, () => {
    basic.showNumber(game.score());
});

Flip until your thumbs get tired!

~button /getting-started/rock-paper-scissors

NEXT: ROCK PAPER SCISSORS

~