2018-10-17 00:58:27 +02:00
# Rock Paper Scissors
2016-05-26 18:15:10 +02:00
2018-10-17 00:58:27 +02:00
## Introduction @unplugged
2016-05-26 18:15:10 +02:00
2018-10-17 00:58:27 +02:00
![Cartoon of the Rock Paper Scissors game ](/static/mb/projects/a4-motion.png )
2016-05-26 18:15:10 +02:00
2018-10-25 06:51:53 +02:00
Use the accelerometer and the screen to build a **Rock Paper Scissors** game that you can play with your friends!
2016-05-26 18:15:10 +02:00
2018-10-17 00:58:27 +02:00
## Step 1 @fullscreen
2017-09-11 19:49:40 +02:00
2018-10-17 00:58:27 +02:00
Add a ``||input:on shake||`` block to run code when when you shake the @boardname @.
2017-09-11 19:49:40 +02:00
2018-10-17 00:58:27 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
2016-05-27 01:18:33 +02:00
2018-10-17 00:58:27 +02:00
## Step 2 @fullscreen
2016-05-26 18:15:10 +02:00
2018-10-25 06:51:53 +02:00
Add a ``hand`` variable and place the ``||variables:set hand to||`` block in the shake event.
2016-05-26 18:15:10 +02:00
2018-10-17 14:56:21 +02:00
![A animation that shows how to create a variable ](/static/mb/projects/rock-paper-scissors/newvar.gif )
## Step 3 @fullscreen
2018-10-25 06:51:53 +02:00
Add a ``||math:pick random||`` block to pick a random number from `1` to `3` and store it in the variable named ``hand``.
2016-05-27 01:18:33 +02:00
2018-10-17 00:58:27 +02:00
```blocks
2018-10-17 14:56:21 +02:00
let hand = 0;
2018-10-17 00:58:27 +02:00
input.onGesture(Gesture.Shake, () => {
2018-10-17 14:56:21 +02:00
hand = Math.randomRange(1, 3)
2018-10-17 00:58:27 +02:00
})
```
2018-10-17 14:56:21 +02:00
In a later step, each of the possible numbers (`1`, `2` , or `3` ) is matched to its own picture. The picture is shown on the LEDs when its matching number is picked.
2018-10-17 00:58:27 +02:00
2018-10-17 14:56:21 +02:00
## Step 4 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-25 06:51:53 +02:00
Place an ``||logic:if||`` block under the ``||math:pick random||`` and check whether ``hand`` is equal to ``1``. Add a ``||basic:show leds||`` block that shows a picture of a piece of paper. The number `1` will mean paper.
2018-10-17 00:58:27 +02:00
2018-10-22 23:05:39 +02:00
![How to drag an if statement ](/static/mb/projects/rock-paper-scissors/if.gif )
2018-10-17 00:58:27 +02:00
2019-02-02 02:57:58 +01:00
```blocks
let hand = 0;
input.onGesture(Gesture.Shake, () => {
hand = Math.randomRange(1, 3)
if (hand == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
}
})
```
2018-10-17 14:56:21 +02:00
## Step 5 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-17 02:57:26 +02:00
Click on the **SHAKE** button in the simulator. If you try enough times, you should see a picture of paper on the screen.
2018-10-17 00:58:27 +02:00
![Shaking a @boardname@ simulator ](/static/mb/projects/rock-paper-scissors/rpsshake.gif )
2018-10-17 14:56:21 +02:00
## Step 6 @fullscreen
2018-10-17 00:58:27 +02:00
2019-02-02 02:57:58 +01:00
Click the ** (+)** button to add an ``||logic:else||`` section.
2018-10-17 00:58:27 +02:00
![Adding an else clause ](/static/mb/projects/rock-paper-scissors/ifelse.gif )
2019-02-02 02:57:58 +01:00
```blocks
let hand = 0;
input.onGesture(Gesture.Shake, () => {
hand = Math.randomRange(1, 3)
if (hand == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else {
}
})
```
2018-10-17 14:56:21 +02:00
## Step 7 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-17 02:57:26 +02:00
Add a ``||basic:show leds||`` block inside the ``||logic:else||``. Make a picture of a scissors in the LEDs.
2018-10-17 00:58:27 +02:00
```blocks
2018-10-17 14:56:21 +02:00
let hand = 0;
2018-10-17 00:58:27 +02:00
input.onGesture(Gesture.Shake, () => {
2018-10-17 14:56:21 +02:00
hand = Math.randomRange(1, 3)
if (hand == 1) {
2018-10-17 00:58:27 +02:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else {
basic.showLeds(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #
`)
}
})
```
2018-10-17 14:56:21 +02:00
## Step 8 @fullscreen
2018-10-17 00:58:27 +02:00
2018-12-06 11:40:30 +01:00
Click the ``+`` button again to add an ``||logic:else if||`` section. Now, add a conditional block for ``||logic:hand = 2||`` to the condition in ``||logic:else if||``. Since ``hand`` can only be `1` , `2` , or `3` , your code is covering all possible cases!
2018-10-17 00:58:27 +02:00
![Adding an else if clause ](/static/mb/projects/rock-paper-scissors/ifelseif.gif )
2018-10-17 14:56:21 +02:00
## Step 9 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-17 02:57:26 +02:00
Get one more ``||basic:show leds||`` block and put it in the ``||logic:else if||``. Make a picture of a rock in the LEDs.
2018-10-17 00:58:27 +02:00
```blocks
2018-10-17 14:56:21 +02:00
let hand = 0;
2018-10-17 00:58:27 +02:00
input.onGesture(Gesture.Shake, () => {
2018-10-17 14:56:21 +02:00
hand = Math.randomRange(1, 3)
if (hand == 1) {
2018-10-17 00:58:27 +02:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
2018-10-17 14:56:21 +02:00
} else if (hand == 2) {
2018-10-17 00:58:27 +02:00
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #
`)
}
})
```
2018-10-17 14:56:21 +02:00
## Step 10 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-17 02:57:26 +02:00
Click on the **SHAKE** button in the simulator and check to see that each image is showing up.
2018-10-17 00:58:27 +02:00
![Shaking a @boardname@ simulator ](/static/mb/projects/rock-paper-scissors/rpssim3.gif )
2018-10-17 14:56:21 +02:00
## Step 11 @fullscreen
2018-10-17 00:58:27 +02:00
2018-10-17 02:57:26 +02:00
If you have a @boardname @, click on ``|Download|`` and follow the instructions to get the code
2018-10-17 00:58:27 +02:00
onto your @boardname @. Your game is ready! Gather your friends and play Rock Paper Scissors!
2018-10-17 02:57:26 +02:00
![A @boardname@ in a hand ](/static/mb/projects/rock-paper-scissors/hand.jpg )