Space invaders game

Hi again, hope everyone is doing well…

im stuck on trying to get my player to shoot bullets at the alien.

main:
my background is an everlasting background which will always carry on!

PImage background; 
int x=0; //global variable background location
int y=0;


Player player1;
Alien  Alien1;
bullet bullet1;

void setup(){
  //size(1000,800);
  size(800,400);
  background = loadImage("spaceBackground.jpg");
  background.resize(width,height);
  player1 = new Player();
  Alien1 = new Alien();
  bullet1 = new bullet();
}

void draw ()
{
    
    image(background, x, 0); //draw background twice adjacent
    image(background, x+background.width, 0);
    x -=4;
    if(x == -background.width) 
          x=0; //wrap background
     player1.display();
     Alien1.display();
     Alien1.move();
}
 
 
void keyPressed() {
  if (key == CODED)
  {
    if (keyCode == UP && player1.y>=0) { //restrict to screen edge
      player1.y -=15;
    } else if (keyCode == DOWN && player1.y<=360) {
      player1.y +=15;
    } else if (keyCode == LEFT && player1.x>=0) {
      player1.x -=15;
    } else if (keyCode == RIGHT && player1.x<=800) {
      player1.x +=15;
    }
  }
  if (key==' ')
    {
      bullet1.x=player1.x+75;
      bullet1.y=player1.y;
 
      bullet1=new bullet();
     // bullet2=new bullet(bulletX, bulletY);
 
    }  
}

Player class:

class Player {
   int x,y;
   float SpeedX,SpeedY;

   Player() {
     this.x = width/2 - 350;
     this.y = height/2;
   }
     
void display() {

 // fill(0,0,200);
  // rect(this.x,this.y,20,10); //draw top box
   fill(255,0,0); //draw rocket
   rect(this.x,this.y+10,50,20);
   triangle(this.x+50,this.y+10,this.x+50,this.y+30,this.x+60,this.y+20);
}

/*
boolean crash()
  {
    color detectedColour;
    for (int i=y; i<=y+3-0; i++)
    {
      detectedColour=get(x+10,i);
 
      if (detectedColour==color(255))
      {
        return true;
      }
    }
        return false;
  }
 */
}

bullet class:

class bullet
{
  int x;
  int y;
 
  bullet()
  {
    this.x=width/2 - 350;
    this.y=height/2;
  }
 
  void render()
  {
    fill(0,255,255);
    rect(x-5,y,10,20);
    fill(255);
    rect(x-5,y-10,10,10);
 
    //fill(255);
    //ellipse(x,y,20,20);
    //fill(0);
    //ellipse(x,y,5,5);
  }
 
  void move()
  {
    y=y-10;
  }
 
  void update()
  {
    render();
    move();
  }
 
 boolean crash()
  {
    color detectedColour;
    for (int i=y;  i<=y+3-0; i++)
    {
      detectedColour=get(x+30,i);
      if (detectedColour==color(255))
      {
        return true;
      }
    }
    return false;
}  

}//end of class

Alien class: (if needed)

final color ALIEN1 = color(255,0,0);
final color ALIEN2 = color(50,100,0);

class Alien {
  
  float x,y;
  float v;
  
  Alien() {
    this.x = width/2 + 350;
     this.y = height/2;
     this.v = 5;
  }
  
  void move() {
 
    y=y+v;  // which direction the alien is moving
 
    if(y> height-10)
 
    { 
      v = -5;
      x = x-45;
    }
    if (y==25)
    {
      v =+5;
      x= x-45;
    }
 
  }//end of move
  
  void display() {
    //draw an alien  
   fill(ALIEN1);
   ellipse(x,y,30,30);
   fill(ALIEN2);
   ellipse(x,y,50,15);    
  }
}

ive also been looking at previous forums on this topic and they don’t see to solve my problem but make it worse… :frowning:

i know i think im close, my mistake is in here I think:

tif (key==' ')
    {
      bullet1.x=player1.x+75;
      bullet1.y=player1.y;
 
      bullet1=new bullet();
     // bullet2=new bullet(bulletX, bulletY);
 
    }  

wait ive corrected one of my mistakes of why the bullet wasnt shooting was because i didnt put it inside void draw()… :joy:

but now I want to make it that the bullet will shoot where my player 1 is eg.

the bullet just comes out of 1 position of where bullet x and y is
eg the bullet is only firing upward and doesn’t follow the player

I think ill need to do bullet(player1.x,player1.y)

well bullet(player1.x,player1.y) didnt work.

changes i made to think that would work:
//in void setup()

bullet1 = new bullet(player1.x,player1.y);

//in keyspressed()
bullet1=new bullet(player1.x,player1.y);
     // bullet2=new bullet(bulletX, bulletY);

and in bullet class:

bullet(int x,int y)
  {
    this.x=width/2 - 350;
    this.y=height/2;
  }
 

ive decided im gonna need assistance as I’m on the verge of crying as this doesn’t work… :pleading_face:

yeh i have no clue on how to do it, been messing around with my code for more than hour, some assistance with this would be nice.

ive got my bullet working with firing straight,

void move()
  {
    x=x+10;
    
  }

i just need help with the bullet moving with my player and all I need to do is now change how the rectangle is firing

edit1: the rectangle has also been fixed, just need to fix the bullet moving with player1

Please (please please please) post your complete working current sketch as a single block of code. It is near impossible to help you unless we can copy, paste, and run your code to see what is going on.

Make helping you easy for us. Having to piece together your code and all the changes you’ve made to it is not easy.

1 Like

yes sorry my bad, ive also been trying to fix it while crying and have made a mess. ill put up an updated new version.

entire code… my background was an image but I’ve just set it to background(0); so less work for you guys,*** cries in laughter but its actually pain***

PImage background; 
int x=0; //global variable background location
int y=0;


Player player1;
Alien  Alien1;
bullet bullet1;

void setup(){
  //size(1000,800);
  size(800,400);
  //background = loadImage("spaceBackground.jpg");
  //background.resize(width,height);
  player1 = new Player();
  Alien1 = new Alien();
  bullet1 = new bullet(player1.x,player1.y);
}

void draw ()
{
    background(0);
   // image(background, x, 0); //draw background twice adjacent
   // image(background, x+background.width, 0);
  //  x -=4;
   // if(x == -background.width) 
        //  x=0; //wrap background
     player1.display();
     bullet1.update();
     Alien1.display();
     Alien1.move();
     
}
 
 
void keyPressed() {
  if (key == CODED)
  {
    if (keyCode == UP && player1.y>=0) { //restrict to screen edge
      player1.y -=15;
    } else if (keyCode == DOWN && player1.y<=360) {
      player1.y +=15;
    } else if (keyCode == LEFT && player1.x>=0) {
      player1.x -=15;
    } else if (keyCode == RIGHT && player1.x<=800) {
      player1.x +=15;
    }
  }
  if (key==' ')
    {
      bullet1.x=player1.x;
      bullet1.y=player1.y;
 
      bullet1=new bullet(player1.x,player1.y);
     // bullet2=new bullet(bulletX, bulletY);
 
    }  
}

final color ALIEN1 = color(255,0,0);
final color ALIEN2 = color(50,100,0);

class Alien {
  
  float x,y;
  float v;
  
  Alien() {
    this.x = width/2 + 350;
     this.y = height/2;
     this.v = 5;
  }
  
  void move() {
 
    y=y+v;  // which direction the alien is moving
 
    if(y> height-10)
 
    { 
      v = -5;
      x = x-45;
    }
    if (y==25)
    {
      v =+5;
      x= x-45;
    }
 
  }//end of move
  
  void display() {
    //draw an alien  
   fill(ALIEN1);
   ellipse(x,y,30,30);
   fill(ALIEN2);
   ellipse(x,y,50,15);    
  }
}
class bullet
{
  int x;
  int y;
 
  bullet(int x,int y)
  {
    this.x=width/2 - 350;
    this.y=height/2;
  }
 
  void render()
  {
    fill(0,255,255);
    rect(x-5,y-10,30,10);
    fill(255);
    rect(x-5,y-10,10,10);
 
    //fill(255);
    //ellipse(x,y,20,20);
    //fill(0);
    //ellipse(x,y,5,5);
  }
 
  void move()
  {
    x=x+10;
    
  }
 
  void update()
  {
    render();
    move();
  }
 
 boolean crash()
  {
    color detectedColour;
    for (int i=y;  i<=y+3-0; i++)
    {
      detectedColour=get(x+30,i);
      if (detectedColour==color(255))
      {
        return true;
      }
    }
    return false;
}  

}//end of class

class Player {
   int x,y;
   //float SpeedX,SpeedY;

   Player() {
     this.x = width/2 - 350;
     this.y = height/2;
   }
     
void display() {

 // fill(0,0,200);
  // rect(this.x,this.y,20,10); //draw top box
   fill(255,0,0); //draw rocket
   rect(this.x,this.y+10,50,20);
   triangle(this.x+50,this.y+10,this.x+50,this.y+30,this.x+60,this.y+20);
}

/*
boolean crash()
  {
    color detectedColour;
    for (int i=y; i<=y+3-0; i++)
    {
      detectedColour=get(x+10,i);
 
      if (detectedColour==color(255))
      {
        return true;
      }
    }
        return false;
  }
 */
}

edit2: after 3 hours and 43mins, im finally getting somewhere with this code. i would like to thank my tears for helping me with this…
updated code:

PImage background; 
int x=0; //global variable background location
int y=0;


Player player1;
Alien  Alien1;
bullet bullet1;

void setup(){
  //size(1000,800);
  size(800,400);
  background = loadImage("spaceBackground.jpg");
  background.resize(width,height);
  player1 = new Player();
  Alien1 = new Alien();
  bullet1 = new bullet();
}

void draw ()
{
    
    image(background, x, 0); //draw background twice adjacent
    image(background, x+background.width, 0);
    x -=4;
    if(x == -background.width) 
          x=0; //wrap background
     player1.display();
     bullet1.update();
     Alien1.display();
     Alien1.move();
     
}
 
 
void keyPressed() {
  if (key == CODED)
  {
    if (keyCode == UP && player1.y>=0) { //restrict to screen edge
      player1.y -=15;
    } else if (keyCode == DOWN && player1.y<=360) {
      player1.y +=15;
    } else if (keyCode == LEFT && player1.x>=0) {
      player1.x -=15;
    } else if (keyCode == RIGHT && player1.x<=800) {
      player1.x +=15;
    }
  }
  if (key==' ')
    {
     // bullet1.x=player1.x;
     // bullet1.y=player1.y;
 
      bullet1=new bullet();
     // bullet2=new bullet(bulletX, bulletY);
 
    }  
}
final color ALIEN1 = color(255,0,0);
final color ALIEN2 = color(50,100,0);

class Alien {
  
  float x,y;
  float v;
  
  Alien() {
    this.x = width/2 + 350;
     this.y = height/2;
     this.v = 5;
  }
  
  void move() {
 
    y=y+v;  // which direction the alien is moving
 
    if(y> height-10)
 
    { 
      v = -5;
      x = x-45;
    }
    if (y==25)
    {
      v =+5;
      x= x-45;
    }
 
  }//end of move
  
  void display() {
    //draw an alien  
   fill(ALIEN1);
   ellipse(x,y,30,30);
   fill(ALIEN2);
   ellipse(x,y,50,15);    
  }
}
class bullet
{
  int x;
  int y;
 
  bullet()
  {
    this.x=player1.x;
    this.y=player1.y;
  }
 
  void render()
  {
    fill(0,255,255);
    rect(x-5,y-10,30,10);
    fill(255);
    rect(x-5,y-10,10,10);
 
    //fill(255);
    //ellipse(x,y,20,20);
    //fill(0);
    //ellipse(x,y,5,5);
  }
 
  void move()
  {
    //to fire bullet
    x=x+10;
    
  }
 
  void update()
  {
    render();
    move();
  }
 
 boolean crash()
  {
    color detectedColour;
    for (int i=y;  i<=y+3-0; i++)
    {
      detectedColour=get(x+30,i);
      if (detectedColour==color(255))
      {
        return true;
      }
    }
    return false;
}  

}//end of class
class Player {
   int x,y;
   //float SpeedX,SpeedY;

   Player() {
     this.x = width/2 - 350;
     this.y = height/2;
   }
     
void display() {

 // fill(0,0,200);
  // rect(this.x,this.y,20,10); //draw top box
   fill(255,0,0); //draw rocket
   rect(this.x,this.y+10,50,20);
   triangle(this.x+50,this.y+10,this.x+50,this.y+30,this.x+60,this.y+20);
}

/*
boolean crash()
  {
    color detectedColour;
    for (int i=y; i<=y+3-0; i++)
    {
      detectedColour=get(x+10,i);
 
      if (detectedColour==color(255))
      {
        return true;
      }
    }
        return false;
  }
 */
}

what i have fixed is that the bullet now somewhat is following the player…



now wherever my player1 goes, my bullet follows, now i just need to show my player without the bullet showing and the bullet should only show when it is fired.

update3: ive fixed my bullet and now it follows my player1 everywhere.

//PImage background; 
int x=0; //global variable background location
int y=0;


Player player1;
Alien  Alien1;
bullet bullet1;

void setup(){
  //size(1000,800);
  size(800,400);
 // background = loadImage("spaceBackground.jpg");
 // background.resize(width,height);
  player1 = new Player();
  Alien1 = new Alien();
  bullet1 = new bullet();
}

void draw ()
{
    background(0);
   // image(background, x, 0); //draw background twice adjacent
   // image(background, x+background.width, 0);
   // x -=4;
   // if(x == -background.width) 
   //       x=0; //wrap background
     player1.display();
     bullet1.update();
     Alien1.display();
     Alien1.move();
     
}
 
 
void keyPressed() {
  if (key == CODED)
  {
    if (keyCode == UP && player1.y>=0) { //restrict to screen edge
      player1.y -=15;
    } else if (keyCode == DOWN && player1.y<=360) {
      player1.y +=15;
    } else if (keyCode == LEFT && player1.x>=0) {
      player1.x -=15;
    } else if (keyCode == RIGHT && player1.x<=800) {
      player1.x +=15;
    }
  }
  if (key==' ')
    {
     // bullet1.x=player1.x;
     // bullet1.y=player1.y;
 
      bullet1=new bullet();
     // bullet2=new bullet(bulletX, bulletY);
 
    }  
}
final color ALIEN1 = color(255,0,0);
final color ALIEN2 = color(50,100,0);

class Alien {
  
  float x,y;
  float v;
  
  Alien() {
    this.x = width/2 + 350;
     this.y = height/2;
     this.v = 5;
  }
  
  void move() {
 
    y=y+v;  // which direction the alien is moving
 
    if(y> height-10)
 
    { 
      v = -5;
      x = x-45;
    }
    if (y==25)
    {
      v =+5;
      x= x-45;
    }
 
  }//end of move
  
  void display() {
    //draw an alien  
   fill(ALIEN1);
   ellipse(x,y,30,30);
   fill(ALIEN2);
   ellipse(x,y,50,15);    
  }
}
class bullet
{
  int x;
  int y;
 
  bullet()
  {
    this.x=player1.x;
    this.y=player1.y+23;
  }
 
  void render()
  {
    fill(0,255,255);
    rect(x-5,y-10,30,10);
    fill(255);
    rect(x-5,y-10,10,10);
 
    //fill(255);
    //ellipse(x,y,20,20);
    //fill(0);
    //ellipse(x,y,5,5);
  }
 
  void move()
  {
    //to fire bullet
    x=x+10;
    
  }
 
  void update()
  {
    render();
    move();
  }
 
 boolean crash()
  {
    color detectedColour;
    for (int i=y;  i<=y+3-0; i++)
    {
      detectedColour=get(x+30,i);
      if (detectedColour==color(255))
      {
        return true;
      }
    }
    return false;
}  

}//end of class
class Player {
   int x,y;
   //float SpeedX,SpeedY;

   Player() {
     this.x = width/2 - 350;
     this.y = height/2;
   }
     
void display() {

 // fill(0,0,200);
  // rect(this.x,this.y,20,10); //draw top box
   fill(255,0,0); //draw rocket
   rect(this.x,this.y+10,50,20);
   triangle(this.x+50,this.y+10,this.x+50,this.y+30,this.x+60,this.y+20);
}

/*
boolean crash()
  {
    color detectedColour;
    for (int i=y; i<=y+3-0; i++)
    {
      detectedColour=get(x+10,i);
 
      if (detectedColour==color(255))
      {
        return true;
      }
    }
        return false;
  }
 */
}

the next part needs to work on is when my bullet collides with the alien needs to disappear and another alien needs to appear on the screen. I’m thinking of maybe working it into a loop

im stuck on when the alien goes right to left, I want to get it back to right.

PImage background; 
int x=0; //global variable background location
int y=0;



Alien  Alien1;


void setup(){
  //size(1000,800);
  size(800,400);
 // background = loadImage("spaceBackground.jpg");
 // background.resize(width,height);

  Alien1 = new Alien();
  
}

void draw ()
{
    background(0);
  //  image(background, x, 0); //draw background twice adjacent
  ///  image(background, x+background.width, 0);
  ///  x -=4;
  //  if(x == -background.width) {
  //        x=0; //wrap background
   // }
    
     Alien1.update();
     
}

Alien class:

final color ALIEN1 = color(255,0,0);
final color ALIEN2 = color(50,100,0);

class Alien {
  
  float x,y;
  float v;
  
  Alien() {
    this.x = width/2 + 350;
     this.y = height/2;
     this.v = 15;
  }
  
  void move() {
 
    y=y+v;  // which direction the alien is moving
 
    if(y> height-10)
 //-10
    { 
      v = -5;
      x = x-45;
    }
    if (y==25)
    {
      v =+5;
      x= x-45;
    }
 //if (x<0)
 //{
 //display();
 //}
  }//end of move
  
  void display() {
    //draw an alien  
   fill(ALIEN1);
   ellipse(x,y,30,30);
   fill(ALIEN2);
   ellipse(x,y,50,15);    
  }
  
  void update() {
    display();
    rebound();
    move();
    
    
    
  }
  void rebound(){
   if ( x <width ) {  
    x = x - 2; 
  }    
  else {
    // move the alien back to the right side of the window, NOT WORKING
    this.x = width/2 + 350;
     this.y = height/2;
  }
}
}

Sometimes i think im generally having a bad day when coding, but right now I’m on fire baby. solved all the problems I had last night with little to no help. so I wont be posting my solutions has I did them myself…

1 Like

Nice work!

Remember you can click ctrl-t in processing (not the forum) to get auto-format of your code

1 Like

oh wow, thanks for that. looks so much cleaner. <3

1 Like