pxt-calliope/docs/lessons/spinner/challenges.md
Sam El-Husseini e3975e65e5 pxt-microbit Accessibility PR (#529)
* Accessibility changes
2017-09-07 13:42:08 -07:00

3.2 KiB

spinner challenges

Create an arrow that randomly points to a player.

Before we get started

Complete the following guided tutorial, your code should look like this:

input.onGesture(Gesture.Shake, () => {
    let randomArrow = Math.random(4)
    if (randomArrow == 3) {
        basic.showLeds(`
            . . # . .
            . # # # .
            # # # # #
            . . # . .
            . . # . .
            `)
    }
    if (randomArrow == 2) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # # # # #
            . # # # .
            . . # . .
            `)
    }
    if (randomArrow == 1) {
        basic.showLeds(`
            . . # . .
            . # # . .
            # # # # #
            . # # . .
            . . # . .
            `)

    }
})

Challenge 1

Modify the random number generator so that it can include new arrows we will create in the next challenge.

input.onGesture(Gesture.Shake, () => {
    let randomArrow = Math.random(8)
    if (randomArrow == 3) {
        basic.showLeds(`
            . . # . .
            . # # # .
            # # # # #
            . . # . .
            . . # . .
            `)
    }
    if (randomArrow == 2) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # # # # #
            . # # # .
            . . # . .
            `)
    }
    if (randomArrow == 1) {
        basic.showLeds(`
            . . # . .
            . # # . .
            # # # # #
            . # # . .
            . . # . .
            `)

    }
})
  • Do not run the code yet because it will not work until you have conditions for every random number.

Challenge 2

Let's add more arrows that point diagonally.

input.onGesture(Gesture.Shake, () => {
    let randomArrow = Math.random(8)
    if (randomArrow == 7) {
        basic.showLeds(`
            . . # . .
            . # # # .
            # # # # #
            . . # . .
            . . # . .
            `)
    }
    if (randomArrow == 6) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # # # # #
            . # # # .
            . . # . .
            `)
    }
    if (randomArrow == 5) {
        basic.showLeds(`
            . . # . .
            . # # . .
            # # # # #
            . # # . .
            . . # . .
            `)

    }
    if (randomArrow == 4) {
        basic.showLeds(`
            . . # . .
            . . . # .
            # # # # #
            . . . # .
            . . # . .
            `)

    }

    if (randomArrow == 3) {
        basic.showLeds(`
            # # # # #
            # # # # .
            # # # # .
            # . . # .
            . . . . #
            `)

    }
    if (randomArrow == 2) {
        basic.showLeds(`
            # # # # #
            # # # # #
            . . # # #
            . # . # #
            # . . . #
            `)

    }
        if (randomArrow == 1) {
        basic.showLeds(`
            # . . . #
            # # . # .
            # # # . .
            # # # # .
            # # # # #
            `)

    }

})

  • Run your code to see if it works as expected

Challenge 3

Add some other arrows if there are more than 8 players.