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

import ddf.minim.*; 

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 charly_vs_arouf extends PApplet {


//musique 

Minim minim;
AudioPlayer uneMusique;

//ecran
int ecran = 0;

//charly
float gunX = 350;
//boule pour tirer
float bulletX = 0;
float bulletY=0;

//arouf
float ellY = 0;
float ellX = 250;

//deplacement arouf
boolean moveDown = true;
boolean moveUp=false;

boolean bullet =true; 

int countA = 0;
int countY =  0;

//vitesse
int vitesse = 4;

//image
PImage terrain ;
PImage charly;
PImage arouf;
PImage menu;

public void setup() {
  
  
  //image
  terrain=loadImage("univers.png");
  charly=loadImage("charly.png");
  arouf=loadImage("arouf.png");
  menu=loadImage("menu.png");
  
  //musique
  minim = new Minim(this);
  uneMusique = minim.loadFile("parodie-aroufmohamed-hennitk78-bande-organise.mp3");
  
  
  uneMusique.loop();
}

public void draw(){
  background(terrain);
  
  //menu
if (ecran == 0) {
  afficherMenu();
}
if (ecran ==1){
  afficherJeu();
} 
}
public void afficherJeu () {
 afficherArouf();
 afficherPers();
 deplacerPers();
 deplacerArouf();
 deplacerTir();
}
public void afficherArouf(){
 fill (255,255,0);                  //couleur
 image(arouf,ellX,ellY,60,60);           //arouf
 textSize(30);                              //taille du texte
 text("Arouf",50,50);                     // placement arouf 
 text(countA,200,50);                     // placement du score arouf
}

public void deplacerArouf(){                          //deplacer arouf 
 if (moveDown == true){                        // deplacement vers le bas
    ellY = ellY +5;                         // vitesse arouf 
  }
 if (ellY  <=650){
  moveDown = true ;                // deplacement vers le bas
}
 if (ellY> 650 ) {                  
  ellY = 0;
  ellX = random(30,670);                 //arouf deplacement aleatoire 
  moveDown = true;
  
  countA++;
 }
}
public void deplacerTir(){
  bulletX =gunX +10;
 ellipse (bulletX,bulletY,10,10);
 
 if (keyPressed &&key == ' ') // bar espace 
{
  moveUp = true;
}
if (moveUp ==true){
  bulletY = bulletY -10; 
}
if (bulletY <10){
 bulletY = 650;
 moveUp = false;
}

if(bulletY < ellY +50 && bulletY > ellY -50 && bulletX <ellX +50 && bulletX > ellX -50)
{
  
 ellY = 0;
 ellX= random(70,600);
 moveDown = true;
 countY++;
 }
}
public void afficherPers(){
 fill(0);       
 image(charly,gunX,600,60,100);        // charly
 text("Charly",400,50);                // placement de charly
 text(countY,550,50);                   // taille du score charly 
} 
 
 public void deplacerPers(){
 
 
 if(keyPressed == true){               //presser la touche
   if(keyCode == RIGHT){                // aller a droite
     gunX += vitesse;                    // vitesse de charly 
     } 
   }
    if(keyCode == LEFT){                // aller a gauche
     gunX -= vitesse;                       // vitesse de charly
      }
   }
public void afficherMenu ()
{
  background(10,20,40);      // fond du menu
  image (menu,0,0);              // image du menu
  
  
  //bouton play
  fill(0, 0, 0); 
  rect(220,350, 300, 80);
  stroke(0, 0, 70, 40);
  strokeWeight(2.0f);

  //bouton exit
  fill(0, 0, 0);
  rect(220, 250 , 300,80);
  stroke(0, 0, 70, 40);
  strokeWeight(2.0f);

  //texte play 
  fill(255,0,0);
  textSize(40);
  text("PLAY", 300, 300); 

  //bouton exit
  fill(255,0,0);
  textSize(40);
  text("EXIT", 300, 400);
}

public void mouseClicked(){
  if((mouseX > width/2-75) && (mouseX < width/2+75) && (mouseY > height/2-110) && (mouseY < height/2+ 5) &&  (ecran == 0)) {
    ecran = 1;
  }
  if((mouseX > width/2-75) && (mouseX < width/2+75) && (mouseY > height/2+62.5f) && (mouseY < height/2+ 137.5f) &&  (ecran == 0)) {
    exit();
  }
}
  public void settings() {  size(700,700); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "charly_vs_arouf" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
