Skip to content

Instantly share code, notes, and snippets.

@Sigafoos
Last active August 10, 2017 15: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 Sigafoos/47b6c634b6773ac4fd40cda49905c33a to your computer and use it in GitHub Desktop.
Save Sigafoos/47b6c634b6773ac4fd40cda49905c33a to your computer and use it in GitHub Desktop.
A game where you have to keep a Circuit Playground level for as long as possible. It keeps track of your "score" (2 points per second) and displays it at the end of the game.
// variables
let soundsOn = true // false if you don't want any sounds
let modifier = 100 // lower == harder
// the game flow
light.pixels.clear()
startUp()
let rounds = playGame(modifier)
serial.writeLine("rounds: " + rounds.toString())
endGame(soundsOn)
showScore(rounds, soundsOn)
function startUp() {
// initial light ring
for (let i = 0; i <= 10 - 1; i++) {
light.pixels.setPixelColor(i, Colors.Green)
loops.pause(200)
light.pixels.show()
}
loops.pause(200)
light.pixels.clear()
loops.pause(100)
light.pixels.setAll(Colors.Green)
light.pixels.show()
loops.pause(100)
light.pixels.clear()
loops.pause(100)
light.pixels.setAll(Colors.Green)
light.pixels.show()
}
function playGame(modifier: number) {
let tilt = 0
// @TODO: read the switch value, for easy/hard mode
let rounds = 0
while (true) {
tilt += Math.abs(input.rotation(Rotation.Pitch) + input.rotation(Rotation.Roll))
for (let i = 1; i <= 10; i++) {
if (tilt > modifier * i) {
light.pixels.setPixelColor(i - 1, Colors.Red)
} else if (tilt > (modifier * i) - (modifier / 2)) {
light.pixels.setPixelColor(i - 1, Colors.Yellow)
}
}
serial.writeLine("tilt: " + tilt.toString())
light.pixels.show()
if (tilt > modifier * 10) {
return rounds
}
rounds++
loops.pause(500)
}
}
// the ending animation and sad trombone sound
function endGame(sounds: boolean) {
for (let i = 9; i >= 0; i--) {
light.pixels.setPixelColor(i, Colors.Black)
loops.pause(75)
}
if (sounds) {
music.playTone(329.63, 600)
music.playTone(293.66, 400)
music.playTone(261.63, 800)
loops.pause(500)
}
}
// each round is 0.5 seconds
// max score is 1 minute (120 rounds)
// based on playtesting, this might need to change?
function showScore(rounds: number, sounds: boolean) {
let maxRounds = 120 // make higher if you routinely get a kill screen
let score = rounds / maxRounds * 10
let green = Math.floor(score)
let yellow = (score - green) >= 0.5 // if you got one final yellow
for (let i = 0; i < green; i++) {
if (sounds) music.playTone(261 + (i * 20), 100)
light.pixels.setPixelColor(i, Colors.Yellow)
loops.pause(500)
if (sounds) music.playTone(271 + (i * 20), 100)
light.pixels.setPixelColor(i, Colors.Green)
loops.pause(500)
}
if (yellow) {
// because of 0-indexing, green is actually the pixel after the highest green
if (sounds) music.playTone(261 + (green * 20), 100)
light.pixels.setPixelColor(green, Colors.Yellow)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment