Skip to content

Instantly share code, notes, and snippets.

@nbogie
Last active October 30, 2017 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbogie/0f209b5d61a3f61412d730a3be414407 to your computer and use it in GitHub Desktop.
Save nbogie/0f209b5d61a3f61412d730a3be414407 to your computer and use it in GitHub Desktop.
micro:bit reaction game
let startTime = 0
let scoring = false
let playing = false
let winTime = 0
input.onPinPressed(TouchPin.P0, () => {
startRound()
})
function handleWin() {
winTime = input.runningTime() - startTime
basic.showNumber(winTime)
radio.sendNumber(winTime)
}
function foul() {
radio.sendNumber(-1)
basic.showIcon(IconNames.Angry)
}
function startRound() {
playing = false
scoring = false
basic.showNumber(3)
basic.showNumber(2)
basic.showNumber(1)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
basic.pause(Math.random(4001))
playing = true
startTime = input.runningTime()
led.plot(Math.random(5), Math.random(5))
}
input.onPinPressed(TouchPin.P1, () => {
if (playing) {
scoring = true
playing = false
basic.showLeds(`
. . # . .
. # . . .
# . . . .
. # . . .
. . # . .
`)
handleWin()
} else if (scoring) {
} else {
foul()
basic.showLeds(`
. . . . .
# . # . .
. # . . .
# . # . .
. . . . .
`)
}
})
input.onPinPressed(TouchPin.P2, () => {
if (playing) {
scoring = true
playing = false
basic.showLeds(`
. . # . .
. . . # .
. . . . #
. . . # .
. . # . .
`)
handleWin()
} else if (scoring) {
} else {
basic.showLeds(`
. . . . .
. . # . #
. . . # .
. . # . #
. . . . .
`)
}
})
radio.setGroup(44)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment