Moses kim
Published © GPL3+

Aim and Shoot

Short "bow" game on an adafruit playground express board

BeginnerShowcase (no instructions)43
Aim and Shoot

Things used in this project

Hardware components

Circuit Playground Express
Adafruit Circuit Playground Express
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Alligator Clips
Alligator Clips
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Potentiometer Game

Arduino
Plug and play
#include <Adafruit_CircuitPlayground.h>

int colors[10];
int blinkOrder[] = {2000,1000,1000,1000,500,500,500};
int enemy;
int timer[6];
int blinkCount;
int ticks;
int hp;
int self = 4;
int hpColor[] = {0xFF0000,0xFF9900,0xFFFF00,0x99FF00,0x00FF00};
int knobValue = 0;
int endPixel = -1;
int prevKnobValue = 0;
bool win = false;
bool cd = true;
float diff = .05;
float currDiff;
int score;

void setup() {
  // put your setup code here, to run once:
  CircuitPlayground.begin();
  score = 0;
  hp = 5;
  ticks = 0;
  win = false;
  cd = true;
  currDiff = 0;
  knobValue = 0;
  prevKnobValue = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  while(hp>0) {
    enemy = random(4);
    colors[enemy] = 0xFF0000;
    CircuitPlayground.setPixelColor(enemy, colors[enemy]);
    timer[enemy] = ticks+2000/(1+currDiff);
    colors[self] = hpColor[hp-1];
    CircuitPlayground.setPixelColor(self, colors[self]);
    while(enemy>=0) {
      knobValue = analogRead(A1);
      if(knobValue == 0) {
        cd = false;
        endPixel = 5;
      }
      for(int x = 0; x < 10; x++) {
        if(x<6 && ticks>timer[x]) {
          if(x==5 && !cd) {
            prevKnobValue = knobValue;
            timer[x] += 100;
          }
          //CircuitPlayground.setPixelColor(x, colors[x]);
          if(x == enemy) {
            if(colors[x] == 0x000000) {
              colors[x] = hpColor[0];
              CircuitPlayground.setPixelColor(x, hpColor[0]);
              if(blinkCount<7) {
                timer[x]+= blinkOrder[blinkCount]/(1+currDiff);
              } else {
                CircuitPlayground.setPixelColor(x, 0x000000);
                enemy = -1;
              }
              blinkCount++;
            } else {
              colors[x] = 0x000000;
              CircuitPlayground.setPixelColor(x, colors[x]);
              timer[x]+= 100;
            }
          } else if(x == 4) {
            if(hpColor[hp-1] != colors[x]) {
              colors[x] = hpColor[hp-1];
              CircuitPlayground.setPixelColor(x, colors[x]);
            }
          }
        } else if(x>5) {
          if(prevKnobValue > knobValue+50) {
            cd = true;
            if(enemy == 4-(int)(prevKnobValue/250) || (enemy == 0 && prevKnobValue > 900)) {
              CircuitPlayground.playTone(1000, 100);
              prevKnobValue = 0;
              CircuitPlayground.setPixelColor(enemy, 0x000000);
              enemy = -1;
              currDiff += diff;
              Serial.println(currDiff);
              win = true;
              score++;
            }
          } else {
            if(x<6+knobValue/250) {
              if(cd) {
                colors[x] = 0xFFFF00;
                CircuitPlayground.setPixelColor(x, colors[x]);
                endPixel = x;
              } else if(colors[x] != 0xFFFFFF) {
                colors[x] = 0xFFFFFF;
                CircuitPlayground.setPixelColor(x, colors[x]);
                endPixel = x;
              }
            } else if(colors[x] != 0x000000) {
                colors[x] = 0x000000;
                CircuitPlayground.setPixelColor(x, colors[x]);
            }
          }
        }
      }
      ticks++;
    }
    if(!win) {
      hp--;
      CircuitPlayground.playTone(500, 100);
    }
    win = false;
    blinkCount = 0;
  }
  CircuitPlayground.playTone(2000, 100);
  for(int x = 0; x < (score/10.0)-1; x++) {
    CircuitPlayground.setPixelColor(x, 0xFFD700);
  }
  bool button1, button2 = false;
  while(1) {
    button1 = CircuitPlayground.rightButton();
    button2 = CircuitPlayground.leftButton();
    if (button1 || button2) {
      for(int x = 0; x <= 9; x++) {
        CircuitPlayground.setPixelColor(x, 0x000000);
      }
      break;
    }
  }
  setup();
}

Credits

Moses kim
4 projects • 0 followers

Comments