You are on page 1of 4

#include <LiquidCrystal.

h>
//opens up the LCD library
#define BUTTON_PIN 13
//sets the pin for the button
#define MAX_ALIENS 7
//sets the maximum numbers of meteors/aliens in the array
int buttonState = 0;
//creates the buttonState variable
int iAlien1 [MAX_ALIENS] = {22,29,35,40,44,47,52};
int iAlien2 [MAX_ALIENS] = {25,31,33,38,42,49,54};
//creats two arrays for the locations of each row of meteors/aliens
int icounter = 0;
//creates a varibale that will tell us whether to print a meteor or the character
int charLocation = 0;
//creates variable to determine character location
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//what pins the LCD uses
byte character[8] = {
B00000,
B00110,
B10100,
B01111,
B01111,
B10100,
B00110,
B00000
};
byte meteor[8] = {
B00000,
B00100,
B01110,
B11001,
B10111,
B01010,
B00100,
B00000
};
byte Crash[8] = {
B00100,
B10101,
B01110,
B01110,
B10101,
B00100,
B00000,

B00000
};
//sets up three custom charcters(meteor/charcter/crash)
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
//tells the arduino that we are using a 16 by 2 LCD
lcd.createChar(0, character);
lcd.createChar(1, meteor);
lcd.createChar(2, Crash);
//creates three custom charcters
pinMode(BUTTON_PIN, INPUT);
//says button is an input
}
void loop() {
//loop code to continuously run
buttonState = digitalRead(BUTTON_PIN);
//reads the buttonstate each time to see if it is HIGH(on) or LOW(off)
delay(150);
//waits so our program doesn't run too quickly
lcd.clear();
//clears screen so previous loop is gone
icounter++;
//adds one to our counter variable
/*
The counter variable is how we determine whether to print the character or the meteors.
Since we can not print them both at the same time at different parts of the screen,
we print the character when we first go throught the loop and then the next time through
the loop,
we print the meteors. This is done by adding one to icounter each time and using the
modulo(%) which takes
the remainder of icounter when divided by two. If there is no remainder(icounter%2 ==
0), then icounter is even,
and we tell it to print the character. The else statement(when the icounter would be odd),
will print the meteors.
That is why we see the meteors and characters flashing, because they are actually
being printed at different times.
*/
if ((icounter%2) == 0){
//if icounter is even, print the character
if(buttonState ==HIGH){
charLocation = 1;
lcd.setCursor(0, charLocation);
lcd.write(byte(0));
//if the button is pressed, print the character at (0,1)

}
else{
charLocation = 0;
lcd.setCursor(0, charLocation);
lcd.write(byte(0));
//if the button isn't pressed, print the character at (0,0)
}
}
else {
//if icounter is odd, print meteors
for(int k = 0; k < MAX_ALIENS; k++)
//make a variable(k) that accesses a certain spot in the array
{
iAlien1[k] = iAlien1 [k] -1;
iAlien2[k] = iAlien2 [k] -1;
//subtract 1 from the value in the array, this will make it move one space to the left, and
it will "fall" towards our character
if(iAlien1[k] <= 15){
lcd.setCursor(iAlien1[k] , 0);
lcd.write(byte(1));
if(iAlien1[k] < 0) {
iAlien1[k] = 55;
//prints the meteors in the first row if they should appear on screen(value less than
15)
}
}
if(iAlien2[k] <= 15){
lcd.setCursor(iAlien2[k] , 1);
lcd.write(byte(1));
if(iAlien2[k] <0) {
iAlien2[k] = 55;
//prints the meteors in the second row
}
}
/*
Both sets of code below are for gameover. The first is a condition for when the first row
of meteors collide with the player.
The second is a condition for when the second row of meteors collide with the player.
After finding out if they collide, both codes do the same thing,
but they print the explosion/crash where the player last was.
The screen is cleared, and the explosion is drawn where the player was. The screen is
cleared again and "Game Over" appears.
Then your score appears, which is measured in seconds that you survived for.
Tat is cleared and then "Press Reset to Continue is printed." When you press reset, you
restart the program,
and all of the meteors reset alomg with your score.

*/
if(iAlien1[k] == 0 && charLocation == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.write(byte(2));
delay(25);
lcd.clear();
lcd.setCursor(3,0);
lcd.print(" Game Over ");
delay(2000);
lcd.clear();
lcd.print("Score:");
lcd.setCursor(10,0);
lcd.print( millis() /1000);
delay(8000);
lcd.clear();
lcd.print(" Press Reset to");
lcd.setCursor(3,1);
lcd.print( " Continue ");
delay(1000000);
lcd.clear();
}
if(iAlien2[k] == 0 && charLocation == 1){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(byte(2));
delay(25);
lcd.clear();
lcd.setCursor(3,0);
lcd.print(" Game Over ");
delay(2000);
lcd.clear();
lcd.print("Score:");
lcd.setCursor(10,0);
lcd.print( millis() /1000);
delay(3000);
lcd.clear();
lcd.print(" Press Reset to");
lcd.setCursor(3,1);
lcd.print( " Continue ");
delay(1000000);
lcd.clear();
}
}
}
}

You might also like