LCD Space Invaders[Not Solved]

I have posted an article in the Project Guidance section of the forum, and now that I have begun to have worked on the project, I need to post in this section of the forum. The first code is an section of the second code. f State is whether or not the fire button has been pressed. At the beginning of the loop, as recommended by the Project guidance, I used an array to store the variables of the space invaders. But now as I am attempting to shoot at the “aliens”, I have found that I do not know how to use the array to fire at the aliens. Also, in this code there are 4 movement buttons, one goes up, and the other, down. Since I put the invaders with an array, I can not move “up” the lcd towards the aliens. Thanks- Ben :wink:

else if(fState==HIGH){
  lcd.setCursor(xShip-1,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-1,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-2,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-2,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-3,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-3,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-4,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-4,yShip);
  lcd.print(" ");
#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { 
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
byte Ailen[8] = {
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001 
};
byte Bullet[8] = {
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
};
int time = 0;
int uButton = 14;
int dButton =8;
int lButton = 13;
int rButton = 6;
int fButton = 7;
int lState = 0;
int rState = 0;
int uState = 0;
int dState = 0;
int fState = 0;
int x = 0;
int y = 0;
int xb = 0;
int yb = 0;
int xShip=15;
int yShip=0;
int xAilens[2] = {0,1};
int yAilens[12]= {0,1,2,3,4,5,6,7,8,9,10,11};
boolean lTrue= false;
boolean rTrue = false;
const int dTime= 150;
void setup() { 
pinMode(uButton, INPUT);
pinMode(dButton, INPUT);
pinMode(lButton, INPUT);
pinMode(rButton, INPUT); 
lcd.begin(16, 2);
lcd.createChar(0, Ship);
lcd.createChar(1,Ailen);
lcd.createChar(2,Bullet);

}

void loop() {
xb = xShip;
yb = yShip;
lState=digitalRead(lButton);
rState=digitalRead(rButton);
fState=digitalRead(fButton); 
uState=digitalRead(uButton);
dState=digitalRead(dButton);
lcd.setCursor(yAilens[y],xAilens[x]);
lcd.write(byte(1));
if(time<=12){
x=0;
y++;
time++;
delay(100);
}
else if(time>12&&time<=24){
x=1;
y--;
time++;
delay(100);
}
else if(time>24){
 if(rState==HIGH&&lState==LOW){
  yShip=0;
  lcd.setCursor(xShip,yShip);
  lcd.write((byte)0);
  lcd.setCursor(xShip,yShip+1);
  lcd.print(" ");
delay(dTime);
if(uState==HIGH&&xShip>0){
  xShip--;
  lcd.setCursor(xShip,yShip);
  lcd.write((byte)0);
  lcd.setCursor(xShip+1,yShip);
  lcd.print(" ");
  delay(dTime);
}
}
 

else if(lState==HIGH&&rState==LOW){
  yShip=1;
  lcd.setCursor(xShip,yShip);
  lcd.write((byte)0);
  lcd.setCursor(xShip,yShip-1);
  lcd.print(" ");
if(lState==HIGH&&rState==HIGH){
  xShip--;
  lcd.setCursor(xShip,yShip);
  lcd.write((byte)0);
  lcd.setCursor(xShip+1,yShip);
  lcd.print(" ");
  delay(dTime);
}
}
 


else if(dState==HIGH&xShip<15){
 xShip++;
 lcd.setCursor(xShip,yShip);
 lcd.write((byte)0);
 lcd.setCursor(xShip-1,yShip);
 lcd.print(" ");
 delay(dTime);
}

else if(fState==HIGH){
  lcd.setCursor(xShip-1,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-1,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-2,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-2,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-3,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-3,yShip);
  lcd.print(" ");
  lcd.setCursor(xShip-4,yShip);
  lcd.write((byte)2);
  delay(150);
  lcd.setCursor(xShip-4,yShip);
  lcd.print(" ");
 
 
  
}
else{
 lcd.setCursor(xShip,yShip);
 lcd.write((byte)0);
 
} 

}
}

I know It may sound like useless semantics, but maybe it will help you think about it in a different way.

Your invaders aren't in an array. The array has some information about your invaders. The index to the array is which invaded your talking about at any one moment, and there is information about the invader in that array.

The array doesn't shoot. It just holds information. So you shoot your missile, update its position on the screen, and then check to see if that is the same position as one of the invaders. If there is an array that holds the positional information, then maybe you roll through that array checking their position against the position of the missile. If the array holds information on whether the invader is being displayed or is already dead, then check that to see if the hit should do anything.

This is the new code, I have fixed most of the non fatal bugs, but there is one bug that is crucial to this game. The code below only display’s one alien(I still don’t get the syntax of the array after reading the site’s pages on it) and when the bullet is at the same coords as the alien, the bullet should stop, and the alien should die, but instead of that, the bullet goes right through the alien(because it never hit the block of code where it stops and where the alien dies. What can I do to make the flow, go to this part of code(and the arrays, I don’t know how to do arrays to make multiple aliens simply). Thanks Again, Ben :wink:

#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
};
int time = 0;//adds to make the bullet stop
int lButton = 13;//pin for the left button
int rButton = 6;//pin for the right button
int fButton = 7;//pin for the firing button
int lState = 0;//state of the left button
int rState = 0;//state of the right button
int fState = 0;//state of the firing button
int xb = 0;//the bullets x coords
int yb = 0;//the bullets y coords
int xa = 9;//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 xAilens[2] = {0,1};
int yAilens[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*/
boolean ailen = true;//is the one ailen alive?
boolean fire = false;//is the bullet firing
const int 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() {
lState=digitalRead(lButton);//pulls the state of the left button
rState=digitalRead(rButton);//pulls the state of the right button
fState=digitalRead(fButton);//pulls the state of the firing button
xb = xShip;
 if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/ 
fire==false;//bullet stops moving
ailen==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==true&&time<17){//if the bullet is moving and is on the screen then   
   xb = xb-time;//the bullet is time further away than the spaceship
   lcd.setCursor(xb-1,yb);//fires one away from ship not on ship
   lcd.write((byte)2);//displays bullet
   lcd.setCursor(xb+1,yb);//clears where bullet was
   lcd.print(" ");//clear symbol
   time++;//increase time
   delay(dTime/2);//bullet moves twice as fast as ship
 }

 if(fire==true&&time>=17){//if the bullet is still moving offscreen then
   
   fire=false;//stop moving the bullet
   time=0;//reset the time for next time a bullet is fired
 }
 else if(fState==HIGH&&fire==false){//if the fire button is pressed
   fire=true;//starts moving the bullet
   yb=yShip;//makes the bullet move where the ship was not where is always is
 }
  
    
 if(rState==HIGH&&lState==LOW){//if the right button is pressed and the left isnt 
  yShip=0;//make the ship move to the right if already on right, nothing happens
  lcd.setCursor(xShip,yShip);//ship's location
  lcd.write((byte)0);//display the ship byte
  lcd.setCursor(xShip,yShip+1);//clear where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing

}
 

else if(lState==HIGH&&rState==LOW){// if the right button is pressed and the right isnt
  yShip=1;//make the ship on the left of the screen, if on left, nothing happens
  lcd.setCursor(xShip,yShip);//coorinates for the ship's location
  lcd.write((byte)0);//displays the ship  
  lcd.setCursor(xShip,yShip-1);//clears where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing
}
   

else{//if nothing is pressed
 lcd.setCursor(xShip,yShip);//set previous coordinates for the ship*good practice
 lcd.write((byte)0);//display the ship
 
} 

}
 if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/ 
fire==false;//bullet stops moving
ailen==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
}

== compares two things, this is what you want in your if statement where you want to compare yb to ya.

= assigns a value, this is what you want to use where you want to change the value of alien to false.

fire==false;//bullet stops moving
ailen==false;//the alien is dead

these two lines are as useless as writing:

false;
false;

You only use one equal sign when you want to change a value.

I switched the == 's to ='s and it still did not work. Please help. I do not know where I would be without this forum. Also how would I change the lone alien to an array of aliens. Thanks

#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
};
int time = 0;//adds to make the bullet stop
int lButton = 13;//pin for the left button
int rButton = 6;//pin for the right button
int fButton = 7;//pin for the firing button
int lState = 0;//state of the left button
int rState = 0;//state of the right button
int fState = 0;//state of the firing button
int xb = 0;//the bullets x coords
int yb = 0;//the bullets y coords
int xa = 9;//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 xAilens[2] = {0,1};
int yAilens[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*/
boolean ailen = true;//is the one ailen alive?
boolean fire = false;//is the bullet firing
const int 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() {
lState=digitalRead(lButton);//pulls the state of the left button
rState=digitalRead(rButton);//pulls the state of the right button
fState=digitalRead(fButton);//pulls the state of the firing button
xb = xShip;
 if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/ 
fire=false;//bullet stops moving
ailen=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==true&&time<17){//if the bullet is moving and is on the screen then   
   xb = xb-time;//the bullet is time further away than the spaceship
   lcd.setCursor(xb-1,yb);//fires one away from ship not on ship
   lcd.write((byte)2);//displays bullet
   lcd.setCursor(xb+1,yb);//clears where bullet was
   lcd.print(" ");//clear symbol
   time++;//increase time
   delay(dTime/2);//bullet moves twice as fast as ship
 }

 if(fire==true&&time>=17){//if the bullet is still moving offscreen then
   
   fire=false;//stop moving the bullet
   time=0;//reset the time for next time a bullet is fired
 }
 else if(fState==HIGH&&fire==false){//if the fire button is pressed
   fire=true;//starts moving the bullet
   yb=yShip;//makes the bullet move where the ship was not where is always is
 }
  
    
 if(rState==HIGH&&lState==LOW){//if the right button is pressed and the left isnt 
  yShip=0;//make the ship move to the right if already on right, nothing happens
  lcd.setCursor(xShip,yShip);//ship's location
  lcd.write((byte)0);//display the ship byte
  lcd.setCursor(xShip,yShip+1);//clear where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing

}
 

else if(lState==HIGH&&rState==LOW){// if the right button is pressed and the right isnt
  yShip=1;//make the ship on the left of the screen, if on left, nothing happens
  lcd.setCursor(xShip,yShip);//coorinates for the ship's location
  lcd.write((byte)0);//displays the ship  
  lcd.setCursor(xShip,yShip-1);//clears where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing
}
   

else{//if nothing is pressed
 lcd.setCursor(xShip,yShip);//set previous coordinates for the ship*good practice
 lcd.write((byte)0);//display the ship
 
} 

}

Hi.

Don't try to solve all problems in one go.
Take each problem and solve that one before you move and concentrate to a next one.

Right now I am mainly concerned with the collision of the bullet and the alien, thus the parenthesis of the other problems.

if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/ 
fire=false;//bullet stops moving
ailen=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

That code is not working
full code-

#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
};
int time = 0;//adds to make the bullet stop
int lButton = 13;//pin for the left button
int rButton = 6;//pin for the right button
int fButton = 7;//pin for the firing button
int lState = 0;//state of the left button
int rState = 0;//state of the right button
int fState = 0;//state of the firing button
int xb = 0;//the bullets x coords
int yb = 0;//the bullets y coords
int xa = 9;//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 xAilens[2] = {0,1};
int yAilens[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*/
boolean ailen = true;//is the one ailen alive?
boolean fire = false;//is the bullet firing
const int 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() {
lState=digitalRead(lButton);//pulls the state of the left button
rState=digitalRead(rButton);//pulls the state of the right button
fState=digitalRead(fButton);//pulls the state of the firing button
xb = xShip;
 if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/ 
fire=false;//bullet stops moving
ailen=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==true&&time<17){//if the bullet is moving and is on the screen then   
   xb = xb-time;//the bullet is time further away than the spaceship
   lcd.setCursor(xb-1,yb);//fires one away from ship not on ship
   lcd.write((byte)2);//displays bullet
   lcd.setCursor(xb+1,yb);//clears where bullet was
   lcd.print(" ");//clear symbol
   time++;//increase time
   delay(dTime/2);//bullet moves twice as fast as ship
 }

 if(fire==true&&time>=17){//if the bullet is still moving offscreen then
   
   fire=false;//stop moving the bullet
   time=0;//reset the time for next time a bullet is fired
 }
 else if(fState==HIGH&&fire==false){//if the fire button is pressed
   fire=true;//starts moving the bullet
   yb=yShip;//makes the bullet move where the ship was not where is always is
 }
  
    
 if(rState==HIGH&&lState==LOW){//if the right button is pressed and the left isnt 
  yShip=0;//make the ship move to the right if already on right, nothing happens
  lcd.setCursor(xShip,yShip);//ship's location
  lcd.write((byte)0);//display the ship byte
  lcd.setCursor(xShip,yShip+1);//clear where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing

}
 

else if(lState==HIGH&&rState==LOW){// if the right button is pressed and the right isnt
  yShip=1;//make the ship on the left of the screen, if on left, nothing happens
  lcd.setCursor(xShip,yShip);//coorinates for the ship's location
  lcd.write((byte)0);//displays the ship  
  lcd.setCursor(xShip,yShip-1);//clears where the ship was
  lcd.print(" ");//clearing block
  delay(dTime);//time to deter mashing
}
   

else{//if nothing is pressed
 lcd.setCursor(xShip,yShip);//set previous coordinates for the ship*good practice
 lcd.write((byte)0);//display the ship
 
} 

}

Hi,

I was posting in the other thread...

Just a thought after reading this. If you're having issues knowing the difference between == and = then I think space invaders is only going to frustrate you.

You do seem to be learning fast though, which is a good thing.

I suggest perhaps doing some more basic coding skills first though? (And possibly after that and if you're keen, some bit-wise operation knowledge, if you know bit-wise stuff then you can do this much more efficiently)

wwb00:
That code is not working

That snippet all depends on the if statement:

if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
 and the alien's position is the same as the bullets, then*/

If any of these isn’t true, you’ll be in trouble.
So to do some debugging here, have a look to see which of these conditions isn’t true.
A true is a 1, a false is a 0 as you already know.
So this:

if(fire==true&&yb==ya&&xb==xa&&ailen==true)

Should have the same result as this, but added with a tool to find out where you get stuck:

if (fire) {
   Serial.print ("condition 1");
  if (yb==ya){
    Serial.print ("condition 2");
   if (xb==xa){
     Serial.print ("condition 3");
    if (ailen){
      Serial.print ("condition 4");
      Do whatever you need to do;
    }
   }
  }
 }

Of course you need to set up serial communication first.
Now you can see what conditions are met, using the serial monitor to debug your code.
(Did you notice the golden tip ?)
The first condition that is not shown, will be your culprit and needs your attention.
After you solved that, recheck because there might be more problems to solve.
If you’re done debugging, you can take that part out and tidy up your code.