Need help with game project

sorry mate you completely lost me with this one :grin:

what is suposed to be inside the list.add ( ?? ) ?

a new Bullet

Arraylist is of type Bullet

hello im back here. I went up and down to learn about arraylist and managed to have my bullets and my asteroids as an arraylist, but i cant find a way to slow it down. In the current state it just creates as many bullets/asteroids as it wants. Im looking for a way to slow this down.

i tried adding an if (asteroids.size() <15) { asteroids.add…etc} but it will spawn 15 at a time and wait for them to get deleted to spawn others. id like to have them fall down constantly but at a slow pace if you know what i mean.

//GameMode File

//initializing classes
Timer t;
Player p;

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();


void setup (){
  
  //window setup
  size(800, 800);
  noCursor();
  background(0);
  smooth();
  
  //creating the classes in setup
  t= new Timer();
  p= new Player();
  
}

void draw (){
  background(0);
  
  //add the classes to draw
  t.run();
  p.run();
  bullets.add(new Bullet(new PVector(p.x, p.y)));
  asteroids.add(new Asteroid(new PVector(random(width), -10)));
  
  
  //adding asteroids to array backwords for delete
  for (int i = asteroids.size()-1; i >= 0; i--) {
    Asteroid a = asteroids.get(i);
    a.run();
    if (a.isDead()) {
      asteroids.remove(i);
    }
  }
  
  
  
  //adding bullets to array backwords for delete
  for (int i = bullets.size()-1; i >= 0; i--) {
    Bullet b = bullets.get(i);
    b.run();
    if (b.isDead()) {
      bullets.remove(i);
    }
  }
}

void keyPressed() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=true;
  } 
  if (key  == 'd') {
    p.keys[1]=true;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=true;
  }
  if (key  == 's') {
    p.keys[3]=true;
  }
}

void keyReleased() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=false;
  }
  if (key  == 'd') {
    p.keys[1]=false;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=false;
  }
  if (key  == 's') {
    p.keys[3]=false;
  }
}

//Asteroids File

class Asteroid {
  
  //class variables
  
  PVector location;
  PVector velocity;
  PVector acceleration;
  
  //float a; //shake speed
  //float noiseScale; //shake intensity
  //float sX; //shake on X axis
  
  //class constructor
  Asteroid(PVector l) {

      location= new PVector(random(width), 0);
      velocity= new PVector(random(-1, 1), random(-2, 2));
      acceleration= new PVector(0, 0.05);

      //a=0;
      //noiseScale=110;
  }
  
    
  //class functions   
  void run() { //functions container
    update();
    display();
    life();
    shoot(); 
  }
  
  
  void update() {
    
    velocity.add(acceleration);
    location.add(velocity);
    
    //drawing the shake
    //float sX = noise(a)*noiseScale;
    //a+= 0.1;
  }
  
  
  void display() {
    //circles color
    stroke(255);
    
    fill(#F70000);
    ellipse(location.x, location.y, 50, 50);
  }
  
  
  void life() {
    
    
  }
  
  void shoot() {
    
    
  }
  
  boolean isDead() {
    if (location.y > 800) {
      return true;
    } else {
      return false;
    }
  }
}
 
//Bullets File

class Bullet {
  
  //class variables
  
  PVector location;
  PVector velocity;
  
  
  
  
  //class constructor
  Bullet(PVector l) {
    
    velocity = new PVector(0, -30);
    location = l.copy();
  }

  
  
  //class functons
  
  void run() {  //functions container
    
    update();
    display();
  }
  
  
  
  void update() {
    
    location.add(velocity);
  }
  
 
  void display() {

    stroke(#17FF81);
    strokeWeight(5);
    fill(255);
    ellipse(location.x, location.y, 20, 20);  
  }
  
  boolean isDead() {
    if (location.y < 3) {
      return true;
    } else {
      return false;
    }
  }
}

//Player File

class Player {
  
  //class variables
  float x;
  float y;
  PShape t;
  boolean[] keys;

 
  
  
  //class constructor
  Player() {
    
    x= 0;
    y= 0;
    keys= new boolean[4];
    
  
    
    t = createShape();
    t.beginShape();
    t.fill(#FFF303);
    t.stroke(255);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+20);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-90, y+90); //left wing
    t.vertex(x-30, y+60); // left ass
    t.vertex(x+30, y+60); // right ass
    t.vertex(x+90, y+90); //right wing
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+20);
    t.endShape(CLOSE);
    
    x= width/2;
    y= height*0.8;
    
  }
  
  
  //class functions
  void run() { //function container
    display();
    shoot();
    life();
    movement();
    
    
  }
  
  
  
  void display() {
    
    pushMatrix();
    translate(x, y);
    
    fill(#03FFDB);
    stroke(#030CFF);
    strokeWeight(2);
    
    shape(t);
    
    popMatrix();
    
  }
  
  void shoot()  {
    
    
  }
  
  void life() {
    
    
  }
  
  //setting the distance value of wasd movement
  void movement() {
  
  if(keys[0]){
    x -= 15;
  }
  if(keys[1]){
    x += 15;
  }
  if(keys[2]){
    y -= 15;
  }
  if(keys[3]){
    y += 15;
  }
}
}

//Timer File

class Timer {
  
  //class variables
  int mstime = millis();  //giving the miliseconds a value
  int minutes;
  int seconds;
  float x; //x of the whole box
  float y; //y of the whole box
  
  //class constructor (defining)
  Timer() {
    x= width*0.003;
    y= height*0.003;
  }
  
  //class functions
  
  void run() {
    display();
  }
  
  
  void display() {
    
    fill(0);
    stroke(255);
    strokeWeight(1);
    rect(x,y, 93, 37);
  
  
  
  if ( millis() > mstime + 1000) 
  {
    mstime= millis();
    seconds = seconds +1;
  }
  
  if (seconds > 59)
  {
    minutes++;
    seconds = 0;
  }
  
  fill(255);
  textAlign(RIGHT);
  textSize(30);
  
  if( minutes <=9) {
    text("0", x+24, y+30);
  }
  text(minutes, x+44, y+30);
  
  if( seconds <=9) {
    text("0", x+69, y+30);
  }
  text(seconds, x+89, y+30);
  
  text( ":", x+51, y+28);
    
  }
}

i added all the code( even the irelevant one) so you can have a look. The important related stuff is in the first quarter of the whole thing.

im also trying to have the asteroids spawn with a random strokeweight each differently but they just change it with every frame. I plan to have the strokeweight as a “shield” as in: if strokewieght 3 and i shoot, stroke weight =2 and so on until if strokeweight=0 and i shoot i will kill the asteroid.
but obviously for this to work i need them to hold the weight untill they get destroyed.

you can add

if(frameCount%10 == 0)

or

if(frameCount%150 == 0)

before it

nice this works, thx. How exactly is this working?

also, where can i place the strokeweight for the asteroids so it doesnt change with every framerate, but they spawn with a random one each separately?

use the strokeWeight here

When you want to have an individual strokeWeight for each asteroid, you want to use a
variable “strokeWeightAsteroid” for the strokeWeight like strokeWeight(strokeWeightAsteroid);

Then before the constructor say

float strokeWeightAsteroid = random (2,12);

not tested

Chrisir

1 Like

I once more hit a dead and and turn here for help and guidance.

im now working on the collision and trying to get the asteroids detect being hit by the bullets (and eventually have them add points and disapear), and after this i’ll have to have the player object(which is a weird shape) detect if hit by the asteroids, which i have no ideea how i will make this work because of the weird shape.

But curently i cant even get the hit detection right for the asteroids and bullets( both ellipses).

//adding asteroids to array backwards for delete
  for (int i = asteroids.size()-1; i >= 0; i--) {
    Asteroid a = asteroids.get(i);
    a.run();
    if (a.isDead()) {
      asteroids.remove(i);
    }
    Bullet b = bullets.get(i);
    b.run();
    if (dist(a.location.x, a.location.y, b.location.x, b.location.y) < a.diam + b.diam) {
      println("hit");
      
    }
  }

where both asteroids and bullets have a float diam = 50; and float diam=10l in their class variables for the size. (as in elipse location.x, location.y, diam, diam) for both of them.

the way i curently did it i get an “IndexOutOfBoundsException: idex:7, size:7” error every time i run it 3-4 seconds in, but it detects the hits.

edit: i also tried: if (dist(asteroids.get(i).location.x, asteroids.get(i).location.y, bullets.get(i).location.x, bullets.get(i).location.y) < asteroids.get(i).diam + bullets.get(i).diam)

still getting this indexoutofboundsexception but it does detect hits.
@Chrisir

You need to check every bullet against every asteroid

This means have a for loop over asteroids and INSIDE it a for loop over bullets
One for loop with i, the other with j

i tried searching on how to do this but i didnt found anything related. Any chance you have time to show me how its done so i can take it from there?

I just explained how to do it

You are almost there


for i....
  Asteroid a= ... get(i);
   for j....
      Bullet b= .... get(j);
       If (dist
1 Like

I think that the 2 ArrayList don’t have the same length, so that’s why you get the exception

But also you need to have a nested for loop

I’ve managed to sort all that out and it works great, thank you @Chrisir.

Todays problem is when i am adding a wait from when my life gets to 0 and when i change the screenstate to the “you died” one. Because of how i did that wait, i get stuck in a constant you died screen even if i press to reset.
here is the piece of code related to this:

//mainfile
if (dist(a.location.x, a.location.y, p.x, p.y+40) < a.diam + p.diam-40){
      p.life--;
      asteroids.remove(i);
      if (p.life <1) {
        //screenState = 2;
        t.waiting=true;
        t.waitStartTime=millis();
      }
    }
//timer class
int waitStartTime;
int waitTime = 400;
void waits () {
    if (waiting) {
      if(millis() > (waitStartTime + waitTime)) {
        screenState=2;
      }
    }
  }

im trying to add this delay from when i get hit and life=0 and when the you died screen pops in so i can add some sort of death animation (probably a new class of squares fading and falling from player location), so i need a little delay to show it off.

I belive the problem is that if(millis() > (waitStartTime + waitTime)) and basically after the delay is done millis will still count up so it will always set my screenstate to 2(you died one). So how do i stop it so i can reset?

here is the whole thing just in case

//GameMode File

//initializing classes
Timer t;
Player p;

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();

int menuScreen=0, gameScreen=1, deadScreen=2;
int screenState= menuScreen;


void setup (){
  
  //window setup
  size(800, 800);
  noCursor();
  background(0);
  smooth();
  
  //creating the classes in setup
  t= new Timer();
  p= new Player();
  restart();
  
}

void draw() {
  if (screenState == menuScreen) {
    drawMenu();
  } else if (screenState == deadScreen) {
    drawDead();
  } else if (screenState == gameScreen) {
    drawGame();
  }
}


void drawGame () {
  screenState=1;
  
  background(0);
  
  //add the classes to draw
  t.run();
  p.run();
  
  if(frameCount%10==0){ //limiting the number of bullets/frame
    bullets.add(new Bullet(new PVector(p.x, p.y)));
  }
  
  if(frameCount%10==0){ //limiting the number of asteroids/frame
    asteroids.add(new Asteroid(new PVector(random(width), -20)));
  }
  
  //adding asteroids to array backwards for delete
  for (int i = asteroids.size()-1; i >= 0; i--) {
    Asteroid a = asteroids.get(i);
    a.run();
    
    if (dist(a.location.x, a.location.y, p.x, p.y+40) < a.diam + p.diam-40){
      p.life--;
      asteroids.remove(i);
      if (p.life <1) {
        //screenState = 2;
        t.waiting=true;
        t.waitStartTime=millis();
      }
    }
    
    
    //adding the bullets to the loop to check for hit detection
    for (int j = bullets.size()-1; j >= 0; j--) {
      Bullet b = bullets.get(j);
      
      //hit detection
      if (dist(a.location.x, a.location.y, b.location.x, b.location.y) < a.diam + b.diam) {
        
          a.life--;
          a.strokeWeightAsteroid--;
          p.points +=1;
          bullets.remove(j);
          
          if (a.life <1) {
            p.points +=3;
            asteroids.remove(i);
          }
      }
    }
    if (a.isDead()) {
      asteroids.remove(i);
    }
  }

  //adding bullets to array backwards for delete
  for (int j = bullets.size()-1; j >= 0; j--) {
    Bullet b = bullets.get(j);
    b.run();
    if (b.isDead()) {
      bullets.remove(j);
    }
  }
}

void keyPressed() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=true;
  } 
  if (key  == 'd') {
    p.keys[1]=true;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=true;
  }
  if (key  == 's') {
    p.keys[3]=true;
  }
  
  if (key == ' ') {
    
    if ( screenState == 0) {
      screenState = 1;
    }else if (screenState == 2) {
      restart();
    }
  }
}

void keyReleased() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=false;
  }
  if (key  == 'd') {
    p.keys[1]=false;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=false;
  }
  if (key  == 's') {
    p.keys[3]=false;
  }
}



void drawMenu () {
  screenState=0;
  
  background(0);
  textAlign(CENTER);
  fill(255, 0 ,0);
  textSize(60);
  text("Press Space to start", width/2, height/2);
  fill(255);
  textSize(30);
  text("Use WASD to move.", width/2, height/2+100);
  fill(255, 70);
  textSize(20);
  text("You have 3 lives!", width/2, height/2+300);
  text("Every 500 points you gain one more.", width/2, height/2+330);
  text("Be careful, some asteroids die hard!", width/2, height/2+360);
  
  
}

void drawDead () {
  screenState=2;
  
  background(0);
  fill(#FC0000);
  textAlign(CENTER);
  fill(255,0,0);
  textSize(90);
  text("You Died!", width/2, height/2);
  fill(255);
  textSize(50);
  text("You gained  "+ p.points +"  Points", width/2, height/2+80);
  fill(255,90);
  textSize(40);
  text("Press Space to restart", width/2, height/2+150);
  
}

void restart() {
  screenState=0;
  p.points=0;
  t.seconds=0;
  t.minutes=0;
  p.life=3;
}

//Asteroids File

class Asteroid {
  
  //class variables
  
  PVector location, velocity, acceleration;
 
  int strokeWeightAsteroid = (int)random(1,6);
  float diam=50;
  int life = strokeWeightAsteroid;
  
  
  //class constructor
  Asteroid(PVector l) {

      location= new PVector(random(width), -25);
      velocity= new PVector(random(-2, 2), random(0, 2));
      acceleration= new PVector(0.001, 0.09);
      
  }
  
    
  //class functions   
  void run() { //functions container
    update();
    display();
  }
  
  
  void update() {
    
    velocity.add(acceleration);
    location.add(velocity);
    
    if(strokeWeightAsteroid < 2) {
      stroke(#000000);
    }
    if(strokeWeightAsteroid < 3 && strokeWeightAsteroid>1) {
      stroke(#15FFF1);
    }
    if(strokeWeightAsteroid < 4 && strokeWeightAsteroid>2) {
      stroke(#00B1F0);
    }
    if(strokeWeightAsteroid < 5 && strokeWeightAsteroid>3) {
      stroke(#0081F0);
    }
    if(strokeWeightAsteroid < 6 && strokeWeightAsteroid>4) {
      stroke(#0021D1);
    }
    if(strokeWeightAsteroid < 7 && strokeWeightAsteroid>5) {
      stroke(#00026C);
    }
  }
  
  
  void display() {
    
    
    strokeWeight(strokeWeightAsteroid);
    fill(#B2B2B2);
    ellipse(location.x, location.y, diam, diam);
  }
  
  boolean isDead() {
    if (location.y > height) {
      return true;
    } else {
      return false;
    }
  }
}
 
//Bullets File

class Bullet {
  
  //class variables
  
  PVector location, velocity;
  float diam;
  
  
  
  
  //class constructor
  Bullet(PVector l) {
    
    velocity = new PVector(0, -10);
    location = l.copy();
    diam = 10;
  }

  
  
  //class functons
  
  void run() {  //functions container
    
    update();
    display();
  }
  
  
  
  void update() {
    
    location.add(velocity);
  }
  
 
  void display() {

    stroke(#17FF81);
    strokeWeight(5);
    fill(255);
    ellipse(location.x, location.y, diam, diam);  
  }
  
  boolean isDead() {
    if (location.y < 1) {
      return true;
    } else {
      return false;
    }
  }
}

//Player File

class Player {
  
  //class variables
  float x, y, curves, diam;
  PShape t;
  boolean[] keys;
  int life, points;

 
  
  
  //class constructor
  Player() {
    
    x= 0;
    y= 0;
    keys= new boolean[4];
    life=3;
    points=0;
    diam= 80;
    
  
    
    t = createShape();
    t.beginShape();
    t.fill(#FFF303);
    t.stroke(#0DFF80);
    t.strokeWeight(4);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+20);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-70, y+80); //left wing
    t.vertex(x-30, y+60); // left ass
    t.vertex(x+30, y+60); // right ass
    t.vertex(x+70, y+80); //right wing
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+20);
    t.endShape(CLOSE);
    
    
    
    x= width/2;
    y= height*0.8;
    
    
  }
  
  
  //class functions
  void run() { //function container
    display();
    points();
    life();
    movement();
    
    
  }
  
  
  
  void display() {
    
    pushMatrix();
    
      translate(x, y);
      shape(t);
     
    popMatrix();
    
  }
  
  void points()  {
    
    fill(255);
    textAlign(LEFT);
    textSize(20);
    text("Points:", width*0.03+60, height*0.03+35);
    text(points, width*0.03+125, height*0.03+35);
    
    
  }
  
  void life() {
    
    fill(255);
    textAlign(RIGHT);
    textSize(20);
    text("Lives:", width*0.03+30, height*0.03+35);
    text(life, width*0.03+50, height*0.03+35);
    
    
  }
  
  //setting the distance value of wasd movement
  void movement() {
    if(keys[0]){
      x -= 15;
    }
    if(keys[1]){
      x += 15;
    }
    if(keys[2]){
      y -= 15;
    }
    if(keys[3]){
      y += 15;
    }
  }
}

//Timer File

class Timer {
  
  //class variables
  int mstime = millis();  //giving the miliseconds a value
  int minutes, seconds;
  float x, y; //x and y of the whole box
  boolean waiting;
  int waitTime = 400;
  int waitStartTime;
  
  //class constructor (defining)
  Timer() {
    x= width*0.003;
    y= height*0.003;
  }
  
  //class functions
  
  void run() {
    display();
    waits();
  }
  
  
  void display() {
    
    fill(0);
    stroke(255);
    strokeWeight(1);
    rect(x,y, 93, 37);
    
    
    if ( millis() > mstime + 1000) {
      mstime= millis();
      seconds = seconds +1;
    }
    
    if (seconds > 59) {
      minutes++;
      seconds = 0;
    }
    fill(255);
    textAlign(LEFT);
    textSize(30);
    
    text(nf(minutes,2)+ ":" + (nf(seconds,2)), x+5, y+30); 
  }
  
  void waits () {
    if (waiting) {
      if(millis() > (waitStartTime + waitTime)) {
        screenState=2;
      }
    }
  }
}

set waiting to false when timer is up

t.waits() needs to be called from draw()

Why don’t you write

screenState=deadScreen;

here waiting = false;

1 Like

setting the waiting false worked.

the new problem im facing now is changing the color of the player stroke when i get hit by the asteroids.
And i tried 50 diferent ways and locations but it wont change. I’m asuming its because its a createShape and the stroke wont change i cant really understand why it wont change no matter what i try.

//playerfile
//class constructor
  Player() {
    
    x= 0;
    y= 0;
    keys= new boolean[4];
    life=3;
    points=0;
    diam= 80;
    hitBlip= #00FF63;
   
    
  
    t = createShape();
    t.beginShape();
    t.fill(#FFF303);
    t.stroke(hitBlip);
    t.strokeWeight(5);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+20);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-70, y+80); //left wing
    t.vertex(x-30, y+60); // left ass
    t.vertex(x+30, y+60); // right ass
    t.vertex(x+70, y+80); //right wing
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+20);
    t.endShape(CLOSE);
    
    
    if (hit){
      hitBlip = #ED0216;
    }
    
    x= width/2;
    y= height*0.8;
    
    
  }

//mainfile

if (dist(a.location.x, a.location.y, p.x, p.y+40) < a.diam + p.diam-40){
      p.life--;
      p.hit=true;
      asteroids.remove(i);
      if (p.life <1) {
        t.waiting=true;
        t.waitStartTime=millis();
      }
    }

im setting hit true when the hit is detected, i cant understand where i have to have that if to allow the color to change. I m trying to make it blip 3 times when hit is detected.

You define the PShape in the constructor of the class

When you display t it’s different

I think that you can say t.setFill(color(255,2,2));

cf.

It should be noted that unlike with fill() and stroke() you must pass a full color as an argument. i.e. instead of saying “setFill(255,0,0)” for a red fill, you’ll need to say “setFill(color(255,0,0))”. In addition, setFill() and setStroke() can take a boolean argument (e.g. setFill(false)) to turn the fill or stroke on or off for a given vertex as well as an integer (e.g. setFill(i,color(255,0,0))) to set the fill or stroke for a specific vertex.

from PShape / Processing.org

this means, you can make a variable playerColor and set it to a color.

Then you can say t.setFill(color(playerColor));

and when you have a hit say: playerColor = color (#00FF63);

im back on this after a little break, im trying to add a new arraylist to when the asteroids die from a bullet, but im not sure where exactly and how to add the arraylist into what i have now, its basically just a few circles falling off from that location and fading out disapearing.

//GameMode File

//initializing classes
Timer t;
Player p;

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
ArrayList<Star> stars = new ArrayList<Star>();
//ArrayList<Debris> debriss = new ArrayList<Debris>();

int menuScreen=0, gameScreen=1, deadScreen=2;
int screenState= menuScreen;


void setup (){
  
  //window setup
  fullScreen();
  noCursor();
  background(0);
  smooth();
  
  //creating the classes in setup
  t= new Timer();
  p= new Player();
  restart();
  
}

void draw() {
  if (screenState == menuScreen) {
    drawMenu();
  } else if (screenState == deadScreen) {
    drawDead();
  } else if (screenState == gameScreen) {
    drawGame();
  }
}


void drawGame () {
  screenState= gameScreen;
  
  background(0);
  
  //add the classes to draw
  t.run();
  p.run();
  
  if(frameCount%10==0){ //limiting the number of bullets/frame
    bullets.add(new Bullet(new PVector(p.x, p.y)));
  }
  
  if(frameCount%5==0){ //limiting the number of asteroids/frame
    asteroids.add(new Asteroid(new PVector(random(width), -20)));
  }
  
  if(frameCount%3==0){ //limiting the number of stars/frame
    stars.add(new Star(new PVector(random(width), -20)));
  }
  
  //adding asteroids to array backwards for delete
  for (int i = asteroids.size()-1; i >= 0; i--) {
    Asteroid a = asteroids.get(i);
    a.run();
    
    //asteroid-player collision
    if (dist(a.location.x, a.location.y, p.x, p.y+40) < a.diam + p.diam-40){
      p.life--;
      p.hit=true;
      t.hitWaiting=true;
      t.hitWaitStartTime=millis();
      asteroids.remove(i);
      
      if (p.life <1) {
        t.waiting=true;
        t.waitStartTime=millis();
      }
    }
    
    
    //adding the bullets to the loop to check for hit detection
    for (int j = bullets.size()-1; j >= 0; j--) {
      Bullet b = bullets.get(j);
      
      //asteroid-bullet collision
      if (dist(a.location.x, a.location.y, b.location.x, b.location.y) < a.diam + b.diam) {
        
          a.life--;
          a.strokeWeightAsteroid--;
          p.points++;
          bullets.remove(j);
          
          if (a.life <1) {
            p.points++;
            asteroids.remove(i);
          }
        }
      }
      if (a.isDead()) { 
        asteroids.remove(i);
      }
    }

  //adding bullets to array backwards for delete
  for (int j = bullets.size()-1; j >= 0; j--) {
    Bullet b = bullets.get(j);
    b.run();
    if (b.isDead()) {
      bullets.remove(j);
    }
  }
  
  
  //adding stars to array backwards for delete
  for (int l = stars.size()-1; l >= 0; l--) {
    Star s = stars.get(l);
    s.run();
    if (s.isDead()) {
      stars.remove(l);
    }
  }
}

void keyPressed() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=true;
  } 
  if (key  == 'd') {
    p.keys[1]=true;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=true;
  }
  if (key  == 's') {
    p.keys[3]=true;
  }
  
  if (key == ' ') {
    
    if ( screenState == menuScreen) {
      screenState = gameScreen;
    }else if (screenState == deadScreen) {
      restart();
    }
  }
}

void keyReleased() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=false;
  }
  if (key  == 'd') {
    p.keys[1]=false;
  }
  //move on y axis
  if (key == 'w') {
    p.keys[2]=false;
  }
  if (key  == 's') {
    p.keys[3]=false;
  }
}



void drawMenu () {
  screenState=menuScreen;
  
  background(0);
  textAlign(CENTER);
  fill(255, 0 ,0);
  textSize(60);
  text("Press Space to start", width/2, height/2);
  fill(255);
  textSize(30);
  text("Use WASD to move.", width/2, height/2+100);
  fill(255, 70);
  textSize(20);
  text("You have 3 lives!", width/2, height/2+300);
  text("Every 500 points you gain one more.", width/2, height/2+330);
  text("Be careful, some asteroids die hard!", width/2, height/2+360);
  
  
}

void drawDead () {
  screenState=deadScreen;
  
  background(0);
  fill(#FC0000);
  textAlign(CENTER);
  fill(255,0,0);
  textSize(90);
  text("You Died!", width/2, height/2);
  fill(255);
  textSize(50);
  text("You gained  "+ p.points +"  Points", width/2, height/2+80);
  fill(255,90);
  textSize(40);
  text("Press Space to restart", width/2, height/2+150);
  
}

void restart() {
  screenState=menuScreen;
  p.points=0;
  t.seconds=0;
  t.minutes=0;
  p.life=3;
  p.x=width/2;
  p.y=height*0.8;
}

//Debris File

class Debris {
  
  //class variables
  
  PVector location, velocity, acceleration;
 
  float diam=10;
  float life = 255;
  
  
  //class constructor
  Debris (PVector l) {

      location= l.copy();
      velocity= new PVector(random(-1, 1),random(-2, 2));
      acceleration= new PVector(0, 0.01);
    }
  
    
  //class functions   
  void run() { //functions container
    update();
    display();
  }
  
  
  void update() {
    
    velocity.add(acceleration);
    location.add(velocity);
    
    life -= 10;
  
  }
  
  
  void display() {
    
    stroke(255, life);
    strokeWeight( 2);
    fill(170, life);
    ellipse(location.x, location.y, diam, diam);
  }
  
  boolean isDead() {
    if ( life < 0) {
      return true;
    } else {
      return false;
    }
  }
}
 

basically i need to start creating the a debris when i remove the asteroid from bullet collision. kinda like a death animation.