I have been working on this space invaders code for a long while, and with some help from stack overflow, I accomplished this code that works properly, but I need one more thing for this code to become an working game. I need to use an array for the aliens.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { //the code for the player ship
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
byte Ailen[8] = {//the code for the ailens
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001
};
byte Bullet[8] = {//the code for the bullet
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
};
#define lButton 13 //pin for the left button
#define rButton 6 //pin for the right button
#define fButton 7 //pin for the firing button
int xa = 0;//the aliens x coords
int ya = 0;//the aliens y coords
int xShip=15;//the starting x coord of the ship
int yShip=0;//starting y coord of the ship
int yAliens[2] = {0,1};
int xAliens[12]= {0,1,2,3,4,5,6,7,8,9,10,11}; /* attempted use of the array
which did not work but I will leave it in*/
#define DTIME 150 //what is the delay for movement
void setup() {
pinMode(lButton, INPUT);//sets up left button
pinMode(rButton, INPUT);//sets up right button
pinMode(fButton, INPUT);//sets up firing button
lcd.begin(16, 2);//parameter of the lcd
lcd.createChar(0, Ship);//sets char
lcd.createChar(1, Ailen);//sets char
lcd.createChar(2, Bullet);//sets char
lcd.setCursor(xa,ya);//coords of the one ailen
lcd.write((byte)1);//prints the one ailen
}
void loop() {
static boolean alien = true; //is the one ailen alive?
static boolean fire = false; //is the bullet firing
static byte xb = 0; //the bullets x coords
static byte yb = 0; //the bullets y coords
byte lState = digitalRead(lButton); //pulls the state of the left button
byte rState = digitalRead(rButton); //pulls the state of the right button
byte fState = digitalRead(fButton); //pulls the state of the firing button
/*if the bullet is moving and the alien's position is
* the same as the bullets, then
* */
if (fire && alien && yb == ya && xb == yb) {
fire = false; //bullet stops moving
alien = false; //the alien is dead
lcd.setCursor(xa, ya); //the position of the alien
lcd.print(" "); //becomes blank
tone(9,400,100); //plays kill noise
}
if (fire) { //if the bullet is moving and is on the screen then
lcd.setCursor(xb+1,yb);//clears where bullet was
lcd.print(" ");//clear symbol
if (xb-- < 0) {//move the bullet
/* bullet ouside of the screen */
fire = false;
} else {
lcd.setCursor(xb, yb);
lcd.write((byte)2); //displays bullet
delay(DTIME / 2);//bullet moves twice as fast as ship
}
}
if (fState == HIGH && !fire) { //if the fire button is pressed
fire = true; //starts moving the bullet
/* bullet starts one away from ship */
xb = xShip - 1;
yb = yShip;
}
//if the right button is pressed and the left isnt
if (rState == HIGH && lState == LOW && yShip != 0) {
lcd.setCursor(xShip, yShip); //ship's location
lcd.print(" "); //clearing block
yShip = 0; //make the ship move to the right
lcd.setCursor(xShip, yShip); //ship's location
lcd.write((byte)0); //display the ship byte
delay(DTIME); //time to deter mashing
}
// if the right button is pressed and the right isnt
else if (lState == HIGH && rState == LOW && yShip != 1) {
lcd.setCursor(xShip, yShip); //ship's location
lcd.print(" "); //clearing block
yShip = 1; //make the ship move to the right
lcd.setCursor(xShip, yShip); //ship's location
lcd.write((byte)0); //display the ship byte
delay(DTIME); //time to deter mashing
}
//if nothing is pressed
else{
lcd.setCursor(xShip, yShip);//set previous coordinates for the ship*good practice
lcd.write((byte)0);//display the ship
}
}
I was thinking that the arrays would look something like this, but I do not know the syntax to accomplish these functions.
int yAliens[2] = {0,1};
int xAliens[12]= {0,1,2,3,4,5,6,7,8,9,10,11};
//this should compare all of the intergers of the array to the bullets coords
if (fire && alien && yb == yAliens[] && xb == xAliens[]) {
fire = false; //bullet stops moving
alien = false; //the alien is dead
//here I need to find the numbers of the array that are true with the function
lcd.setCursor(yAliens[], xAliens[]; //this is where they would go
lcd.print(" "); //becomes blank
tone(9,400,100); //plays kill noise