micro:bit – Hledání pokladu

Popis hry

  • Pomocí náklonu (popřípadě předdefinovaných tlačítek) ovládáme pohyb rozsvícené diody
  • Někam náhodně umístíme skrytý poklad
  • Poklad hledáme a při nalezení spustíme nějakou akci
    • Zobrazení ikony
    • Zahrání melodie

Připomenutí pojmů

  • Proměnné
    • x, y představují souřadnice rozsvícené diody
    • px, py …
  • Funkce
    • Schovej poklad – nastaví souřadnice pokladu (náhodně)
  • Větvení (podmínky)
  • Cykly

Samotný program jako bloky

Kód v Pythonu

def schovejPoklad():

    global Pokladx, Poklady

    Pokladx = randint(0, 4)

    Poklady = randint(0, 4)

    while Pokladx == 2 and Poklady == 2:

        Pokladx = randint(0, 4)

        Poklady = randint(0, 4)

def on_button_pressed_a():

    global x

    led.unplot(x, y)

    x += -1

    if x < 0:

        x = 4

input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_ab():

    global y

    led.unplot(x, y)

    y += 1

    if y > 4:

        y = 0

input.on_button_pressed(Button.AB, on_button_pressed_ab)

def on_button_pressed_b():

    global x

    led.unplot(x, y)

    x += 1

    if x > 4:

        x = 0

input.on_button_pressed(Button.B, on_button_pressed_b)

def on_logo_pressed():

    global y

    led.unplot(x, y)

    y += -1

    if y < 0:

        y = 4

input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

Poklady = 0

Pokladx = 0

y = 0

x = 0

x = 2

y = 2

schovejPoklad()

def on_forever():

    global y, x

    led.plot(x, y)

    if input.rotation(Rotation.PITCH) > 20:

        led.unplot(x, y)

        y += 1

        if y > 4:

            y = 0

        led.plot(x, y)

        basic.pause(200)

    if input.rotation(Rotation.PITCH) < -20:

        led.unplot(x, y)

        y += -1

        if y < 0:

            y = 4

        led.plot(x, y)

        basic.pause(200)

    if input.rotation(Rotation.ROLL) > 20:

        led.unplot(x, y)

        x += 1

        if x > 4:

            x = 0

        led.plot(x, y)

        basic.pause(200)

    if input.rotation(Rotation.ROLL) < -20:

        led.unplot(x, y)

        x += -1

        if x < 0:

            x = 4

        led.plot(x, y)

        basic.pause(200)

    if x == Pokladx and y == Poklady:

        basic.show_leds(„““

            . # . # .

                        . # . # .

                        # . . . #

                        . # # # .

                        . . # . .

        „““)

        for i in range(3):

            music.set_volume((i + 1) * 85)

            music.play_melody(„D E F G A B B B „, (i + 1) * 200)

        basic.pause(2000)

        control.reset()

basic.forever(on_forever)

Kód v JavaScriptu

function schovejPoklad () {

    Pokladx = randint(0, 4)

    Poklady = randint(0, 4)

    while (Pokladx == 2 && Poklady == 2) {

        Pokladx = randint(0, 4)

        Poklady = randint(0, 4)

    }

}

input.onButtonPressed(Button.A, function () {

    led.unplot(x, y)

    x += -1

    if (x < 0) {

        x = 4

    }

})

input.onButtonPressed(Button.AB, function () {

    led.unplot(x, y)

    y += 1

    if (y > 4) {

        y = 0

    }

})

input.onButtonPressed(Button.B, function () {

    led.unplot(x, y)

    x += 1

    if (x > 4) {

        x = 0

    }

})

input.onLogoEvent(TouchButtonEvent.Pressed, function () {

    led.unplot(x, y)

    y += -1

    if (y < 0) {

        y = 4

    }

})

let Poklady = 0

let Pokladx = 0

let y = 0

let x = 0

x = 2

y = 2

schovejPoklad()

basic.forever(function () {

    led.plot(x, y)

    if (input.rotation(Rotation.Pitch) > 20) {

        led.unplot(x, y)

        y += 1

        if (y > 4) {

            y = 0

        }

        led.plot(x, y)

        basic.pause(200)

    }

    if (input.rotation(Rotation.Pitch) < -20) {

        led.unplot(x, y)

        y += -1

        if (y < 0) {

            y = 4

        }

        led.plot(x, y)

        basic.pause(200)

    }

    if (input.rotation(Rotation.Roll) > 20) {

        led.unplot(x, y)

        x += 1

        if (x > 4) {

            x = 0

        }

        led.plot(x, y)

        basic.pause(200)

    }

    if (input.rotation(Rotation.Roll) < -20) {

        led.unplot(x, y)

        x += -1

        if (x < 0) {

            x = 4

        }

        led.plot(x, y)

        basic.pause(200)

    }

    if (x == Pokladx && y == Poklady) {

        basic.showLeds(`

            . # . # .

            . # . # .

            # . . . #

            . # # # .

            . . # . .

            `)

        for (let i = 0; i <= 2; i++) {

            music.setVolume((i + 1) * 85)

            music.playMelody(„D E F G A B B B „, (i + 1) * 200)

        }

        basic.pause(2000)

        control.reset()

    }

})