pxt-calliope/docs/tutorials/hour-of-code/rock-paper-scissors.md
Amerlander 918af4f3ac
Bump V3.0.22 (#110)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

3.4 KiB

Rock Paper Scissors

Step 1

We want the @boardname@ to choose rock, paper, or scissors when you shake it. Place a ||input:on shake|| block so when you shake the @boardname@, it will run part of a program.

input.onGesture(Gesture.Shake, () => {

})

Step 2

Add a tool variable to store a random number computed with ||math:pick random||.

When you shake the @boardname@, it should pick a random number from 0 to 2 and store it in the variable tool. (This variable is named tool because rock, paper, and scissors are the tools you use to challenge your friends!)

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    tool = randint(0, 3)
})

In a later step, each of the possible numbers (0, 1, or 2) is matched to its own picture. The picture is shown on the LEDs when its number is picked.

Step 3

Place an if block under the ||math:pick random|| and check whether tool is equal to 0.

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    let tool = randint(0, 3)
    if (tool == 0) {
    }
})

Step 4

In the if block, place a ||basic:show leds|| block that shows a picture of a piece of paper.

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    let tool = randint(0, 3)
    if (tool == 0) {
        basic.showLeds(`
            # # # # #
            # . . . #
            # . . . #
            # . . . #
            # # # # #
            `)
    }
})

Step 5

Add an else if block to the if block and check whether tool is equal to 1.

Click on the (+) at the bottom of the if block to add an else if section.

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    let tool = randint(0, 3)
    if (tool == 0) {
        basic.showLeds(`
            # # # # #
            # . . . #
            # . . . #
            # . . . #
            # # # # #
            `)
    } else if (tool == 1) {
    }
})

Step 6

Place a ||basic:show leds|| block under the else if and draw a rock image on the screen.

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    let tool = randint(0, 3)
    if (tool == 0) {
        basic.showLeds(`
            # # # # #
            # . . . #
            # . . . #
            # . . . #
            # # # # #
            `)
    } else if (tool == 1) {
        basic.showLeds(`
            . . . . .
            . # # # .
            . # # # .
            . # # # .
            . . . . .
            `)
    }
})

Step 7

Add a ||basic:show leds|| block with a picture of scissors to the else part.

You don't need to check if tool 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.

let tool = 0;
input.onGesture(Gesture.Shake, () => {
    let tool = randint(0, 3)
    if (tool == 0) {
        basic.showLeds(`
            # # # # #
            # . . . #
            # . . . #
            # . . . #
            # # # # #
            `)
    } else if (tool == 1) {
        basic.showLeds(`
            . . . . .
            . # # # .
            . # # # .
            . # # # .
            . . . . .
            `)
    } else {
        basic.showLeds(`
            # # . . #
            # # . # .
            . . # . .
            # # . # .
            # # . . #
            `)
    }
})

Step 8

Your game is ready! Gather your friends and play Rock Paper Scissors!