Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // taille des Objets
- int tVaisseau = 30;
- int tEnnemis = 30;
- //vitesse ennemis +vaisseau
- int eSpeed = 2;
- int vSpeed = 2;
- // coordonnée vaisseau
- int xVaisseau;
- int yVaisseau;
- int xs1,ys1,xs2,ys2,xs3,ys3;
- // position enemies
- ArrayList<Integer> xE = new ArrayList();
- ArrayList<Integer> yE = new ArrayList();
- // touche du jeu
- boolean up= false;
- boolean down= false;
- boolean left= false;
- boolean right=false;
- boolean z= false;
- boolean q= false;
- boolean s= false;
- boolean d= false;
- // mode de jeu
- int screen ;
- // aire triangle
- double aireT;
- void setup(){
- size(800,600);
- noCursor();
- smooth();
- xVaisseau = width/2;
- yVaisseau = height/2;
- frameRate(180);
- screen = 0;
- aireT = triangleA(xVaisseau, yVaisseau,xVaisseau+tVaisseau, (yVaisseau-tVaisseau/2), xVaisseau+tVaisseau, (yVaisseau+tVaisseau/2));
- }
- void draw()
- {
- if(screen == 0)
- ecranAcceuil();
- else if (screen == 1){
- background(0);
- if((int)random(0,30)==0)
- ajouterEnnemis();
- bougerEnnemi();
- bougerVaisseau();
- collision();
- affichage();
- }
- }
- void mousePressed(){
- if (screen == 0) screen=1;
- }
- void ecranAcceuil(){
- background(0);
- fill(0);
- stroke(255,0,0);
- ellipse(width/2,height/2,200,80);
- //textFont(f,75);
- textAlign(CENTER);
- fill(255,0,0);
- text("BALLOON BREAKER",width/2,height/5);
- //textFont(f,30);
- text("PLAY/JOUER",width/2,height/2+10);
- fill(255,255,255);
- stroke(255,255,255);
- }
- void ajouterEnnemis(){
- xE.add(tEnnemis);
- yE.add((int)(Math.random()*(height-tEnnemis)));
- }
- void bougerEnnemi(){
- for(int i=0;i<xE.size();i++){
- xE.set(i,xE.get(i)+1);
- }
- }
- void bougerVaisseau(){
- if(z||up){
- yVaisseau=yVaisseau-vSpeed;
- }
- if(s||down){
- yVaisseau=yVaisseau+vSpeed;
- }
- if(q||left){
- xVaisseau=xVaisseau-vSpeed;
- }
- if(d||right){
- xVaisseau=xVaisseau+vSpeed;
- }
- // nouveau sommet triangle
- xs1 =xVaisseau;
- ys1=yVaisseau;
- xs2=xVaisseau+tVaisseau;
- ys2=yVaisseau-tVaisseau/2;
- xs3=xVaisseau+tVaisseau;
- ys3=yVaisseau+tVaisseau/2;
- }
- void keyPressed(){
- switch(keyCode){
- case UP: up = true; break;
- case DOWN : down = true; break;
- case LEFT : left = true; break;
- case RIGHT : right = true; break;
- case 'z': z= true; break;
- case 'q': q= true; break;
- case 's': s= true; break;
- case 'd': d= true; break;
- }
- }
- void keyReleased(){
- switch(keyCode){
- case UP: up = false; break;
- case DOWN : down = false; break;
- case LEFT : left = false; break;
- case RIGHT : right = false; break;
- case 'z': z= false; break;
- case 'q': q= false; break;
- case 's': s= false; break;
- case 'd': d= false; break;
- }
- }
- void collision(){
- float x,y,r;
- r=tEnnemis/2;
- for (int i=0 ; i<xE.size();i++)
- {
- x=xE.get(i);y=yE.get(i);
- if(x-tEnnemis>width){
- xE.remove(i);
- yE.remove(i);
- }
- else if((y+r > yVaisseau-tVaisseau/2 && y-tEnnemis/2< yVaisseau+tVaisseau/2 )&&( x+tEnnemis/2>xVaisseau && x-tEnnemis/2<xVaisseau+tVaisseau)){
- if (colision(x,y,r))
- {
- xE.remove(i);
- yE.remove(i);
- }
- }
- }
- }
- float triangleA(int px1, int py1 , int px2 ,int py2 ,int px3 , int py3){
- float A , longA,longB,longC ,longD;
- // A = 1/2||AB vectoriel AC||
- longA=px1-px3;
- longB=py1-py3;
- longC=px2-px3;
- longD= py2-py3;
- A=0.5* abs((longA*longD)-(longB*longC));
- return A;
- }
- boolean colision(float x,float y ,float r){
- int xC,yC,xG,yG; // point sur le cerlcle de du vecteur centre gravité triangle centre cercle
- float longux,longuy,angle ,A1,A2,A3,AT;
- // centre de gravité du triangle
- xG = (xs1+xs2+xs3)/3;
- yG = (ys1+ys2+ys3)/3;
- longux = xG-x;
- longuy = yG-y;
- angle = atan2(longuy,longux);
- //calcul du potentiel point de colision
- xC= int(x+cos(angle)*r);
- yC =int(y+sin(angle)*r);
- // calcule des 3 aires créées par le point potentiel de collision
- A1=triangleA(xC,yC,xs1,ys1,xs2,ys2);
- A2=triangleA(xC,yC,xs2,ys2,xs3,ys3);
- A3=triangleA(xC,yC,xs1,ys1,xs3,ys3);
- AT= A1+A2+A3;
- // la somme des 3 aire est egal alors le point est dans le triangle
- if (aireT==AT)
- return true;
- else if(colC(xs1 ,ys1 , x,y,r))
- return true;
- else if(colC(xs2 ,ys2 , x,y,r))
- return true;
- else if(colC(xs3 ,ys3 , x,y,r))
- return true;
- return false;
- }
- boolean colC(float xs,float ys,float xc,float yc,float r){
- return (xs-xc)*(xs-xc)+(ys-yc)*(ys-yc)<= r*r;
- }
- void affichage(){
- int x,y;
- for(int i = 0;i<xE.size();i++){
- x = xE.get(i); y = yE.get(i);
- ellipse(x,y,tEnnemis,tEnnemis);
- }
- triangle(xs1, ys1,xs2 ,ys2, xs3, ys3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement