Space Invaders issue
in
Programming Questions
•
5 months ago
I checked through similar codes and was still unable to figure out exactly why my issue is happening. Once the spaceships bounce off the wall for the fifth time, the don't change direction and the y+=20 loops until they are off the screen. I have tried probably going on fifteen or so different combinations of changes to figure out why this is happening but I'm drawing a blank. In addition, the ship on the second row from the top and all the way to the left drifts away from the other ships, even though they entirely share code and that shouldn't be possible.
The code is listed below. Thanks in advance for any help.
Bullet[]bullets = new Bullet[100];
Ship[]ships = new Ship[30];
Base base1 = new Base();
int bulletCounter=0;
int xloc=400;
int yloc;
float s=2;
int y=50;
boolean posCheck;
int shipY=50;
int timer;
void setup(){
size(800,800);
smooth();
int d=50; //placement of ships x coordinate at start
int shipwid=15; //size of ship (wanted it to be easily changable)
for(int i = 0; i < 15; i++){
ships[i] = new Ship(d,shipwid,shipY); //initializes the ships (first row)
d=d+45; //spaces ships 40 apart
}
d=50;
for(int i = 15; i < 30; i++){
ships[i] = new Ship(d,shipwid,shipY+50); //initializes the ships (second row)
d=d+45; //spaces ships 40 apart
}
}
void draw(){
background(255);
noCursor();
base1.display();
base1.drive();
for(int i=0; i<bulletCounter; i++){ //allows bullets to fire upon mousePress
bullets[i].fire();
bullets[i].display();
}
for(int i = 0; i<ships.length; i++){
ships[i].fly(s); //shows and flies ships
ships[i].display();
if(timer>4){
s=1;}
if(timer>8){
s=1.5;
}
for(int i2=0; i2 < bulletCounter; i2++){
if(bullets[i2].intersect(ships[i])){ //checks to see if ships hit
ships[i].damage(); //damages ships
bullets[i2].bulletHit();
}
}
y=0;
posCheck= ships[i].check();
if(posCheck==true){ //moves ships as a group
s=s*-1;
y=20;
for(int j = 0; j<ships.length; j++){
ships[j].drop(y);
}
timer+=1;
}
}
}
void mousePressed(){
xloc=base1.x;
yloc=height-(base1.hei/2);
if(bulletCounter>=bullets.length){ //resets bullets to first element of array
bulletCounter=0;
}
bullets[bulletCounter] = new Bullet(xloc,yloc); //creates a new Bullet at xloc
bulletCounter++;
}
class Base{
int x;
int y;
int wid;
int hei;
int speed;
Base(){
x=width/2;
wid=100;
hei=50;
y=800-(hei/2);
speed=5;
}
void display(){
rectMode(CENTER);
stroke(0);
strokeWeight(1);
fill(0);
rect(x,y,wid,hei);
}
void drive(){
if(mouseX>x){
x+=speed;
}
if(mouseX<x){
x-=speed;
}
}
}
class Bullet{
int x;
int y;
int speed;
int vol;
Bullet(int a,int b){
x=a;
y=b;
speed=5;
vol=3;
}
void display(){
fill(0);
rectMode(CENTER);
rect(x,y,vol,vol*2); //displays bullet
}
void fire(){
y=y-speed; //moves bullet
if(mousePressed){ //ensures bullets start moving upon mousePress.
speed=5;
}
}
boolean intersect(Ship ship){ //checks for bullet hitting ship
if(dist(x,y,ship.x,ship.y1) < ship.wid){
return true;
}
else{ return false; }
}
void bulletHit(){
x=x+800;
y=y+800;
vol=0;
}
}
class Ship{
float x;
int y1;
int speed;
int wid;
boolean ch;
int col=255;
int damageCounter=0;
int timer=0;
Ship(float x_,int wid_,int y_){
x=x_;
y1=y_;
wid=wid_;
}
void display(){ //displays the ships
fill(col);
strokeWeight(1);
rectMode(CENTER);
if(damageCounter<3){
rect(x,y1,wid,wid); //ship main body
rect(x,y1+(wid/3),wid*1.5,wid/3); //ship saucer piece
}
}
void fly(float sp){ //makes the ships fly around
x+=sp;
if((x+(wid/2)<=width)||(x-(wid/1.5)<=0)){
//s=s*-1;
//y1=y1+10;
timer+=1;
}
}
//checks to see if edge of screen is hit and if so, drops
//and changes speed for all ships of the row
boolean check(){
if((x+(wid/2)>width)||(x-(wid/1.5)<0)){
ch=true;
}
else{ch=false;}
return ch;
}
void damage(){
if(damageCounter==3){
wid=0;
y1=-800;
damageCounter=4;
}
if(damageCounter==2){
col=0;
damageCounter=3;
}
if(damageCounter==1){
col=125;
damageCounter=2;
}
if(damageCounter==0){
col=185;
damageCounter=1;
}
}
void drop(int d){
y1=y1+d;
}
}
1