Skip to content

Instantly share code, notes, and snippets.

@zaru
Last active January 5, 2020 02:08
Show Gist options
  • Save zaru/f169d27d1073d07ad546ae8a1c41a407 to your computer and use it in GitHub Desktop.
Save zaru/f169d27d1073d07ad546ae8a1c41a407 to your computer and use it in GitHub Desktop.
micro:bit ボタン早押しゲーム
let inGame = false
let pushedFlag = 0
let score = 0
let plot = 0
let showScoreLED: Function
input.onButtonPressed(Button.A, function () {
if (!inGame) {
gameStart()
} else if (inGame && pushedFlag == 0) {
scoreUp()
}
})
input.onButtonPressed(Button.B, function () {
if (inGame && pushedFlag == -1) {
scoreUp()
}
})
music.onEvent(MusicEvent.BackgroundMelodyEnded, function () {
if (plot > 23) {
gameClear()
} else {
gameOver()
}
})
function init() {
inGame = false
pushedFlag = 0
score = 0
plot = 0
}
function scoreUp() {
// 他の音と重ねることができない…
// music.ringTone(262)
score += 1
showScoreLED()
pushedFlag = ~pushedFlag
}
function gameOver() {
basic.pause(250)
basic.clearScreen()
basic.showString("GameOver")
inGame = false
}
function gameClear() {
basic.pause(250)
basic.clearScreen()
basic.showString("Clear!")
inGame = false
}
function gameStart() {
init()
music.playTone(523, music.beat(BeatFraction.Quarter))
music.playTone(659, music.beat(BeatFraction.Quarter))
music.playTone(784, music.beat(BeatFraction.Quarter))
inGame = true
basic.showNumber(3)
basic.pause(500)
basic.showNumber(2)
basic.pause(500)
basic.showNumber(1)
basic.pause(500)
basic.showNumber(0)
music.beginMelody(music.builtInMelody(Melodies.Nyan), MelodyOptions.OnceInBackground)
basic.clearScreen()
showScoreLED = makeShowScoreLED()
}
function makeShowScoreLED() {
let index = 2
let thershold = 0
let x = 0
let y = 0
return function () {
if (score > thershold) {
led.plot(x, y)
if (plot > 23) {
music.stopMelody(MelodyStopOptions.All)
}
plot++
x = Math.floor(plot / 5)
y = plot % 5
thershold = Math.round(plot ** index / 36)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment