pxt-microbit
Version:
micro:bit target for Microsoft MakeCode (PXT)
87 lines (62 loc) β’ 2.95 kB
Markdown
# Rock Paper Scissors
### @explicitHints true
## {Introduction @unplugged}

Turn your micro:bit into a **Rock Paper Scissors** game that you can play with your friends!
## {Step 1 @fullscreen}
We'll start our Rock Paper Scissors game when we shake π our micro:bit. Add anΒ ``||input:on shake||`` function to run code when you shake the @boardname@. Type the code below, or drag a code snippet from the ``||input:Input||`` Toolbox category.
```spy
input.onGesture(Gesture.Shake, function () {
})
```
## {Step 2}
Create a variable named "hand" - this will help us keep track of whether we have a Rock, Paper or Scissors in our hand. Then inside the ``||input:on shake||`` function, use the ``||math:randint||`` function to set the hand variable to a random number from **1** to **3** representing a Rock, Paper or Scissors.
```spy
let hand = 0
input.onGesture(Gesture.Shake, function () {
hand = randint(1, 3)
})
```
## {Step 3}
To check the value of the hand variable, type ``||logic:if hand==1||`` then use the ``||basic:show icon||`` function to show a small square icon representing a π Rock.
```spy
let hand = 0
input.onGesture(Gesture.Shake, function () {
hand = randint(1, 3)
if (hand == 1) {
basic.showIcon(IconNames.SmallSquare)
}
})
```
## {Step 4}
Now add an ``||logic:else if||`` clause to check if the hand value is **2**. In that case, use the ``||basic:show icon||`` function to show a large square icon representing π Paper.
```spy
let hand = 0;
input.onGesture(Gesture.Shake, function() {
hand = randint(1, 3)
if (hand == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (hand == 2) {
basic.showIcon(IconNames.Square)
}
})
```
## {Step 5}
Finally let's deal with the last condition - if our hand variable isn't holding a 1 (Rock) or a 2 (Paper), then it must be 3 (Scissors)! Add an ``||logic:else||`` clause and use the ``||basic:show icon||`` function to show βοΈ Scissors.
```spy
let hand = 0;
input.onGesture(Gesture.Shake, function() {
hand = randint(1, 3)
if (hand == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (hand == 2) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
})
```
## {Step 6}
Let's test your code! Press the white **SHAKE** button on the micro:bit on-screen simulator, or move your cursor quickly back and forth over the simulator. Do you see the icons for rock, paper and scissors randomly appear? β Great job! β
## {Step 7}
If you have a @boardname@ device, connect it to your computer and click the ``|Download|`` button. Follow the instructions to transfer your code onto the @boardname@. Once your code has been downloaded, attach your micro:bit to a battery pack and challenge another micro:bit or a human to a game of π Rock, π Paper, βοΈ Scissors!