bundle.262984c7f73585c42a474864f81087869bba6a968adbd0ec8865ede9fee55627c3136a2a0e4dcc864224c3763797d0a15e83047fe9e1c5bcafbafd8657d1e757

Micro:bit Shooting Game with Sound and Light Effects

Overview

Warning
  1. This program only supports Micro:bit v2 or higher.

Demo

Code

Library

  1. Because I use Expansion Board, please add Neopixel extension in your micro:bit editor. (https://makecode.microbit.org/pkg/microsoft/pxt-neopixel)

Block code

  • This is the block code, you can press edit to see the source code.

Python code

  • If you prefer Python, the following is the code. (I use block only to develop this.)

     1def on_button_pressed_a():
     2    monkey.change(LedSpriteProperty.X, -1)
     3input.on_button_pressed(Button.A, on_button_pressed_a)
     4
     5def on_button_pressed_ab():
     6    global bullet, banana
     7    music.play_sound_effect(music.builtin_sound_effect(soundExpression.happy),
     8        SoundExpressionPlayMode.IN_BACKGROUND)
     9    bullet = game.create_sprite(monkey.get(LedSpriteProperty.X),
    10        monkey.get(LedSpriteProperty.Y))
    11    bullet.turn(Direction.LEFT, 90)
    12    for index in range(4):
    13        bullet.move(1)
    14        if bullet.is_touching(banana):
    15            bullet.delete()
    16            banana.delete()
    17            game.add_score(1)
    18            music.play_sound_effect(music.builtin_sound_effect(soundExpression.giggle),
    19                SoundExpressionPlayMode.IN_BACKGROUND)
    20            strip.show_rainbow(1, 360)
    21            for index2 in range(30):
    22                strip.show()
    23                strip.rotate(1)
    24                basic.pause(100)
    25            basic.pause(1000)
    26            strip.show_color(neopixel.colors(NeoPixelColors.BLACK))
    27            strip.clear()
    28            banana = game.create_sprite(randint(0, 3), 0)
    29        else:
    30            basic.pause(200)
    31    bullet.delete()
    32input.on_button_pressed(Button.AB, on_button_pressed_ab)
    33
    34def on_button_pressed_b():
    35    monkey.change(LedSpriteProperty.X, 1)
    36input.on_button_pressed(Button.B, on_button_pressed_b)
    37
    38bullet: game.LedSprite = None
    39monkey: game.LedSprite = None
    40banana: game.LedSprite = None
    41strip: neopixel.Strip = None
    42strip = neopixel.create(DigitalPin.P2, 24, NeoPixelMode.RGB)
    43strip.set_brightness(50)
    44banana = game.create_sprite(randint(0, 4), 0)
    45monkey = game.create_sprite(2, 4)
    46game.set_score(0)
    47game.start_countdown(50000)
    48
    49def on_forever():
    50    basic.pause(randint(500, 5000))
    51    banana.set(LedSpriteProperty.X, randint(0, 4))
    52    banana.set(LedSpriteProperty.Y, randint(0, 2))
    53basic.forever(on_forever)
    

參考

Posts in this Series

    bundle.262984c7f73585c42a474864f81087869bba6a968adbd0ec8865ede9fee55627c3136a2a0e4dcc864224c3763797d0a15e83047fe9e1c5bcafbafd8657d1e757