Skip to content

Instantly share code, notes, and snippets.

@damiensawyer
Created December 7, 2019 02:52
Show Gist options
  • Save damiensawyer/b6c2df351abc11f8f907e49ed9b2a373 to your computer and use it in GitHub Desktop.
Save damiensawyer/b6c2df351abc11f8f907e49ed9b2a373 to your computer and use it in GitHub Desktop.
Microbit Rection Game Code
let initialMode = 0
let countdownMode = 100
let cheatedMode = 200
let playingMode = 300
let finishedMode = 400
let p1Wins = false
let randomTime = 0
let gameStartTime = 0
let activeMode = initialMode
let p1Score = 0
let p2Score = 0
let gameTime = () => input.runningTime() - gameStartTime
// 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.Happy, 0)
}
// Start Game
if (input.pinIsPressed(TouchPin.P0)) {
gameStartTime = input.runningTime()
randomTime = Math.randomRange(1000, 5000)
activeMode = countdownMode
p1Wins = false
}
// 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)) {
activeMode = finishedMode
if (p1Pressed) {
p1Score++
p1Wins = true
} else {
basic.showArrow(ArrowNames.East)
p2Score++
}
}
if (activeMode == finishedMode) {
if (p1Wins)
basic.showArrow(ArrowNames.West, 0)
else
basic.showArrow(ArrowNames.East, 0)
}
// 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
{
basic.showLeds(`
. . . . .
. . . . .
# . # . .
. # . . .
# . # . .
`, 0)
}
if (!p1Pressed && p2Pressed) {
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