Need help with enemy and bullet collision in my little game

hi, so im making a shooting simple game in processing but I have no idea how to make my enemy disappear and then respawn after my bullets hit it.

this is my code:

int pixelsize = 4;
int gridsize  = pixelsize * 7 + 5;
Player player;
Enemy e;
ArrayList bullets = new ArrayList();
int direction = 1;


int score = 0;
PFont f;

void setup()
{
  size (600, 600);
  background(#A7A007);
  player = new Player();
  e = new Enemy();


  f = createFont("Arial", 20, true);
}

void draw()
{
  background(#3907A7);

  drawScore();

  player.draw();

  e.move();
  e.display();

  for (int i = 0; i < bullets.size(); i++)
  {
    Bullet bullet = (Bullet) bullets.get(i);
    bullet.draw();
  }

  
}

void drawScore()
{
  textFont(f);
  fill(#C9C5E8);
  text("Player Score: " + String.valueOf(score), 240, 20);
}

class Bullet
{
  int x, y;

  Bullet(int xpos, int ypos)
  {
    x = xpos + gridsize/2 - 4;
    y = ypos;
  }

  void draw()
  {
    fill(255);
    rect(x, y, pixelsize, pixelsize);
    y -= pixelsize * 2;
  }
}

class Enemy
{
  float EnemyX, EnemyY, EnemyW, EnemyH;
  float speedX, speedY, maxSpeed;
  
  boolean isAlive = true;
 



  Enemy() //constructor
  {
    EnemyX = 0;  //this makes the top LEFT corner in the middle of the screen when running.
    EnemyY = 100; //y position when respowning
    EnemyW = 50; //the width is 50
    EnemyH = 50; //the height is 50
    speedX = 2.5; //initialize the speed to 0 so it does not move when hit "run"
  }
  void move()
  {
    if (isAlive)
    {
    EnemyX += speedX;
    checkBounds();
    }
    
  
    
      
  }

  void checkBounds()
  {
    if (EnemyX > width)
    {
      EnemyX = -EnemyW;
    }
  }

  void display()  //making my enemy
  {

    noStroke(); //no stroke
    fill(#95E0B8); //pink but fancier
    rect(EnemyX, EnemyY, EnemyW, EnemyH);
 
  }
 
}

class SpaceShip
{
  int x, y;
  String ship[];
  color baseColor = color(#E095C2);
  color nextColor = baseColor;

  void draw()
  {
    updateObj();
    drawShip(x, y);
  }

  void drawShip(int xpos, int ypos)
  {
    fill(nextColor);

    nextColor = baseColor;

    for (int i = 0; i < ship.length; i++)
    {
      String row = (String) ship[i];

      for (int j = 0; j < row.length(); j++)
      {
        if (row.charAt(j) == '1')
        {
          rect(xpos+(j * pixelsize), ypos+(i * pixelsize), pixelsize, pixelsize);
        }
      }
    }
  }

  void updateObj()
  {
  }
}

class Player extends SpaceShip {
  boolean canShoot = true;
  int shootdelay = 0;

  Player() {
    x = width/gridsize/2;
    y = height - (10 * pixelsize);
    ship    = new String[5];
    ship[0] = "0010100";
    ship[1] = "0110110";
    ship[2] = "1111111";
    ship[3] = "1111111";
    ship[4] = "0111110";
  }

  void updateObj()
  {
    if (keyPressed && keyCode == LEFT)
    {
      x -= 5;
    }

    if (keyPressed && keyCode == RIGHT)
    {
      x += 5;
    }

    if (keyPressed && keyCode == UP)
    {
      y -= 5;
    }

    if (keyPressed && keyCode == DOWN)
    {
      y += 5;
    }

    if (keyPressed && key == ' ' && canShoot)
    {
      bullets.add(new Bullet(x, y));
      canShoot = false;
      shootdelay = 0;
    }

    shootdelay++;

    if (shootdelay >= 20)
    {
      canShoot = true;
    }
  }
}

Hi @butterjwab,

Welcome to the processing forum. Would you please read the following to format your code…

https://discourse.processing.org/faq#format-your-code

This makes it easier for us to help you.

Cheers
— mnse

1 Like

Hi sorry, I just started using this website and wasn’t being aware of the format. I’ve changed it.

int pixelsize = 4;
int gridsize  = pixelsize * 7 + 5;
Player player;
Enemy e;
ArrayList bullets = new ArrayList();
int direction = 1;


int score = 0;
PFont f;

void setup()
{
  size (600, 600);
  background(#A7A007);
  player = new Player();
  e = new Enemy();


  f = createFont("Arial", 20, true);
}

void draw()
{
  background(#3907A7);

  drawScore();

  player.draw();

  e.move();
  e.display();

  for (int i = 0; i < bullets.size(); i++)
  {
    Bullet bullet = (Bullet) bullets.get(i);
    bullet.draw();
  }

  
}

void drawScore()
{
  textFont(f);
  fill(#C9C5E8);
  text("Player Score: " + String.valueOf(score), 240, 20);
}

class Bullet
{
  int x, y;

  Bullet(int xpos, int ypos)
  {
    x = xpos + gridsize/2 - 4;
    y = ypos;
  }

  void draw()
  {
    fill(255);
    rect(x, y, pixelsize, pixelsize);
    y -= pixelsize * 2;
  }
}

class Enemy
{
  float EnemyX, EnemyY, EnemyW, EnemyH;
  float speedX, speedY, maxSpeed;
  
  boolean isAlive = true;
 



  Enemy() //constructor
  {
    EnemyX = 0;  //this makes the top LEFT corner in the middle of the screen when running.
    EnemyY = 100; //y position when respowning
    EnemyW = 50; //the width is 50
    EnemyH = 50; //the height is 50
    speedX = 2.5; //initialize the speed to 0 so it does not move when hit "run"
  }
  void move()
  {
    if (isAlive)
    {
    EnemyX += speedX;
    checkBounds();
    }
    
  
    
      
  }

  void checkBounds()
  {
    if (EnemyX > width)
    {
      EnemyX = -EnemyW;
    }
  }

  void display()  //making my enemy
  {

    noStroke(); //no stroke
    fill(#95E0B8); //pink but fancier
    rect(EnemyX, EnemyY, EnemyW, EnemyH);
 
  }
 
}

class SpaceShip
{
  int x, y;
  String ship[];
  color baseColor = color(#E095C2);
  color nextColor = baseColor;

  void draw()
  {
    updateObj();
    drawShip(x, y);
  }

  void drawShip(int xpos, int ypos)
  {
    fill(nextColor);

    nextColor = baseColor;

    for (int i = 0; i < ship.length; i++)
    {
      String row = (String) ship[i];

      for (int j = 0; j < row.length(); j++)
      {
        if (row.charAt(j) == '1')
        {
          rect(xpos+(j * pixelsize), ypos+(i * pixelsize), pixelsize, pixelsize);
        }
      }
    }
  }

  void updateObj()
  {
  }
}

class Player extends SpaceShip {
  boolean canShoot = true;
  int shootdelay = 0;

  Player() {
    x = width/gridsize/2;
    y = height - (10 * pixelsize);
    ship    = new String[5];
    ship[0] = "0010100";
    ship[1] = "0110110";
    ship[2] = "1111111";
    ship[3] = "1111111";
    ship[4] = "0111110";
  }

  void updateObj()
  {
    if (keyPressed && keyCode == LEFT)
    {
      x -= 5;
    }

    if (keyPressed && keyCode == RIGHT)
    {
      x += 5;
    }

    if (keyPressed && keyCode == UP)
    {
      y -= 5;
    }

    if (keyPressed && keyCode == DOWN)
    {
      y += 5;
    }

    if (keyPressed && key == ' ' && canShoot)
    {
      bullets.add(new Bullet(x, y));
      canShoot = false;
      shootdelay = 0;
    }

    shootdelay++;

    if (shootdelay >= 20)
    {
      canShoot = true;
    }
  }
}