Skip to content

Instantly share code, notes, and snippets.

@damiensawyer
Created September 30, 2021 04:14
Show Gist options
  • Save damiensawyer/3c6c7817bf9575a62a4b3f2bc986f1ab to your computer and use it in GitHub Desktop.
Save damiensawyer/3c6c7817bf9575a62a4b3f2bc986f1ab to your computer and use it in GitHub Desktop.
MIcrobit Reaction Game With Sound
let initialMode = 0
let countdownMode = 100
let cheatedMode = 200
let playingMode = 300
let finishedMode = 400
let soundDuration = 300
let p1Wins = false
let randomTime = 0
let gameStartTime = 0
let activeMode = initialMode
let p1Score = 0
let p2Score = 0
let winSoundPlayed = false
let gameTime = () => input.runningTime() - gameStartTime
type note = {frequency:number, duration:number}
type rest = {duration:number}
type tune = (note|rest)[]
const isNote = (f: rest | note): f is note => {
return (f as note).frequency !== undefined
}
const gameMusic = {
play:(tune:tune) =>{
tune.forEach(x=>{
if (isNote(x))
music.playTone(x.frequency, x.duration)
else
music.rest(x.duration)
})
},
error: () => { music.playTone(100,100) },
start: () => { music.playTone(1000, 100) },
winner: () => {
if (!winSoundPlayed)
{
let d = 70
gameMusic.play([
{ frequency: 1000, duration: d},
{ frequency: 2000, duration: d },
{ frequency: 3000, duration: d },
{ frequency: 4000, duration: d },
{ frequency: 5000, duration: d },
{ frequency: 1000, duration: d },
{ frequency: 2000, duration: d },
{ frequency: 3000, duration: d },
{ frequency: 4000, duration: d },
{ frequency: 5000, duration: d },
])
winSoundPlayed = true
}
}
}
// Game loop
while (true) {
let p1Pressed = false
let p2Pressed = false
if (input.buttonIsPressed(Button.A)) {
basic.showNumber(p1Score, 0)
} else if (input.buttonIsPressed(Button.B)) {
basic.showNumber(p2Score, 0)
} else {
// Power on
if (activeMode == initialMode) {
basic.showIcon(IconNames.StickFigure, 0)
}
// Start Game
if (input.pinIsPressed(TouchPin.P0)) {
music.stopAllSounds()
gameMusic.start()
winSoundPlayed=false
gameStartTime = input.runningTime()
randomTime = Math.randomRange(1000, 5000)
activeMode = countdownMode
p1Wins = false
}
let ringTime = 100
// Countdown Mode
if (activeMode == countdownMode) {
let gt = gameTime()
if (gt <= 1000) {
basic.showNumber(3, 0)
} else if (gt <= 2000) {
basic.showNumber(2, 0)
} else if (gt <= 3000) {
basic.showNumber(1, 0)
} else if (gt <= 3000 + randomTime) {
basic.clearScreen() }
else
{
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`, 0)
activeMode = playingMode
}
}
p1Pressed = input.pinIsPressed(TouchPin.P1)
p2Pressed = input.pinIsPressed(TouchPin.P2)
if (activeMode == playingMode && (p1Pressed || p2Pressed)) {
music.stopAllSounds()
activeMode = finishedMode
if (p1Pressed) {
p1Score++
p1Wins = true
} else {
p2Score++
}
}
if (activeMode == finishedMode) {
if (p1Wins)
basic.showArrow(ArrowNames.West, 0)
else
basic.showArrow(ArrowNames.East, 0)
gameMusic.winner()
}
// Check for cheats (There is a bug here as once one has cheated it changes to cheat mode. Can't catch both cheats)
if (activeMode == countdownMode && (p1Pressed || p2Pressed)) {
activeMode = cheatedMode
if (p1Pressed && !p2Pressed) // p1 cheated
{
gameMusic.error()
basic.showLeds(`
. . . . .
. . . . .
# . # . .
. # . . .
# . # . .
`, 0)
}
if (!p1Pressed && p2Pressed) {
gameMusic.error()
basic.showLeds(`
. . . . .
. . . . .
. . # . #
. . . # .
. . # . #
`, 0)
}
// if (p1Pressed && p2Pressed) { // both cheated
// basic.showIcon(IconNames.Angry)
// }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment