LCD Space Invaders

Below is an working program of how the spaceship moves. Would it be possible to add "aliens" that move left and right, and when the "bullet" hits the certain area of the LCD, it clears the alien from the tile, but doesn't clear any of the other aliens, only the one the bullet was to hit. Would this be possible with the capabilities of the arduino uno? If so, then how should I approach this problem? Thanks, Ben :wink:

#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 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 xShip=15;
int yShip=0;
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() {
lState=digitalRead(lButton);
rState=digitalRead(rButton);
fState=digitalRead(fButton); 
uState=digitalRead(uButton);
dState=digitalRead(dButton);


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

}
}

Also, the circuit is wired with 5 buttons. Two, up and down, Two left and right, and one to fire. The lcd is wired as it should be.

Would this be possible with the capabilities of the arduino uno?

Yes.

The best way is to have an array with the ships in it, with a true if the ship is there and a false if it is not. Your code should draws all the ships that are there, that is who's array entry is true.

Then when your bullet hits a ship you change that array entry to false, so next time the ship does not get drawn.

However you need to learn about arrays. Also you need to learn about loops that code is just too turgid to do anything with.

I made snake which was made up of a 1D array (to check if it ate itself), but that was on a standard AVR, not arduino.

If I recall there were RAM issues sooner or later, but quite doable, and quite playable too (I had high scores saved to FRAM)

Space invaders sounds fun!

Grumpy_Mike:
Yes.

The best way is to have an array with the ships in it, with a true if the ship is there and a false if it is not. Your code should draws all the ships that are there, that is who's array entry is true.

Then when your bullet hits a ship you change that array entry to false, so next time the ship does not get drawn.

However you need to learn about arrays. Also you need to learn about loops that code is just too turgid to do anything with.

I looked at the arduino array pages, and I do not understand how the arrays would help in the project, how would/should they be used in the code? This would really help if there possibly was a sample code for how to use the array for this project. What I understood from the array pages, was that arrays were a way to store multiple variables into one easy "package" and that they could be used in loops.

Delta_G:
Given what Atari was working with when they made the original, it should surely be possible to do with an Arduino.

I hope so...

1:1:
I made snake which was made up of a 1D array (to check if it ate itself), but that was on a standard AVR, not arduino.

If I recall there were RAM issues sooner or later, but quite doable, and quite playable too (I had high scores saved to FRAM)

Space invaders sounds fun!

Yes, Space Invaders for the arduino would be Fun!!

Your missiles for each frame will move up 1 pixel (1 screen pixel = 1 'game space' unit) for each unit of 'game time'. At the same time your array of aliens are moving sideways and/or down one screen pixel per unit game time, you only need one point to define the group as all individual aliens are simply offsets from this point. The top left corner for instance could be a good point, assuming your screen has it's origin at the top left. The alien offsets are a scalar factor of their array indices, which makes it very easy at each point of game time to check IF ('current missile position'== 'current alien position') && (alien is alive).

etc.

(Very likely you could make it more efficient/clever than that, but as for explaining it in a forum I left it more 'human logical'...)

Read what Mike said again now, it's a good idea, hopefully it's make more sense?

1:1:
Your missiles for each frame will move up 1 pixel (1 screen pixel = 1 'game space' unit) for each unit of 'game time'. At the same time your array of aliens are moving sideways and/or down one screen pixel per unit game time, you only need one point to define the group as all individual aliens are simply offsets from this point. The top left corner for instance could be a good point, assuming your screen has it's origin at the top left. The alien offsets are a scalar factor of their array indices, which makes it very easy at each point of game time to check IF ('current missile position'== 'current alien position') && (alien is alive).

etc.

(Very likely you could make it more efficient/clever than that, but as for explaining it in a forum I left it more 'human logical'...)

Read what Mike said again now, it's a good idea, hopefully it's make more sense?

Thanks, you both explained it very thoroughly to me. I think I will be able to do this. If not, I will post on the programming questions. Thanks!

Thanks again for the help, but I have one more question. I was declaring the aliens as an array, and how could I check to see if each alien was true, and which were false?

int xAilens[2] = {0,1};
int yAilens[12]= {0,1,2,3,4,5,6,7,8,9,10,11};

Have another array to decide if you need to display the alien or not. This array starts off as every member as true, when you hit one you change its entry to false. When you plot them you check this array and only plot that position if it is true.

You've just made an array that has values that are the same as its indices. Do you see how this is redundant information? i.e. wasteful of RAM.

Please read what we've said again.

Ask questions if you don't understand :slight_smile: