pxt-calliope/docs/reference/js/lessons/rock-paper-scissors/activity.md

55 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# rock paper scissors activity
a game against the BBC micro:bit. #docs
### ~avatar avatar
### @video td/videos/rock-paper-scissors-0
Welcome! This tutorial will help you create a game of rock paper scissors with the BBC micro:bit. Let's get started!
### ~
We want the BBC micro:bit to choose rock, paper, or scissors when it is shaken. Let's begin by creating an `input->on shake` condition so the micro:bit will run code when it is shaken.
2016-04-13 17:48:42 +02:00
```blocks
2016-03-26 00:47:20 +01:00
input.onGesture(Gesture.Shake, () => {
})
```
Next, create an image that contains 3 frames: rock, paper, and scissors. We will control which image is shown with `offset`.
2016-04-13 17:48:42 +02:00
```blocks
2016-03-26 00:47:20 +01:00
input.onGesture(Gesture.Shake, () => {
let img = images.createImage(`
. . . . . # # # # # . . . . #
. # # # . # . . . # # # . # .
. # # # . # . . . # . # # . .
. # # # . # . . . # # # . # .
. . . . . # # # # # . . . . #
`)
})
```
The BBC micro:bit will look like it's showing 1 frame of the image by displaying the whole image with plot frame and math random. We can help the BBC micro:bit randomly decide which offset to using plot image by math random. The BBC micro:bit will randomly pick the image to display with plot image and the `math->random(3)` function.
2016-04-13 17:48:42 +02:00
```blocks
2016-03-26 00:47:20 +01:00
input.onGesture(Gesture.Shake, () => {
let img1 = images.createImage(`
. . . . . # # # # # . . . . #
. # # # . # . . . # # # . # .
. # # # . # . . . # . # # . .
. # # # . # . . . # # # . # .
. . . . . # # # # # . . . . #
`)
img1.plotFrame(Math.random(3))
})
```
### ~avatar avatar
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/rock-paper-scissors/challenges)!
2016-03-26 00:47:20 +01:00
### ~