pxt-calliope/docs/projects/coin-flipper.md

75 lines
1.6 KiB
Markdown
Raw Normal View History

2016-06-24 08:52:24 +02:00
# The amazing coin flipper
## ~avatar avatar
2016-06-24 08:52:24 +02:00
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 @boardname@ to choose for you!
2016-06-24 08:52:24 +02:00
## ~
2016-06-24 08:52:24 +02:00
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.
```blocks
input.onButtonPressed(Button.B, () => {
if (Math.randomBoolean()) {
basic.showString("H");
} else {
basic.showString("T");
}
});
```
## ~hint
2016-06-24 08:52:24 +02:00
2016-12-20 21:31:25 +01:00
The `[Math.randomBoolean()]` block randomly tells the ``if``
block `true` or `false`. If value picked is `true`, the
2016-06-24 08:52:24 +02:00
``if`` block shows the letter `H`. Otherwise, it shows the letter `T`.
That's it!
## ~
2016-06-24 08:52:24 +02:00
## Keeping score
2016-06-24 08:52:24 +02:00
### ~avatar
2016-06-24 08:52:24 +02:00
To keep track out of how many guesses you've won,
add these blocks to your coin flipper:
### ~
2016-06-24 08:52:24 +02:00
```blocks
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
2016-11-02 01:44:37 +01:00
your score, and if you press `A` and `B` together, the @boardname@ will
2016-06-24 08:52:24 +02:00
show your score.
When you're done, your coin flipping program should look like this:
```blocks
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!