Help with Spacewar Assignment

Greetings, Proceessing community.

I’m working on a Spacewar iteration assignment for my university which is due in 5 days by now. I ran into a problem where I’m attempting to declare a scoring system (in which for each bullet hit, it adds one score, and it must reach the targeted score, such as 20; if the first ship hits the enemy ship 20 times, the game ends) and a reaction to the ships getting hit by the opposing ship. However, I’m having trouble trying to fix these lines of code. Does anyone know how to solve the problem? I have my code displayed as of now:

Main File


int x;
int y;
int i = 0;
float angle = 0;

Ship  myShip;
Ship2 myShip2;
Starfield myStarfield = new Starfield(50);
ArrayList<Missile> missiles = new ArrayList<Missile>();

void setup() {
  size(500,500);
  
  x = width/2;
  y = height/2;
  myStarfield.StarfieldSetup(width, height);
  myShip = new Ship(x, y, 0);
  myShip2 = new Ship2(x, y, 0);
}

void draw() {
  background(10);
  
   myShip.move();
    myShip.show();
    
     myShip2.move();
    myShip2.show();
    
    myShip.getScore(20);
    myShip2.getScore(20);
    
    updateMissile();
    
    stroke(255);
    strokeWeight(5);
    i = 0;
    while(i < 10)
    {
      myStarfield.StarfieldDraw(i, width);
      i++;
    }
}

  
   void updateMissile(){
    for(int i = missiles.size()-1; i>= 0 ; i--){
      Missile missile = missiles.get(i);
      if(missile.offscreen()) {
          missiles.remove(missile);
        }
        else {
          missile.move();
          missile.show();
          
    }
    }
    }
    
    
void keyPressed()
{
   if(keyCode == LEFT)
    { 
    myShip.changeDirX(-1);
    } 
   else if(keyCode == RIGHT)
    { 
    myShip.changeDirX(1);
    }
   else if(keyCode == UP)
    {
     myShip.changeDirY(-1);
    }
   else if(keyCode == DOWN)
    { 
     myShip.changeDirY(1);
    }
       else if(keyCode == 76)  //L Key
    {
     myShip.turnAngle(-5);
    }
   else if(keyCode == 82)  // R Key
    { 
     myShip.turnAngle(5);
    }
    else if(keyCode == 32)  // Spacebar
    { 
      PVector mp = new PVector(0, 0);
      mp = myShip.currentPos();
      PVector ms = new PVector(0, 0) ;
      ms = myShip.currentSpeed();
      PVector ma = new PVector(0, 0) ;
      ma = myShip.currentSpeed();
      ma.div(2);
      
      missiles.add(new Missile(mp, ms, ma, 8));
    }
    if(keyCode == 65) // A Key
    { 
    myShip2.changeDirX(-1);
    } 
   else if(keyCode == 68) // D Key
    { 
    myShip2.changeDirX(1);
    }
   else if(keyCode == 87) // W Key
    {
     myShip2.changeDirY(-1);
    }
   else if(keyCode == 83) // S Key
    { 
     myShip2.changeDirY(1);
    }
       else if(keyCode == 81)  //Q Key
    {
     myShip2.turnAngle(-5);
    }
   else if(keyCode == 69)  // E Key
    { 
     myShip2.turnAngle(5);
    }
    else if(keyCode == 9)  // Tab
    { 
    PVector mp = new PVector(0, 0);
      mp = myShip2.currentPos();
      PVector ms = new PVector(0, 0) ;
      ms = myShip2.currentSpeed();
      PVector ma = new PVector(0, 0) ;
      ma = myShip2.currentSpeed();
      ma.div(2);
      
      missiles.add(new Missile(mp, ms, ma, 8));
}
}

Missile Class

class Missile{
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(0, 0);
  PVector accel = new PVector(0, 0);
  int prop;
  int radio;
  
  // Missile (PVector P_pos, PVector P_speed, int P_accel, int r) {  
  Missile (PVector P, PVector s, PVector a, int r) {  
    pos = P;
    speed = s;
    // speed.mult(5);
    accel = a;
    radio = r;
  }

  void move() {
    speed.add(accel);
    pos.add(speed);
  }

  void show() {
    noStroke();  
    ellipse(pos.x, pos.y, radio, radio);
    fill(106,67,15);
  }
  
  boolean offscreen(){
    boolean b = false;
    if (pos.x >= width || pos.y >= height || pos.x <= 0 || pos.y <= 0) {b = true; }
    return b;
  }
}

Ship Class

class Ship {
  PVector pos = new PVector(0,0);
  PVector speed = new PVector(1, 0);
  int angle = 0;
  int accel;
  int prop = (1);
  int score = 20;
  int relocate;
  int missile;
  
  
  Ship(int x, int y, int a){
    pos.x = x;
    pos.y = y;
    angle = a % 180;
    pos.setMag(width/2);
  }
  
  void relocate(){
    pos = PVector.random2D();
    pos.setMag(width/2);
  }
  
  void show(){
    stroke(0);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(angle));
    fill(237,255,0);
    rectMode(CENTER);
    triangle(-10,15,10,15,0,-15);
    rect(0,0,35,10);
    rect(20,10, 10,10);
    rect(-20,10, 10,10);
    popMatrix(); 
  }
  
  void getScore(){
    
  }
  
    void addScore() {
    int addScore = 20;
   
    updateMissile();
    if (missile.prop == 1) {
          if (myShip2.collisionBullet(missile.pos)) {  //This indicates the Ship reacting to getting hit by the opposing ship.
              myShip.addScore();
              score ++;
              myShip2.relocate();
             missile.remove(missile);
             // If Ship2 gets hit by Ship, a score is added for Ship.
          }
    }
}
  
  void move(){
    speed.limit(4);
    pos.add(speed);
    
    pos.x = pos.x % width;
    pos.y = pos.y % height;
  }
  
  void changeDirX(int v){
    speed.x = speed.x + v;  
  }
  
  void changeDirY(int v){
    speed.y = speed.y + v;  
  }
  
  PVector currentPos () {
     PVector cp = new PVector(pos.x, pos.y);
     return cp;
  }
  
  PVector currentSpeed () {
    PVector cs = new PVector(speed.x, speed.y);
   return cs;
  }

  void turnAngle(int a){
    angle = angle + (a % 180) ;
  }

boolean collisionBullet(PVector P_pos){
  
  float dist = PVector.dist(P_pos, pos);
  return(dist < 15);
}
    }

//void printScore(){
//  for (int = 0; i< score; i+)

Ship2 Class

class Ship2 {
  PVector pos = new PVector(0,0);
  PVector speed = new PVector(1, 0);
  int angle = 0;
  int accel;
  int prop = (2);
  int score = 20;
  PVector collisionMissile;
  PVector relocate;
  PVector missile;
  
  
  Ship2(int x, int y, int a){
    pos.x = x;
    pos.y = y;
    angle = a % 180;
    pos.setMag(width/2);
  }
  
  void show(){
    stroke(0);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(angle));
    fill(137,255,0);
    rectMode(CENTER);
    triangle(-10,15,10,15,0,-15);
    rect(0,0,35,10);
    rect(20,10, 10,10);
    rect(-20,10, 10,10);
    popMatrix();
    
  }
  
  void addScore() {
    
    int addScore = 20;

   updateMissile();
  if (missile.prop == 2)
          if (myShip.collisionBullet(missile.pos));
          myShip2.addScore();
          score ++;
           myShip.relocate();
          missile.remove(missile);
          // If Ship gets hit by Ship2, a score is added for Ship2.
        }
  
  void move(){
    speed.limit(4);
    pos.add(speed);
    
    pos.x = pos.x % width;
    pos.y = pos.y % height;
  }
  
  void changeDirX(int v){
    speed.x = speed.x + v;  
  }
  
  void changeDirY(int v){
    speed.y = speed.y + v;  
  }
  
  PVector currentPos () {
     PVector cp = new PVector(pos.x, pos.y);
     return cp;
  }
  
  PVector currentSpeed () {
    PVector cs = new PVector(speed.x, speed.y);
   return cs;
  }

  void turnAngle(int a){
    angle = angle + (a % 180) ;
  }
  
  boolean collisionBullet(PVector P_pos){
  
  float dist = PVector.dist(P_pos, pos);
  return(dist < 15);
}
    }

void collisionMissile() {
  
}

I tried my best on solving the issue by looking at previous examples, but they didn’t help. I even tried to declare some of the errors displayed on the code. I am currently declaring collisionMissile() and getScore() in my code.The errors on my current attempt at making a scoreboard will delay the rest of the goals I have to complete until they are fixed somehow. If any knows how to solve this, let me know. I will appreciate any form of help that can fit in with my project accordingly.

-Christian

Edit your post, select your code, and hit the </> button to format it, please.

Without proper formatting, it’s a real pain for us to look at, let alone work with.

Already did. Thanks for the reminder.

Even formatted, that is not a runnable example. But this is:

int x;
int y;
int i = 0;
float angle = 0;

Ship myShip;
Ship2 myShip2;
//Starfield myStarfield = new Starfield(50);
ArrayList missiles = new ArrayList();

void setup() {
  size(500, 500);

  x = width/2;
  y = height/2;
  //myStarfield.StarfieldSetup(width, height);
  myShip = new Ship(x, y, 0);
  myShip2 = new Ship2(x, y, 0);
}

void draw() {
  background(128);

  myShip.move();
  myShip.show();

  myShip2.move();
  myShip2.show();

  myShip.getScore();//20);
  //myShip2.getScore();//20);

  updateMissile();

  stroke(255);
  strokeWeight(5);
  i = 0;
  //while (i < 10)
  //{
    //myStarfield.StarfieldDraw(i, width);
    //i++;
  //}
}

void updateMissile() {
  for (int i = missiles.size()-1; i>= 0; i--){
    Missile missile = (Missile)missiles.get(i);
    if (missile.offscreen()) {
      missiles.remove(missile);
    } else {
      missile.move();
      missile.show();
    }
  }
}

void keyPressed()
{
  if (keyCode == LEFT)
  {
    myShip.changeDirX(-1);
  } else if (keyCode == RIGHT)
  {
    myShip.changeDirX(1);
  } else if (keyCode == UP)
  {
    myShip.changeDirY(-1);
  } else if (keyCode == DOWN)
  {
    myShip.changeDirY(1);
  } else if (keyCode == 76) //L Key
  {
    myShip.turnAngle(-5);
  } else if (keyCode == 82) // R Key
  {
    myShip.turnAngle(5);
  } else if (keyCode == 32) // Spacebar
  {
    PVector mp = new PVector(0, 0);
    mp = myShip.currentPos();
    PVector ms = new PVector(0, 0) ;
    ms = myShip.currentSpeed();
    PVector ma = new PVector(0, 0) ;
    ma = myShip.currentSpeed();
    ma.div(2);

    missiles.add(new Missile(mp, ms, ma, 8));
  }
  if (keyCode == 65) // A Key
  { 
    myShip2.changeDirX(-1);
  } else if (keyCode == 68) // D Key
  {
    myShip2.changeDirX(1);
  } else if (keyCode == 87) // W Key
  {
    myShip2.changeDirY(-1);
  } else if (keyCode == 83) // S Key
  {
    myShip2.changeDirY(1);
  } else if (keyCode == 81) //Q Key
  {
    myShip2.turnAngle(-5);
  } else if (keyCode == 69) // E Key
  {
    myShip2.turnAngle(5);
  } else if (keyCode == 9) // Tab
  {
    PVector mp = new PVector(0, 0);
    mp = myShip2.currentPos();
    PVector ms = new PVector(0, 0) ;
    ms = myShip2.currentSpeed();
    PVector ma = new PVector(0, 0) ;
    ma = myShip2.currentSpeed();
    ma.div(2);

    missiles.add(new Missile(mp, ms, ma, 8));
  }
}



class Missile {
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(0, 0);
  PVector accel = new PVector(0, 0);
  int prop;
  int radio;

  // Missile (PVector P_pos, PVector P_speed, int P_accel, int r) {
  Missile (PVector P, PVector s, PVector a, int r) {
    pos = P;
    speed = s;
    // speed.mult(5);
    accel = a;
    radio = r;
  }

  void move() {
    speed.add(accel);
    pos.add(speed);
  }

  void show() {
    noStroke();
    ellipse(pos.x, pos.y, radio, radio);
    fill(106, 67, 15);
  }

  boolean offscreen() {
    boolean b = false;
    if (pos.x >= width || pos.y >= height || pos.x <= 0 || pos.y <= 0) {
      b = true;
    }
    return b;
  }
}

class Ship {
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(1, 0);
  int angle = 0;
  int accel;
  int prop = (1);
  int score = 20;
  int relocate;
  int missile;

  Ship(int x, int y, int a) {
    pos.x = x;
    pos.y = y;
    angle = a % 180;
    pos.setMag(width/2);
  }

  void relocate() {
    pos = PVector.random2D();
    pos.setMag(width/2);
  }

  void show() {
    stroke(0);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(angle));
    fill(237, 255, 0);
    rectMode(CENTER);
    triangle(-10, 15, 10, 15, 0, -15);
    rect(0, 0, 35, 10);
    rect(20, 10, 10, 10);
    rect(-20, 10, 10, 10);
    popMatrix();
  }

  void getScore() {
  }

  void addScore() {
    int addScore = 20;

    updateMissile();
    /*
    if (missile.prop == 1) {
      if (myShip2.collisionBullet(missile.pos)) {  //This indicates the Ship reacting to getting hit by the opposing ship.
        myShip.addScore();
        score ++;
        myShip2.relocate();
        missile.remove(missile);
        // If Ship2 gets hit by Ship, a score is added for Ship.
      }
    }
    */ // Unclear what missile we're dealing with here...
  }

  void move() {
    speed.limit(4);
    pos.add(speed);

    pos.x = pos.x % width;
    pos.y = pos.y % height;
  }

  void changeDirX(int v) {
    speed.x = speed.x + v;
  }

  void changeDirY(int v) {
    speed.y = speed.y + v;
  }

  PVector currentPos () {
    PVector cp = new PVector(pos.x, pos.y);
    return cp;
  }

  PVector currentSpeed () {
    PVector cs = new PVector(speed.x, speed.y);
    return cs;
  }

  void turnAngle(int a) {
    angle = angle + (a % 180) ;
  }

  boolean collisionBullet(PVector P_pos) {

    float dist = PVector.dist(P_pos, pos);
    return(dist < 15);
  }
}

//void printScore(){
// for (int = 0; i< score; i+)

class Ship2 {
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(1, 0);
  int angle = 0;
  int accel;
  int prop = (2);
  int score = 20;
  PVector collisionMissile;
  PVector relocate;
  PVector missile;

  Ship2(int x, int y, int a) {
    pos.x = x;
    pos.y = y;
    angle = a % 180;
    pos.setMag(width/2);
  }

  void show() {
    stroke(0);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(angle));
    fill(137, 255, 0);
    rectMode(CENTER);
    triangle(-10, 15, 10, 15, 0, -15);
    rect(0, 0, 35, 10);
    rect(20, 10, 10, 10);
    rect(-20, 10, 10, 10);
    popMatrix();
  }

  void addScore() {

    int addScore = 20;
    updateMissile();
    /*
    if (missile.prop == 2)
      if (myShip.collisionBullet(missile.pos));
    myShip2.addScore();
    score ++;
    myShip.relocate();
    missile.remove(missile);
    */ // Again, what missile?
    // If Ship gets hit by Ship2, a score is added for Ship2.
  }

  void move() {
    speed.limit(4);
    pos.add(speed);

    pos.x = pos.x % width;
    pos.y = pos.y % height;
  }

  void changeDirX(int v) {
    speed.x = speed.x + v;
  }

  void changeDirY(int v) {
    speed.y = speed.y + v;
  }

  PVector currentPos () {
    PVector cp = new PVector(pos.x, pos.y);
    return cp;
  }

  PVector currentSpeed () {
    PVector cs = new PVector(speed.x, speed.y);
    return cs;
  }

  void turnAngle(int a) {
    angle = angle + (a % 180) ;
  }

  boolean collisionBullet(PVector P_pos) {

    float dist = PVector.dist(P_pos, pos);
    return(dist < 15);
  }
}

void collisionMissile() {
}

I had to rip out the star field because you didn’t give us that class, and there were a few sections I had to comment out and there was a loop that wasn’t doing i-- properly. Now that the effort to make your code runnable has been put in, I can start putting in some effort to see what’s wrong with it.

That’s okay. Don’t worry. I’m new to the forums, so It’s going to take some time for me to adapt. Thank you for your input!

My main, immediate concern is that you have a Ship class, and then a Ship2 class, which are basically the same. The point of using a class is that very similar objects can share the same class. Essentially, the class is the blueprint for making one of those objects. If you want to build two identical houses (that differ only in which color the front door is), you don’t need a different set of blueprints for each house.

I ran your code through the cleaner and the Ship2 class vanished.

Ship myShip;
Ship myShip2;
ArrayList<Missile> missiles = new ArrayList();

void setup() {
  size(500, 500);
  myShip = new Ship(width/2, height/2, 0, 0);
  myShip2 = new Ship(width/2, height/2, 0, 1);
}

void draw() {
  background(128);
  // Starfield here.
  // TODO.
  // Move and show missiles.
  updateMissile();
  // Move and show first ship.
  myShip.move();
  myShip.show();
  // move and show second ship.
  myShip2.move();
  myShip2.show();
}

void updateMissile() {
  for (int i = missiles.size()-1; i>= 0; i--) {
    Missile missile = (Missile)missiles.get(i);
    if (missile.offscreen()) {
      missiles.remove(missile);
    } else {
      missile.move();
      missile.show();
    }
  }
}

void keyPressed() {
  // First ship controls.
  if (keyCode == LEFT)  myShip.changeDirX(-1);
  if (keyCode == RIGHT) myShip.changeDirX(1);
  if (keyCode == UP)    myShip.changeDirY(-1);
  if (keyCode == DOWN)  myShip.changeDirY(1);
  if (keyCode == 'L')   myShip.turnAngle(-5);
  if (keyCode == 'R')   myShip.turnAngle(5);
  if (keyCode == ' ')   myShip.shoot();
  // Second ship controls.  
  if (keyCode == 'A')   myShip2.changeDirX(-1);
  if (keyCode == 'D')   myShip2.changeDirX(1);
  if (keyCode == 'W')   myShip2.changeDirY(-1);
  if (keyCode == 'S')   myShip2.changeDirY(1);
  if (keyCode == 'Q')   myShip2.turnAngle(-5);
  if (keyCode == 'E')   myShip2.turnAngle(5);
  if (keyCode == TAB)   myShip2.shoot();
}

// --- MISSILE CLASS

class Missile {
  
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(0, 0);
  PVector accel = new PVector(0, 0);
  int prop;
  int radio;

  Missile (PVector p, PVector v, PVector a, int r) {
    pos = p;
    speed = v;
    // speed.mult(5);
    accel = a;
    radio = r;
  }

  void move() {
    speed.add(accel);
    pos.add(speed);
  }

  void show() {
    noStroke();
    fill(106, 67, 15);
    ellipse(pos.x, pos.y, radio, radio);
  }

  boolean offscreen() {
    return (pos.x >= width || pos.y >= height || pos.x <= 0 || pos.y <= 0);
  }
  
} // End of Missile class.

// --- SHIP CLASS

class Ship {
  
  PVector pos = new PVector(0, 0);
  PVector speed = new PVector(1, 0);
  int angle = 0;
  int accel;
  int prop = (1);
  int score = 20;
  int relocate;
  int missile;
  int id;

  Ship(int x, int y, int a, int iid) {
    pos.x = x;
    pos.y = y;
    angle = a % 180;
    pos.setMag(width/2);
    id = iid;
  }

  void relocate() {
    pos = PVector.random2D();
    pos.setMag(width/2);
  }

  void show() {
    stroke(0);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(radians(angle));
    if ( id == 0 ) {
      fill(237, 255, 0);
    } else {
      fill(137, 255, 0);
    }
    rectMode(CENTER);
    triangle(-10, 15, 10, 15, 0, -15);
    rect(0, 0, 35, 10);
    rect(20, 10, 10, 10);
    rect(-20, 10, 10, 10);
    popMatrix();
  }

  void move() {
    speed.limit(4);
    pos.add(speed);
    pos.x = pos.x % width;
    pos.y = pos.y % height;
  }

  void changeDirX(int v) {
    speed.x = speed.x + v;
  }

  void changeDirY(int v) {
    speed.y = speed.y + v;
  }

  void turnAngle(int a) {
    angle = angle + (a % 180) ;
  }

  void shoot() {
    missiles.add(new Missile(pos, speed, speed.div(2), 8));
  }
  
} // End of Ship class.

Also notice that I moved the logic to add a new Missile into the Ship class. Now a Ship can just shoot()! Since the Ship is tracking its position and speed, it can pass that information on to the new Missile without needing GetPosition() and GetSpeed() functions.

I see. I added the Ship and Ship2 classes to make two separate ships. They are not identical in terms of appearance since they varied in color and keyword commands, such as Ship2 moving with WASD instead of Ship moving with just the arrow keys, and so on. I did try to copy my example of the problem that I’m facing, but to no avail.

As you can see above, the easiest way to differentiate the ships is just to give each an id - like a house number. House 0 has a yellow door. House 1 has the green door. Except they are Ships, not houses.

Mhm…
Do I have to declare both ships in one Ship class? How?

No. The Ship class defines what a ship is. It’s a new TYPE of thing. When you write a class, you’re saying “This is what a Ship is.”

You can have two objects that are that type of thing. “This thing is a Ship. This other thing is also a Ship.”

The id variable allows for minor differences between the two. When the first ship is created, “This is a Ship, and it’s id is 0.” The second: “This is a Ship, and its id is 1.”

The class defines that ships with id 0 are different - in color - than other ships:

if ( id == 0 ) {
  fill(237, 255, 0);
} else {
  fill(137, 255, 0);
}

Anyway, your question was about collision with missiles.

You will want to check, every frame, if any missile has collided with a ship.

// In draw(), because we want to do this every frame,
  // Look at each missile in order (that means have a loop),
    // If the missile we're looking at right now is hitting a ship,
      // Then the other ship gets some points.
    // That's all we do if a missile is hitting.
  // That's all we do for this missile.
// That's all we have to do to deal with missiles colliding.

There are going to be some problems, of course. Since each missile starts on top of the ship that shot it, it’s immediately going to collide with that ship. Basically, you’re firing on your own ship! ou’ll need to add some more logic to deal with this.

// In draw(), because we want to do this every frame,
  // Look at each missile in order (that means have a loop),
    // If the missile we're looking at right now is hitting a ship THAT DIDN'T FIRE THIS MISSILE
      // Then the other ship gets some points.
    // That's all we do if a missile is hitting.
  // That's all we do for this missile.
// That's all we have to do to deal with missiles colliding.

This means that each missile is going to need to remember which ship shot it. Conveniently, each ship has a unique id variable now. Could you add an id variable to the missile class, to record which ship fired it, and use that id when checking missile collisions?

Alright, I see. How should I pull it off?
Is the first id example where the class that defines the ship with ids a step to make 2 ships exist in one class?

In that case, I may or may not rewrite my code to accommodate the sole Ship class, but I should try to change the WASD and Tab keys (the keys I assigned to Ship2) to the second id that identifies the second ship. I’ll see how I can pull it off, but is this optional or required for my Spacewar iteration to work?

Take the full, working example I posted last (Help with Spacewar Assignment) and try adding missile collision to it. Post the code of your attempt.

I’ll see what I can do. Thank you. I’ll let you know.

Here’s what I made so far; I had to re-add my Starfield class since it’s required for my final project. But here’s what I made so far:

int x;
int y;
int i = 0;
float angle = 0;

Ship myShip;
Ship myShip2;
Starfield myStarfield = new Starfield(50);
ArrayList<Missile> missiles = new ArrayList();

void setup() {
  size(500, 500);
   x = width/2;
  y = height/2;
  myShip = new Ship(width/2, height/2, 0, 0);
  myShip2 = new Ship(width/2, height/2, 0, 1);
  myStarfield.StarfieldSetup(width, height);
}

void draw() {
  background(128);
  
  stroke(255);
    strokeWeight(5);
    i = 0;
    while(i < 10)
    {
      myStarfield.StarfieldDraw(i, width);
      i++;
    }
}

{
  // TODO.
  // Move and show missiles.
  updateMissile();
  // Move and show first ship.
  myShip.move();
  myShip.show();
  // move and show second ship.
  myShip2.move();
  myShip2.show();
  }
  
void updateMissile() {
  for (int i = missiles.size()-1; i>= 0; i--) {
    Missile missile = (Missile)missiles.get(i);
    if (missile.offscreen()) {
      missiles.remove(missile);
    } else {
      missile.move();
      missile.show();
    }
  }
}

void keyPressed() {
  // First ship controls.
  if (keyCode == LEFT)  myShip.changeDirX(-1);
  if (keyCode == RIGHT) myShip.changeDirX(1);
  if (keyCode == UP)    myShip.changeDirY(-1);
  if (keyCode == DOWN)  myShip.changeDirY(1);
  if (keyCode == 'L')   myShip.turnAngle(-5);
  if (keyCode == 'R')   myShip.turnAngle(5);
  if (keyCode == ' ')   myShip.shoot();
  // Second ship controls.  
  if (keyCode == 'A')   myShip2.changeDirX(-1);
  if (keyCode == 'D')   myShip2.changeDirX(1);
  if (keyCode == 'W')   myShip2.changeDirY(-1);
  if (keyCode == 'S')   myShip2.changeDirY(1);
  if (keyCode == 'Q')   myShip2.turnAngle(-5);
  if (keyCode == 'E')   myShip2.turnAngle(5);
  if (keyCode == TAB)   myShip2.shoot();
}

However, when I run the program, the Ship’s speeds accelerate when I press the key command that causes them to shoot rather than keeping their speed. We haven’t proceeded to the scoreboard and missile reactions yet. But we’ll work on it eventually.

I also need a bit of help on how I’m going to make those ships react to getting hit in terms of collision and have a score added for which ship caused the other ship to react to the collision. I defined prop, which is the property of the missile where it decided who it belongs to. However, it gives me an error: the primitive type int of missile does not have a filed prop. What should I do in this case? How do I define the prop? The code also gives me another error: cannot invoke remove(int) on the primitive type int.

Here’s the code for reference:

  void getScore(){
    
  }
  
    void addScore() {
    int addScore = 20;
   
    updateMissile();
    if (missile.prop == Ship1) {
          if (myShip2.collisionBullet(missile.pos)) {  //This indicates the Ship reacting to getting hit by the opposing ship.
              myShip1.addScore();
              score ++;
              myShip2.relocate();
             missile.remove(missile);
             // If Ship2 gets hit by Ship, a score is added for Ship.
          }
    }
}

What is missile? Is it an ArrayList of Missiles? If so, what a terrible name for that variable!

Is it a single missile? If so, where is is defined? Shouldn’t you be looping over all the missiles?

Go back and look at the plain-words explanation of the process you’ll need to follow to deal with missiles colliding. Each line of that explanation translate directly into code!

If you’re going to have an attitude while trying to help someone with a processing issue, then maybe troubleshooting isn’t for you.

To fix the ‘prop’ error add a parameter to the Missile class’ contructor:

Missile (PVector p, PVector v, PVector a, int r, int id) {
  prop = id;
  ...
}

Then inside shoot():

missiles.add(new Missile(pos, speed, speed.div(2), 8), id); // added ship's id

I recommend changing updateMissile() to include the collision test as it already loops through each missile object.
As TfGuy outlined, compare the owner of each missile as well as location to decide where the points go.