import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class connor_ extends PApplet {


Player spaceship; //made a new player

ArrayList<Enemy> enemies = new ArrayList<Enemy>(); // set of enemy

int numEnemy = 25;
int score;

public void setup() {
  

score = 0;

  spaceship= new Player(800, 800); 
  for (int i = 0; i < numEnemy; i++) {
    enemies.add(new Enemy((int)random(0, width), (int)random(-200, 0)));
  }
}


public void draw() {
  background(142, 7, 7);

if (spaceship.lives == 0) {
 fill(139, 139, 139);
  rect(0, 0, width, height);
  fill(0);
  text("GAME OVER", width / 2, height / 2);
 noLoop(); 
}

  spaceship.update();


  for (int i = 0; i < enemies.size(); i++) {
    Enemy e = enemies.get(i);
    e.update();
  }
  
if (enemies.size() < numEnemy) {
respawn();
}

text("lives  " + spaceship.lives,50,50);
text("score",50,70);
}//end of draw

  public void keyPressed() {
    if (key == ' ') {
      spaceship.fire();
    
    }
  }
  public void respawn() {
     enemies.add(new Enemy((int)random(0, width), (int)random(-200, 0)));
}
class Bullet {
  //variables
  int x;
  int y;
 int bHeight;
 int bWidth;
  
  Bullet(int xPos, int yPos) {
   
    this.x = xPos;
    this.y = yPos;
    this.bWidth =25;
    this.bHeight = 25;
}


  public void update() {
    draw();
    move();
  }

  public void draw() {
    fill(192, 218, 219);
    ellipse(x, y, bWidth,bHeight);
  }

  public void move() {
    y = y - 5;
  }

 public boolean hit(Enemy e) {
   return abs(x - e.x) < (bWidth + e.eWidth) / 2 && abs(y - e.y) < (bHeight + e.eHeight) /1;
 }
   
}//end of the class
class Enemy {
  //variables
  int x;
  int y;
  int eWidth;
  int eHeight;
  Enemy(int xPos, int yPos) {//constructor 
    this.x = xPos;
    this.y = yPos;
    this.eWidth = 50;
    this.eHeight = 51;

}

  public void draw () {
    fill (10, 252, 8);
    rect(x, y, 50, 51);
  }
public void update() {
  draw();
  move();
}
  public void move() {
    y = y + 8; //move downwards
  
  if (y > height) {
 y =  (int)random(-2000,-1000);
}
}
}
class Player { //class
  int x;
  int y;
  int pHeight;
  int pWidth;
  int lives;


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

  Player(int xPos, int yPos) {//constructor
    this.x = xPos;
    this.y = yPos;
    this.pWidth = 100;
    this.pHeight = 100;
    this.lives = 3;
  }

  public void draw() {
    fill(76, 154, 234);
    rect(x, y, 50, 50);
  }

  public void fire() {
    bullets.add(new Bullet(x + 25, y));
  }

  public void hit() {
    for (int i = 0; i < enemies.size(); i++) {
      Enemy e = enemies.get(i);
      if (abs(x - e.x) < (pWidth + e.eWidth)/2 && abs(y - e.y) < (pHeight + e.eHeight)/2) {
      enemies.remove(i);
        lives--;
      }
    }
  }

  public void update() {
    draw();
    move();
    hit();
    for (int i = 0; i < bullets.size(); i ++) {
      Bullet b = bullets.get(i);
      b.update(); 

      if (b.y < 0) {
        bullets.remove(i);
      }

      for (int j = 0; j < enemies.size(); j++) {
        Enemy e = enemies.get(j); 
        if (b.hit(e)) { 
          enemies. remove(j);
          bullets.remove(i);
         score++; 
      }
      }
    }
  }



  public void move() {
    if (keyPressed) {
      if (keyCode == LEFT) {
        x = x - 15;
      }

      if (keyCode == RIGHT) {
        x = x + 15;
      }
    }
  }
}
  public void settings() {  size(1000, 900); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "connor_" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
