Skip to content

Instantly share code, notes, and snippets.

@unwiredben
Created November 28, 2019 01:47
Show Gist options
  • Save unwiredben/9869bceae129f09d210a0a61d1e35f80 to your computer and use it in GitHub Desktop.
Save unwiredben/9869bceae129f09d210a0a61d1e35f80 to your computer and use it in GitHub Desktop.
Simple CircuitPlayground classic game where you try to stop the LED at the right time
// a simple CircuitPlayground game by Ben Combee (age 45) and Elias Combee (age 6)
#include <Adafruit_CircuitPlayground.h>
int i = 0;
int direction = 1;
int speed = 80;
void setup() {
CircuitPlayground.begin();
}
void win() {
for (int j = 0; j < 2; j = j + 1) {
for (int i = 0; i < 10; i = i + 1) {
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(i * 25));
delay(30);
CircuitPlayground.setPixelColor(i, 0);
}
}
}
void lose() {
for (int i = 0; i < 10; i = i + 1) {
CircuitPlayground.setPixelColor(i, 255, 0, 0);
}
delay(500);
CircuitPlayground.clearPixels();
}
boolean game() {
i = 0;
direction = 1;
while (1) {
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(i * 25));
delay(speed);
if (CircuitPlayground.leftButton()) {
while (CircuitPlayground.leftButton()) { delay(1); }
if (i == 7)
return true;
else
return false;
}
CircuitPlayground.setPixelColor(i, 0);
i = i + direction;
if (i < 0) i = 9;
if (i > 9) i = 0;
}
}
void loop() {
if (game() == true) {
win();
if (speed > 20)
speed = speed - 10;
}
else {
lose();
if (speed < 120)
speed = speed + 10;
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment