ArrayList Processing

Hi

Firstly, I’m french, so my english is not so good :slight_smile: 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 :

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 :

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

1 Like

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.

1 Like

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

Kf

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

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)

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

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:

      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.

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

Oh yes, it works ! thank you :wink: