import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class quickgame extends PApplet {//254
yourguy guy = new yourguy(300,200);

int CounterResetValue = 500;
int counter;

int score = 0;



HashSet enemies = new HashSet();
float SCREENSIZE = 400;
public void setup(){
  size(400,400);
  ellipseMode(CENTER_RADIUS);

  addBadGuy();
  
  framerate(60);
  
  counter = CounterResetValue;
textFont(loadFont("Arial-Black-14.vlw"),14);
}

public void   addBadGuy(){
  float x,y;
  x = guy.x;
  y = guy.y;
  while(abs(guy.x - x ) < 50 ||abs(guy.y - y ) < 50){
  
      if( PApplet.toInt(random(2)) == 0){
          if( PApplet.toInt(random(2)) ==0){
            x = 20; 
          } else {
             x = SCREENSIZE - 20; 
          }
          y = random(SCREENSIZE - 30)+ 30;
      } else {
          if( PApplet.toInt(random(2)) ==0){
            y = 20; 
          } else {
             y = SCREENSIZE - 20; 
          }
          x = random(SCREENSIZE - 30)+ 30;
        
      }
      
  }
            enemies.add( new badguy(x,y));
}

float lastMouseX, lastMouseY;

public void mouseReleased(){
   if(guy.lives <= 0){
        enemies.clear();
guy.lives=3;
        CounterResetValue = 500;

score = 0;

  
  counter = CounterResetValue;
   } 
}


public void draw(){
  background(100);
    fill(0);

  if(guy.lives>0){   
    text("score:"+score,10,40);
    score++;
  } else {
     text("game over! score:"+score+" click to replay ",10,40);
  }




if(guy.lives>0){  guy.move();}

  guy.bounds();
  guy.draw();
  float Xdiff = mouseX - lastMouseX;
  float Ydiff = mouseY - lastMouseY;
  float power  = sqrt((Xdiff*Xdiff)+(Ydiff*Ydiff));


  if(guy.mouseOn()){
    guy.hit(Xdiff,Ydiff);
  } 
  else {
    guy.unhit();
  }

  lastMouseX = mouseX;
  lastMouseY = mouseY;

  Iterator i = enemies.iterator();

  boolean enemyHit = false;

        while(i.hasNext()){
    badguy enemy = (badguy) i.next(); 

  if(enemy.hit(guy)){
     enemyHit = true; 
  }

    enemy.adjust(guy);
if(guy.lives>0){     enemy.move();}

    enemy.bounds();
    enemy.draw();

    if(enemy.dead && enemy.isOffscreen){
      
      i.remove();
    } 
  }
  
  if(enemyHit){
     if(guy.nowHit == false){
        guy.nowHit = true;
        guy.lives--;
     } 
  } else {
     guy.nowHit = false; 
  }
  
  if(guy.lives > 0){
counter--;
  }
if(enemies.size() == 0){
     counter = CounterResetValue;

  addBadGuy();
  
}

if(counter <=0){
   counter = CounterResetValue;

  addBadGuy();
  
  if(CounterResetValue > 50){
    CounterResetValue -= 5;
  }
}

}




class yourguy{
  public boolean nowHit;
  
  public int lives  = 3;
  public float x,y,xs,ys;
  float size=10;
  float acc = .03f;
  boolean ready;
  yourguy(float px, float py){
    x = px;
    y = py;
  }
  public boolean mouseOn(){
    return (abs(mouseX-x) <  size && abs(mouseY-y) <  size);
  }
  public void move(){
    x += xs;
    y += ys; 
  }
  public void unhit(){
    ready = true; 
  }
  public void hit(float dx, float dy){
    if(ready){
      xs = ((xs * 5 )+ (dx* 5))/ 10;
      ys = ((ys * 5 )+ (dy* 5))/ 10;
      ready = false;
    }
  }


  public void bounds(){
    if(x  < size){
      x = size;
      xs *= -.8f;    
    }
    if(x  > SCREENSIZE - size){
      x = SCREENSIZE - size;
      xs *= -.8f;    
    }

    if(y  < size){
      y = size;
      ys *= -.8f;    
    }
    if(y  > SCREENSIZE - size){
      y = SCREENSIZE - size;
      ys *= -.8f;    
    }


  }


  public void draw(){
    
    fill(255); //white circles
    ellipse(x,y,size,size);

       fill(0);


      text(lives,x,y);

    if(nowHit){
       text("OUCH!",x+10,y-10);
    }
  
    }

}





class badguy{

  public boolean dead = false;
  public boolean isOffscreen;

  public float x,y,xs,ys;
  float size=10;
  float acc = .03f;
  boolean ready;

  badguy(float px, float py){
    x = px;
    y = py;
  }
  
  public boolean hit(yourguy guy){
     if(       sqrt(   
               ( (guy.x - x)*(guy.x - x) )+( (guy.y - y)*(guy.y - y) ) 
               )
               < (2*size)
               ){

            return true;
     }
     return false;
  }

  public void move(){
    x += xs;
    y += ys; 
  }
  public void adjust(yourguy target){
    if(!dead){
      if(x < target.x){
        xs += acc;
      } 
      else {
        xs -= acc;
      }
      if(y < target.y){
        ys += acc;
      }  
      else {
        ys -= acc;
      }
      xs = xs * .99f;
      ys = ys * .99f;

    } 
    else {
      if(x - (size*2) < 0 || (x+ size*2) > SCREENSIZE||y - (size*2) < 0 || (y+ size*2) > SCREENSIZE){
        isOffscreen = true;
      }
    }//must be dead
  }



  public void bounds(){
    if(x  < size){
      dead = true;   
    }
    if(x  > SCREENSIZE - size){
      dead = true;
    }

    if(y  < size){
      dead = true;
    }
    if(y  > SCREENSIZE - size){
      dead = true;

    }


  }


  public void draw(){
    if(! dead){
      fill(100,20,20); 
    } 
    else {
      fill(0); 
    }
    ellipse(x,y,size,size);
  }

}
static public void main(String args[]) {   PApplet.main(new String[] { "quickgame" });}}