Need help making a colission

``I'm having trouble detecting the bullet collide with the enemy ship in my shooter game. Here's my code so far. Any ideas on how I can make it work?

PImage background;
ship player= new ship();

int score;
PImage enemypic;
boolean enemyAlive = true;
boolean instructions = true;
boolean level1=true;
boolean gameOver=false;
enemy enemyArr [] = new enemy [11];
color rectColor = color (200);
ArrayList <Bullet> bullets;
void setup() {
size(1000, 750);
bullets = new ArrayList();
background=loadImage("shooterbackground.png");
ship=loadImage("ship.png");
enemypic = loadImage("enemy.png");
frameRate(13);
smooth();
noStroke();
float enemyY = 0;
for (int i = 0; i < 11; i++) {

 enemyArr [i] = new enemy(900, 5 + enemyY);
enemyY +=65;
}
}
void draw() {
  println(frameRate);
image (background, 0, 0, 1000, 750); 

moveAll();
displayAll();
if (instructions) {
 fill(rectColor);
//rect(0, 0, 1000, 750);
fill(0);
text("afdsf", 0, 0);
          }
      if (instructions) {
background(200, 200, 200);
fill(225);
rect(300, 275, 400, 200);
fill(0);
textSize(65);
text("Start", 420, 390);
         }

       if (enemyAlive && !instructions && !gameOver) {

   player.display();
for (int i = 0; i < 11; i++) {
  enemyArr[i].update();
  enemyArr[i].display();
}
}

if (playerX > 244) {//player move limits
playerX = 244;
}
if (playerX < 0) {
playerX = 0;
}
if (playerY < -68) {//wrap screen
playerY = 740;
}
if (playerY > 740) {
playerY = -68;
}
}  


void keyPressed() {
 if (key == 'a') {
  playerX -= 8;
 }
  if (key == 'd') {
  playerX+= 8;
 }
  if (key == 'w') {
  playerY -= 8;
 }
  if (key == 's') {
    playerY += 8;
   }
  }




   void mousePressed() {

   if (mouseX > 300 && mouseX < 700 &&  mouseY > 275 && mouseY  < 475 && instructions) {
    instructions = false;
   }
   Bullet temp = new Bullet(playerX+65, playerY+34);
   bullets.add(temp);
   }



public class enemy {
  float health, speed;
  float yPos, xPos;


   enemy(float XPOS, float YPOS) {

    health=5;
     speed=10;
    xPos=XPOS;
    yPos=YPOS;
  }

  public void display() {
    image(enemypic, xPos, yPos, 75, 75);
   }
   public void update() {
     if (enemyAlive) {
     xPos -=7;
        }
   if (xPos < -65) {
    xPos=925;
      }
            }
         }

     class Bullet{//bullet class

       float x=playerX +20;
      float y=playerY;
     float speed;
    Bullet(float tx, float ty){

        x = tx;
       y = ty;
     }
       void display(){

     fill(255, 0,0);
        rect(x,y, 10, 5);
      }
       void move(){

        x += 25;
      }

     void moveAll(){

     for (Bullet temp : bullets){

       temp.move();
        }
       }
       void displayAll(){

       for (Bullet temp : bullets){

           temp.display();
      }
       }

 PImage ship;
    int playerX = 100;
    int playerY = 300;

     public class ship {

 public void display() {
image(ship, playerX, playerY, 75, 75);
        }
    }

Answers

Sign In or Register to comment.