Micro:bit 射擊遊戲 (有聲光效果)
文章目錄
注意
- 這個程式只支援 Micro:bit v2 或更高版.
展示
程式
Library
- 因為我有使用 Expansion Board, 因此請在 micro:bit editor 新增 Neopixel 這個擴充套件 (https://makecode.microbit.org/pkg/microsoft/pxt-neopixel)
Block code
-
這是積木版的程式, 你可以按下 編輯 檢視原始程式.
Python code
-
以下是 Python 程式碼 (我是用 block code 寫這個小遊戲)
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)