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 breastpong extends PApplet {
PImage breastpic[] = new PImage[100];

HashSet breasts = new HashSet();
int WIN = 100;

int BEFOREGAME = 0;
int DURINGGAME = 1;
int AFTERGAME = 2;

int gamestate = BEFOREGAME;



float PADDLESIZE = 60;
float PADDLETOP = 260;
float PADDLEHEIGHT = 20;

float counter;
float counterresetval = 40;
int lives;
int score = 0;




public void setup(){
  size(300,300);
  for(int i = 1; i <= 100; i++){
    breastpic[i-1] = loadImage("Image"+i+".jpg");
  }

  textFont(loadFont("Arial-Black-14.vlw"),14);
  textAlign(CENTER);
  framerate(60);
}


public void addBreast(){
  breasts.add(new breastclass(150,0,random(4)-2,random(5)+.5f)); 
}

public void mouseReleased(){
   if(gamestate == BEFOREGAME || gamestate == AFTERGAME){
      score = 0;
      lives = 3;
      counterresetval = 40;
      gamestate = DURINGGAME;
        breasts.clear();
        addBreast();
        
   } 
   
}

public void draw(){
  background(255); 


  if(gamestate == DURINGGAME){

    PADDLETOP = mouseY;
    if(PADDLETOP < 200) PADDLETOP = 200;

    counter--;
    if(counter <= 0){
      counter = counterresetval;
      //counterresetval -= 1;
      addBreast();
    }

    Iterator i = breasts.iterator();
    while(i.hasNext()){
      breastclass breast = (breastclass)i.next();
      breast.move();
      breast.checksides();
      breast.checkhit();
      breast.show();
      
      if(breast.toolow()){
        i.remove();
                  lives--; 
      }
      if(breast.toohigh()){
        i.remove();
      }
    }

    /*
     */
    noStroke();
    fill(0);
    rect(mouseX - (PADDLESIZE/2), PADDLETOP, PADDLESIZE, PADDLEHEIGHT);

    text("score:"+score+"    lives"+lives, 150,290);
  
    if(score >= WIN || lives <= 0){
       gamestate = AFTERGAME; 
    }
  
  }

  


  if(gamestate==BEFOREGAME){
     fill(0);
     
     text("BREASTPONG",150,100);
     text("score 100 points,",150,200);
     text("see 100 breasts!",150,220);
     
     text("click to play!",150,270);
     

  }
  if(gamestate==AFTERGAME){

    if(score < WIN){
      fill(0);
      text("YOU LOSE!!!!",150,100);

      text("click to try again",150,220);
    } else {
      for(int i = 0; i < 100; i++){
     image(breastpic[i], 30*(i / 10), 30*(i % 10));
     }
    }
  }
}
//----------------------------------------------------------------
class breastclass{
  boolean up = false;
  float x,y,xs,ys;
  PImage img;
  breastclass(float px, float py, float pxs,float pys){
    int number =  PApplet.toInt(random(100))+1;
    String imagefile = "Image"+number+".jpg";
    img = loadImage(imagefile);
    x = px;
    y = py;
    xs = pxs;
    ys = pys;
  }

  public void show(){
    image(img,x,y);
  }

  public boolean checkhit(){
    if(
    overlap(x,x+30,mouseX - (PADDLESIZE/2),mouseX + (PADDLESIZE/2))
      &&
      overlap(y,y+30,PADDLETOP,PADDLETOP+PADDLEHEIGHT)
      ){
      ys = -1 * abs(ys);
      if(!up){
        up = true;
        score++; 
      }
      return true; 
    }
    return false;
  }

public boolean toohigh(){
   return y < -30; 
}
public boolean toolow(){
   return y > 300; 
}

  public void checksides(){
    if(x < 0){
      x = 0;
      xs *= -1;        
    } 
    if(x > 270){
      x = 270;
      xs *= -1;        
    } 
  }

  public void move(){
    x+=xs;
    y+=ys;
  } 


}



public boolean overlap(float line1val1,float line1val2,float line2val1,float line2val2){
  float line1min = (line1val1 < line1val2)?line1val1:line1val2;
  float line1max = (line1val1 > line1val2)?line1val1:line1val2;
  float line2min = (line1val1 < line2val2)?line2val1:line2val2;
  float line2max = (line1val1 > line2val2)?line2val1:line2val2;

  if(line1min < line2min && line1max < line2min){
    return false; 
  }
  if(line1min > line2max && line1max > line2max){
    return false; 
  }  


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