#include <LiquidCrystal.h>
#include <Wire.h>
#include <ArduinoNunchuk.h>
ArduinoNunchuk nunchuk = ArduinoNunchuk();
LiquidCrystal lcd(2,3,4,5,6,7);
int posX = 90;
int x = 0;
int x2 = 0;
int prevx = 0;
int prevx2 = 0;
int prevPosX = 0;
int zState = 0;
int zLast = 0;
unsigned long previousMillis = 0;
unsigned long startMillis = 0;
unsigned long time = 0;
unsigned long shotTime = 0;
long interval = 500;
long shotDelay = 1000;
int shot = 9;
int i = 0;
int j = 0;
int score = 0;
const int buzzer = 9;
byte enemy[8] = {
B01110,
B10001,
B10101,
B10001,
B10101,
B11011,
B10101,
};
byte gun[8] = {
B00000,
B00000,
B00100,
B01110,
B01010,
B01110,
B11111,
};
byte full[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
byte empty[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
};
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
nunchuk.init();
lcd.createChar(5, enemy);
lcd.createChar(1, gun);
lcd.createChar(2, full);
lcd.createChar(3, empty);
}
void loop(){
nunchuk.update();
unsigned long currentMillis = millis();
x = constrain(x, 0, 14);
x2 = constrain(x2, 0, 14);
lcd.setCursor(x,0);
lcd.write(5);
lcd.setCursor(x2,1);
lcd.write(1);
lcd.setCursor(15, 0);
lcd.print(score);
lcd.setCursor(15, 1);
lcd.print(shot);
zState = nunchuk.zButton;
posX = nunchuk.analogX;
if(zState == HIGH && x == x2 && currentMillis < shotTime + shotDelay){
lcd.setCursor(x,0);
lcd.write(2);
tone(buzzer, 100, 90);
delay(333);
score++;
x = random(0,15);
}
if(posX < 60 && prevPosX > 60){
x2--;
}
if(posX > 200 && prevPosX < 200){
x2++;
}
if(currentMillis - previousMillis > interval){
i = random(0,2);
if(i == 0){
x--;
}
if(i == 1){
x++;
}
previousMillis = currentMillis;
}
if(zState != zLast && zState == HIGH){
shotTime = currentMillis;
tone(buzzer, 1319, 50);
if(x != x2){
shot--;
}
}
zLast = zState;
if(shot == 0){
shot = 9;
score--;
}
if(score == 9){
time = currentMillis - startMillis;
lcd.setCursor(0,0);
lcd.print(" GAME OVER ");
lcd.setCursor(2,1);
lcd.print(time / 1000);
lcd.print(" Seconds ");
delay(5000);
score = 0;
shot = 9;
startMillis = millis();
}
if(score < 0){
lcd.setCursor(0,0);
lcd.print(" GAME OVER ");
lcd.setCursor(0,1);
lcd.print(" YOU LOSE! ");
delay(5000);
score = 0;
shot = 9;
startMillis = millis();
}
if(x != prevx || x2 != prevx2){
lcd.clear();
}
prevx = x;
prevPosX = posX;
prevx2 = x2;
}