pxt-calliope/docs/getting-started/rock-paper-scissors.md

206 lines
4.5 KiB
Markdown
Raw Normal View History

2016-06-24 08:52:24 +02:00
# Rock Paper Scissors
### ~avatar avatar
Build a Rock Paper Scissors game with the @boardname@! You can play
2016-11-02 01:44:37 +01:00
the game with a friend who has it on a @boardname@. You can also play
2016-06-24 08:52:24 +02:00
it with friends who are just using their hands. (The game is built
like a coin flipper, but with three choices instead of two.)
### ~
## Step 1: Getting started
2016-11-02 01:44:37 +01:00
We want the @boardname@ to choose rock, paper, or scissors when you
2016-06-24 08:52:24 +02:00
shake it. Try creating an ``on shake`` block so when you shake the
2016-11-02 01:44:37 +01:00
@boardname@, it will run part of a program.
2016-06-24 08:52:24 +02:00
Clear up the blocks and add the blocks below.
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
2016-11-02 01:44:37 +01:00
Next, when you shake the @boardname@, it should pick a random number from `0` to `2`
2016-06-24 08:52:24 +02:00
and store it in the variable `item`.
Add a ``set`` block with a variable. Then add a ``pick random`` block,
and store the random number in the variable,
like this:
```blocks
input.onGesture(Gesture.Shake, () => {
let item = Math.random(3)
})
```
### ~hint
No one can predict random numbers. That's what makes them great for Rock Paper Scissors!
### ~
Each possible number these blocks can make (`0`, `1`, or `2`) means a different picture.
We will show the right picture for that number on the LED screen.
## Step 2: Picking paper
Put an ``if`` block after the ``let`` block that checks whether
`item` is `0`. Make sure the ``if`` block has an ``else if`` part
and an ``else`` part.
Next, add a ``show leds`` block that shows a
picture of a piece of paper:
```blocks
input.onGesture(Gesture.Shake, () => {
let item = Math.random(3)
if (item == 0) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (false) {
} else {
}
})
```
## Step 3: A random rock
2016-11-02 01:44:37 +01:00
Now we are going to add a new picture for the @boardname@ to show
2016-06-24 08:52:24 +02:00
when another random number comes up.
Make the ``else if`` part check if the variable `item` is `1`.
Then add a ``show leds`` block with a picture of a rock.
```blocks
input.onGesture(Gesture.Shake, () => {
let item = Math.random(3)
if (item == 0) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (item == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
}
})
```
## Step 4: Suddenly scissors
Add a ``show leds`` block with a picture of scissors to the ``else`` part:
```blocks
input.onGesture(Gesture.Shake, () => {
let item = Math.random(3)
if (item == 0) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (item == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #
`)
}
})
```
### ~hint
You don't need to check if `item` is `2` because `2` is the only number left out of `0`, `1`, and `2`.
That's why you can use an ``else`` instead of an ``else if``.
### ~
Your game is ready!
Click **Download** to move your program to the @boardname@!
2016-06-24 08:52:24 +02:00
Have fun!
## Step 5: Are you the greatest?
Here is a way you can make your Rock Paper Scissors game better.
When button ``A`` is pressed,
2016-11-02 01:44:37 +01:00
the @boardname@ will add `1` to your score.
2016-06-24 08:52:24 +02:00
Open the ``Game`` drawer, and then add the block ``change score by 1`` to your program,
like this:
```blocks
input.onButtonPressed(Button.A, () => {
game.addScore(1)
})
```
## Step 6: Prove you're the greatest!
2016-11-02 01:44:37 +01:00
After your @boardname@ can add `1` to the score, show how many wins you have.
2016-06-24 08:52:24 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
game.addScore(1)
basic.showString("WINS:")
basic.showNumber(game.score())
})
```
## Step 7: Staying honest
2016-11-02 01:44:37 +01:00
Success! Your @boardname@ can track wins!
2016-06-24 08:52:24 +02:00
But what about losses?
Use the ``Game`` drawer to subtract `1` from your score when you press button `B`.
Here are all the blocks you will need:
```shuffle
input.onButtonPressed(Button.B, () => {
game.addScore(-1)
basic.showString("LOSSES:")
basic.showNumber(game.score())
})
```
Click **Download** to move your program to the @boardname@!
2016-06-24 08:52:24 +02:00
2016-06-24 22:27:34 +02:00
### ~button /projects
NEXT: PROJECTS!
### ~