pxt-calliope/docs/projects/rock-paper-scissors.md

239 lines
5.2 KiB
Markdown
Raw Normal View History

2016-05-26 18:15:10 +02:00
# rock paper scissors
2016-06-12 01:43:54 +02:00
![](/static/mb/projects/a4-motion.png)
2016-05-26 18:15:10 +02:00
### ~avatar avatar
2016-05-27 00:10:46 +02:00
```sim
input.onGesture(Gesture.Shake, () => {
let img = Math.random(3)
if (img == 0) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
2016-05-26 18:15:10 +02:00
2016-05-27 00:10:46 +02:00
} else if (img == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #
`)
}
})
```
In this project, you will build a Rock Paper Scissors game with the BBC micro:bit.
You can play the game with a friend who has it on a micro:bit.
You can also play it with friends who are just using their hands.
2016-05-26 18:15:10 +02:00
### ~
2016-05-26 18:15:10 +02:00
## Materials needed
* Your BBC micro:bit -- that's it!
2016-05-26 18:15:10 +02:00
## Step 1: Getting started
2016-05-26 18:15:10 +02:00
We want the micro:bit to choose rock, paper, or scissors when you shake it.
Try creating an ``on shake`` block so when you shake the micro:bit, it will run part of a program.
2016-05-26 18:15:10 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
Next, when you shake the micro:bit, it should pick a random number from `0` to `2`
and store it in the variable `weapon`. (This variable is named `weapon` because
rock, paper, and scissors are the weapons you use to battle your friends!)
Add a ``set`` block with a variable. Then add a ``pick random`` block,
and store the random number in the variable,
like this:
2016-05-26 18:15:10 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
2016-05-26 18:15:10 +02:00
})
```
### ~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
`weapon` 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:
2016-05-26 18:15:10 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 18:15:10 +02:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (false) {
} else {
2016-05-26 18:15:10 +02:00
}
})
```
## Step 3: A random rock
2016-05-26 18:15:10 +02:00
Now we are going to add a new picture for the micro:bit to show
when another random number comes up.
2016-05-26 18:15:10 +02:00
Make the ``else if`` part check if the variable `weapon` is `1`.
Then add a ``show leds`` block with a picture of a rock.
2016-05-26 18:15:10 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 18:15:10 +02:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (weapon == 1) {
2016-05-26 18:15:10 +02:00
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
2016-05-26 18:15:10 +02:00
}
})
```
## Step 4: Suddenly scissors
2016-05-26 18:15:10 +02:00
Add a ``show leds`` block with a picture of scissors to the ``else`` part:
2016-05-26 18:15:10 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 18:15:10 +02:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (weapon == 1) {
2016-05-26 18:15:10 +02:00
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
2016-05-27 00:10:46 +02:00
# # . . #
2016-05-26 18:15:10 +02:00
# # . # .
. . # . .
# # . # .
2016-05-27 00:10:46 +02:00
# # . . #
2016-05-26 18:15:10 +02:00
`)
}
})
```
### ~hint
You don't need to check if `weapon` 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``.
2016-05-26 18:15:10 +02:00
### ~
Your game is ready! Have fun!
## Step 5: Are you the greatest?
2016-05-26 18:15:10 +02:00
Here is a way you can make your Rock Paper Scissors game better.
When button ``A`` is pressed,
the micro:bit will add `1` to your score.
Open the ``Game`` drawer, and then add the block ``change score by 1`` to your program,
like this:
2016-05-26 18:15:10 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
game.addScore(1)
})
```
## Step 6: Prove you're the greatest!
2016-05-26 18:15:10 +02:00
After your micro:bit can add `1` to the score, show how many wins you have.
2016-05-26 18:15:10 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
game.addScore(1)
basic.showString("WINS:")
basic.showNumber(game.score())
})
```
## Step 7: Staying honest
Success! Your micro:bit can track wins!
But what about losses?
Use the ``Game`` drawer to subtract `1` from your score when you press button `B`.
2016-05-26 18:15:10 +02:00
Here are all the blocks you will need:
2016-05-26 18:15:10 +02:00
```shuffle
input.onButtonPressed(Button.B, () => {
game.addScore(-1)
basic.showString("LOSSES:")
basic.showNumber(game.score())
})
```
## Step 8: Hacking Rock Paper Scissors
How else can you make your game better?
Ever hear of [Rock Paper Scissors Spock Lizard](http://www.samkass.com/theories/RPSSL.html)?
2016-06-25 23:22:50 +02:00