Advertisement
Tecnelm

Untitled

Jan 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. // taille des Objets
  2. int tVaisseau = 30;
  3. int tEnnemis = 30;
  4. //vitesse ennemis +vaisseau
  5. int eSpeed = 2;
  6. int vSpeed = 2;
  7. // coordonnée vaisseau
  8. int xVaisseau;
  9. int yVaisseau;
  10. int xs1,ys1,xs2,ys2,xs3,ys3;
  11. // position enemies
  12. ArrayList<Integer> xE = new ArrayList();
  13. ArrayList<Integer> yE = new ArrayList();
  14. // touche du jeu
  15. boolean up=   false;
  16. boolean down= false;
  17. boolean left= false;
  18. boolean right=false;
  19. boolean z=    false;
  20. boolean q=    false;
  21. boolean s=    false;
  22. boolean d=    false;
  23. // mode de jeu
  24. int screen ;
  25. // aire triangle
  26. double aireT;
  27.  
  28.  
  29. void setup(){
  30.   size(800,600);
  31.   noCursor();
  32.   smooth();
  33.   xVaisseau =  width/2;
  34.   yVaisseau = height/2;
  35.   frameRate(180);
  36.   screen = 0;
  37.   aireT = triangleA(xVaisseau, yVaisseau,xVaisseau+tVaisseau, (yVaisseau-tVaisseau/2), xVaisseau+tVaisseau, (yVaisseau+tVaisseau/2));
  38. }
  39.  
  40. void draw()
  41. {
  42.   if(screen == 0)
  43.    ecranAcceuil();
  44.   else if (screen == 1){
  45.     background(0);
  46.    
  47.       if((int)random(0,30)==0)
  48.       ajouterEnnemis();
  49.       bougerEnnemi();
  50.       bougerVaisseau();
  51.       collision();
  52.       affichage();
  53.   }
  54. }
  55. void mousePressed(){
  56. if (screen == 0) screen=1;
  57.  
  58. }
  59. void ecranAcceuil(){
  60.   background(0);
  61.   fill(0);
  62.   stroke(255,0,0);
  63.   ellipse(width/2,height/2,200,80);
  64.   //textFont(f,75);
  65.   textAlign(CENTER);
  66.   fill(255,0,0);
  67.   text("BALLOON BREAKER",width/2,height/5);
  68.   //textFont(f,30);
  69.   text("PLAY/JOUER",width/2,height/2+10);
  70.   fill(255,255,255);
  71.   stroke(255,255,255);
  72.  
  73.   }
  74. void ajouterEnnemis(){
  75.   xE.add(tEnnemis);
  76.   yE.add((int)(Math.random()*(height-tEnnemis)));  
  77. }
  78.  void bougerEnnemi(){
  79.   for(int i=0;i<xE.size();i++){
  80.     xE.set(i,xE.get(i)+1);
  81.   }
  82. }
  83. void bougerVaisseau(){
  84.     if(z||up){
  85.       yVaisseau=yVaisseau-vSpeed;
  86.     }
  87.     if(s||down){
  88.        yVaisseau=yVaisseau+vSpeed;
  89.     }
  90.    
  91.     if(q||left){
  92.       xVaisseau=xVaisseau-vSpeed;
  93.     }
  94.    
  95.     if(d||right){
  96.       xVaisseau=xVaisseau+vSpeed;
  97.     }
  98.    
  99.     // nouveau sommet triangle
  100.   xs1 =xVaisseau;
  101.   ys1=yVaisseau;
  102.   xs2=xVaisseau+tVaisseau;
  103.   ys2=yVaisseau-tVaisseau/2;
  104.   xs3=xVaisseau+tVaisseau;
  105.   ys3=yVaisseau+tVaisseau/2;
  106. }
  107. void keyPressed(){
  108.   switch(keyCode){
  109.     case UP: up =        true; break;
  110.     case DOWN : down =   true; break;
  111.     case LEFT : left =   true; break;
  112.     case RIGHT : right = true; break;
  113.     case 'z': z=         true; break;
  114.     case 'q': q=         true; break;
  115.     case 's': s=         true; break;
  116.     case 'd': d=         true; break;
  117.    
  118.   }
  119. }
  120. void keyReleased(){
  121.    switch(keyCode){
  122.     case UP: up =        false; break;
  123.     case DOWN : down =   false; break;
  124.     case LEFT : left =   false; break;
  125.     case RIGHT : right = false; break;
  126.     case 'z': z=         false; break;
  127.     case 'q': q=         false; break;
  128.     case 's': s=         false; break;
  129.     case 'd': d=         false; break;
  130.   }  
  131. }
  132. void collision(){
  133.   float x,y,r;
  134.   r=tEnnemis/2;
  135.  
  136.   for (int i=0 ; i<xE.size();i++)
  137.   {
  138.     x=xE.get(i);y=yE.get(i);
  139.     if(x-tEnnemis>width){
  140.       xE.remove(i);
  141.       yE.remove(i);
  142.     }    
  143.     else if((y+r > yVaisseau-tVaisseau/2 && y-tEnnemis/2< yVaisseau+tVaisseau/2 )&&( x+tEnnemis/2>xVaisseau && x-tEnnemis/2<xVaisseau+tVaisseau)){          
  144.       if (colision(x,y,r))
  145.       {
  146.         xE.remove(i);
  147.         yE.remove(i);
  148.       }  
  149.     }    
  150.   }
  151. }
  152.  
  153. float triangleA(int px1, int py1 , int px2 ,int py2 ,int px3 , int py3){
  154.  
  155.   float A , longA,longB,longC ,longD;
  156.   // A = 1/2||AB vectoriel AC||
  157.   longA=px1-px3;
  158.   longB=py1-py3;
  159.   longC=px2-px3;
  160.   longD= py2-py3;
  161.  
  162.   A=0.5* abs((longA*longD)-(longB*longC));
  163.   return A;
  164.     }
  165.    
  166. boolean colision(float x,float y ,float r){
  167.   int xC,yC,xG,yG; // point sur le cerlcle de du vecteur centre gravité triangle centre cercle
  168.   float longux,longuy,angle ,A1,A2,A3,AT;
  169.   // centre de gravité du triangle
  170.   xG = (xs1+xs2+xs3)/3;
  171.   yG = (ys1+ys2+ys3)/3;    
  172.   longux = xG-x;
  173.   longuy = yG-y;
  174.   angle  = atan2(longuy,longux);  
  175.   //calcul du potentiel point de colision
  176.   xC= int(x+cos(angle)*r);
  177.   yC =int(y+sin(angle)*r);
  178.   // calcule des 3 aires créées par le point potentiel de collision
  179.   A1=triangleA(xC,yC,xs1,ys1,xs2,ys2);
  180.   A2=triangleA(xC,yC,xs2,ys2,xs3,ys3);
  181.   A3=triangleA(xC,yC,xs1,ys1,xs3,ys3);
  182.    
  183.   AT= A1+A2+A3;
  184.   // la somme des 3 aire est egal alors le point est dans le triangle
  185.   if (aireT==AT)
  186.     return true;
  187.   else if(colC(xs1 ,ys1 , x,y,r))
  188.     return true;
  189.   else if(colC(xs2 ,ys2 , x,y,r))
  190.     return true;
  191.   else if(colC(xs3 ,ys3 , x,y,r))
  192.     return true;  
  193.   return false;
  194. }
  195.  
  196. boolean colC(float xs,float ys,float xc,float yc,float r){
  197.   return (xs-xc)*(xs-xc)+(ys-yc)*(ys-yc)<= r*r;
  198.  
  199. }
  200.  
  201. void affichage(){
  202.   int x,y;
  203.  
  204.   for(int i = 0;i<xE.size();i++){
  205.     x = xE.get(i); y = yE.get(i);
  206.     ellipse(x,y,tEnnemis,tEnnemis);
  207.   }
  208.    triangle(xs1, ys1,xs2 ,ys2, xs3, ys3);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement