# Space invaders game

**URL:** <https://discourse.processing.org/t/space-invaders-game/25489>\
**Category:** Coding Questions\
**Created:** [November 17, 2020, 2:47pm UTC](https://discourse.processing.org/t/space-invaders-game/25489 "2020-11-17T14:47:02Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 2:47pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/1 "2020-11-17T14:47:02Z")

</div>

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!

```auto
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:

```auto
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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 2:47pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/2 "2020-11-17T14:47:50Z")

</div>

Alien class: (if needed)

```auto
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);    
  }
}

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 2:55pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/3 "2020-11-17T14:55:49Z")

</div>

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

 ![Screenshot 2020-11-17 at 14.55.37](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/26f6cb4788fdeb60e147b14a2d6a780b56e6da43.jpeg)

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 3:04pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/4 "2020-11-17T15:04:26Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 3:07pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/5 "2020-11-17T15:07:12Z")

</div>

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

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

 ![Screenshot 2020-11-17 at 15.06.13](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ad0c8d70025556d8b34826b3bad49cc1cc73f262.jpeg)

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

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 3:20pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/6 "2020-11-17T15:20:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 3:34pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/7 "2020-11-17T15:34:26Z")

</div>

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

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 4:12pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/8 "2020-11-17T16:12:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 5:06pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/9 "2020-11-17T17:06:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 6:54pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/10 "2020-11-17T18:54:03Z")

</div>

ive got my bullet working with firing straight,

```auto
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

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 17, 2020, 7:20pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/11 "2020-11-17T19:20:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 7:22pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/12 "2020-11-17T19:22:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 7:25pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/13 "2020-11-17T19:25:08Z")

</div>

> <https://stackoverflow.com/questions/64881465/space-invaders-game-in-processing-https-processing-org>

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\*\*\*

```auto
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;
  }
 */
}

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 8:30pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/14 "2020-11-17T20:30:44Z")

</div>

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:

```auto
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…

 ![Screenshot 2020-11-17 at 20.31.36](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bdb635eae92d480e80ae54a8eb69130b17c75508.jpeg)  
 ![Screenshot 2020-11-17 at 20.32.02](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/ff25ee452b1b6d8fdd46f71e7c140cdfdb382651.jpeg)  
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.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 8:41pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/15 "2020-11-17T20:41:55Z")

</div>

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

```auto
//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

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 17, 2020, 10:18pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/16 "2020-11-17T22:18:20Z")

</div>

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

```auto
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:

```auto
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;
  }
}
}

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 18, 2020, 12:11pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/17 "2020-11-18T12:11:26Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 18, 2020, 12:26pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/18 "2020-11-18T12:26:32Z")

</div>

Nice work!

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

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 18, 2020, 12:29pm UTC](https://discourse.processing.org/t/space-invaders-game/25489/19 "2020-11-18T12:29:39Z")

</div>

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