# ArrayList Processing

**URL:** <https://discourse.processing.org/t/arraylist-processing/410>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 26, 2018, 1:59pm UTC](https://discourse.processing.org/t/arraylist-processing/410 "2018-05-26T13:59:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hedre](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hedre](https://discourse.processing.org/u/Hedre)\
**Post date:** [May 26, 2018, 1:59pm UTC](https://discourse.processing.org/t/arraylist-processing/410/1 "2018-05-26T13:59:11Z")

</div>

Hi

Firstly, I’m french, so my english is not so good 🙂 and I have trouble using the forum

I programmed a kind of Space Invaders, it’s a project for the school. I must have finished him for monday but I have a problem : we must shoot to update the number of lives

concerned party :

```auto
void game(){
  for(Bullet Bullet : bullets){
    Bullet.display();
    Bullet.move();
  }
  
  for(Enemy Enemy : enemies){
    Enemy.display();
    Enemy.move();
  }
  
  if(millis()-iTime > random(1000, 2000)){
    enemies.add(new Enemy(random(4, 1030), -16, random(25, 50), random(1, 6)));
    iTime = millis();
  }
  
  for(int i=0; i < bullets.size(); i++){
    for(int j=0; j < enemies.size(); j++){
      float distance = dist(bullets.get(i).x, bullets.get(i).y, enemies.get(j).x+enemies.get(j).w/2, enemies.get(j).y+enemies.get(j).w/2);
      if(distance < enemies.get(j).w/2+9){
        bullets.remove(i);
        enemies.remove(j);
        score += 1;
        break;
      }
      
      else if(bullets.get(i).y+5 < 50){
        bullets.remove(i);
        break; 
      }
      
      else if(enemies.get(j).y+enemies.get(j).w > 688){
        enemies.remove(j);
        lives -= 1;
        break;
      }
    }
  }

```

program :

```auto
int clr;
int iTime;
int lives = 3;
int score = 0;
int xplayer=500;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Enemy> enemies = new ArrayList<Enemy>();
boolean keyAPressed=false, keyZPressed=false, keySPressed=false;

void setup(){
  size(1080, 680);
  frameRate(60);
  iTime = millis();
}

void draw(){
  smooth();
  noStroke();
  colorMode(HSB, 75);
  clr++;
  if(clr>75) clr=0;
  clean();
  if(keySPressed){
    if(lives > 0){
      game();
      player();
    }
    else{
      gameover();
      for(int i=enemies.size()-1; i >= 0; i--){
        enemies.remove(i);
      }
    
     /* for(int i=bullets.size()-1; i >= 0; i--){
        bullets.remove(i);
      }*/
    
      if(keyPressed && key==ENTER){
        game();
        lives = 3;
        score = 0;
      }
    }
  }
  else{
    startgame();
  }
}

void clean(){
  background(0);
}

void startgame(){
  fill(0, 0, 100);
  textSize(30);
  text("Press 'ENTER' to start game", 330, 350);
}

void player(){
  fill(0, 0, 100);
  rect(xplayer, 660, 80, 20);
  rect(xplayer+20, 650, 40, 10);
  if(-20 < xplayer) if(keyAPressed) xplayer -= 10;
  if(xplayer < 1020) if(keyZPressed) xplayer += 10;
}

void game(){
  for(Bullet Bullet : bullets){
    Bullet.display();
    Bullet.move();
  }
  
  for(Enemy Enemy : enemies){
    Enemy.display();
    Enemy.move();
  }
  
  if(millis()-iTime > random(1000, 2000)){
    enemies.add(new Enemy(random(4, 1030), -16, random(25, 50), random(1, 6)));
    iTime = millis();
  }
  
  for(int i=0; i < bullets.size(); i++){
    for(int j=0; j < enemies.size(); j++){
      float distance = dist(bullets.get(i).x, bullets.get(i).y, enemies.get(j).x+enemies.get(j).w/2, enemies.get(j).y+enemies.get(j).w/2);
      if(distance < enemies.get(j).w/2+9){
        bullets.remove(i);
        enemies.remove(j);
        score += 1;
        break;
      }
      
      else if(bullets.get(i).y+5 < 50){
        bullets.remove(i);
        break; 
      }
      
      else if(enemies.get(j).y+enemies.get(j).w > 688){
        enemies.remove(j);
        lives -= 1;
        break;
      }
    }
  }
  
  textSize(20);
  fill(0, 0, 100);
  text("Lives : "+lives, 5, 670);
  text("Score : "+score, 955, 670);
}
  

void gameover(){
  fill(0, 0, 100);
  textSize(150);
  text("GAME OVER", 100, 300);
  textSize(30);
  text("Press 'ENTER' to restart game", 330, 450);
  text("Your score : "+score, 430, 380);
  xplayer=500;
}

void keyPressed(){
  if(key=='a') keyAPressed=true;
  if(key=='z') keyZPressed=true;
  if(key==' ') bullets.add(new Bullet(xplayer+40, 650));
  if(key==ENTER) keySPressed=true;
}

void keyReleased(){
  if(key=='a') keyAPressed = false;
  if(key=='z') keyZPressed = false;
}

class Bullet{
  int x, y;
  
  Bullet(int x, int y){
    this.x=x;
    this.y=y;
  }
  
  void display(){
    fill(clr, 100, 100);
    ellipse(x, y, 10, 10);
  }
  
  void move(){
    y -= 15;
  }
}

class Enemy{
  float x, y, w, speed;
  
  Enemy(float x, int y, float w, float speed){
    this.x = x;
    this.y = y;
    this.w = w;
    this.speed = speed;
  }
  
  void display(){
    fill(clr, 100, 100);
    rect(x, y, w, w);
  }
  
  void move(){
    y += speed;
  }
}

```

thanks

---

<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:** [May 26, 2018, 2:17pm UTC](https://discourse.processing.org/t/arraylist-processing/410/2 "2018-05-26T14:17:41Z")

</div>

Your game seems to work. I do not know what is wrong with it.

Please describe in more detail what it is not doing that you wish it was doing.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 26, 2018, 11:01pm UTC](https://discourse.processing.org/t/arraylist-processing/410/3 "2018-05-26T23:01:10Z")

</div>

> [@Hedre](#):
>
> we must shoot to update the number of lives

This is snot clear, and it is key to your question. Please provide more details as mention by TfGuy44.

Kf

---

<div class="post-metadata">

**Author:** ![Hedre](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hedre](https://discourse.processing.org/u/Hedre)\
**Post date:** [May 27, 2018, 7:41am UTC](https://discourse.processing.org/t/arraylist-processing/410/4 "2018-05-27T07:41:48Z")

</div>

The number of lives shown on the bottom left is supposed to update as soon as an enemy reaches the bottom, but it only updates if you shoot

---

<div class="post-metadata">

**Author:** ![Zaiko](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@Zaiko](https://discourse.processing.org/u/Zaiko)\
**Post date:** [May 27, 2018, 7:41am UTC](https://discourse.processing.org/t/arraylist-processing/410/5 "2018-05-27T07:41:50Z")

</div>

I’m Hedre, I could not answer for the day because my account was blocked while waiting for it to be verified, but I’m in a hurry.  
The problem is that the number of lives displayed on the bottom left is only updated if you shoot (press space)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 27, 2018, 7:46am UTC](https://discourse.processing.org/t/arraylist-processing/410/6 "2018-05-27T07:46:48Z")

</div>

That is because you have a loop over your list of bullets:

```auto
for(int i=0; i < bullets.size(); i++){
}

```

…and for each bullet, it checks all the enemies  
…and for each enemy, it also checks this:

```auto
      else if(enemies.get(j).y+enemies.get(j).w > 688){
        enemies.remove(j);
        lives -= 1;
        break;
      }

```

So if there aren’t any bullets, this check won’t run. Try moving it outside the bullets loop, and loop over the enemies again, separately, after you are done bullet-checking.

```auto
for(int j=0; j < enemies.size(); j++){
  if(enemies.get(j).y+enemies.get(j).w > 688){
    enemies.remove(j);
    lives -= 1;
    break;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Hedre](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hedre](https://discourse.processing.org/u/Hedre)\
**Post date:** [May 27, 2018, 11:14am UTC](https://discourse.processing.org/t/arraylist-processing/410/7 "2018-05-27T11:14:33Z")

</div>

Oh yes, it works ! thank you 😉
