วันอาทิตย์ที่ 15 กันยายน พ.ศ. 2556

-Game Egg-




     code

void setup() {
 size(500, 500);
 background(255);
 textSize(30);
 fill(0);
 
 text("Click to play",160,250);
 
}
int xSS=0;
float[] x1=new float[100];//egg left
float[] x2=new float[100];
float[] y1=new float[100];//egg left
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int count=0;
int take=0;
void draw () {
 if (take==0) {
   
   if (mousePressed && mouseButton==LEFT) {
     take=1;
     i=1;sum=0;miss1=0;miss2=0;count=0;
     SetEgg();xSS=0;
   }
 }
 if (take==1) {
   BG();
   frameRate(90);
   strokeWeight (3);
   fill(255);
   drawBody();
   fill(255, 228, 196);
   i=0;
   while (i<x1.length) {
     if (x1[i]<=160) {
       if (count%30==0) {
         x1[i]=x1[i]+25;
       }
     }
     if (x2[i]>340) {
       if (count%30==0) {
         x2[i]=x2[i]-25;
       }
     }
     if (x1[i]>=90) {
       if (count%30==0) {
         y1[i]=y1[i]+13;
       }
     }
     if (x2[i]<=400) {
       if (count%30==0) {
         y2[i]=y2[i]+13;
       }
     }
     ellipse(x1[i], y1[i], 20, 25);
     ellipse(x2[i], y2[i], 20, 25);
     ///////chack get point
     if (y1[i]>=225&&y1[i]<=240) {
       if (xSS==1) {
         sum++;
         x1[i]=1000;
         y1[i]=1000;///Delete egg
       }
     }
     if (y2[i]>=225&&y2[i]<=240) {
       if (xSS==2) {
         sum++;
         x2[i]=1000;
         y2[i]=1000;///Delete egg
       }
     }
     if (y1[i]>=329&&y1[i]<=342) {
       if (xSS==3) {
         sum++;
         x1[i]=1000;
         y1[i]=1000;///Delete egg
       }
     }
     if (y2[i]>=329&&y2[i]<=342) {
       if (xSS==4) {
         sum++;
         x2[i]=1000;
         y2[i]=1000;///Delete egg
       }
     }

     /////check miss point
     if (y1[i]>=252&&y1[i]<=276) {
       miss1++;
       y1[i]=600;
     }
     if (y2[i]>=252&&y2[i]<=276) {
       miss2++;
       y2[i]=600;
     }
     if (y1[i]>=351&&y1[i]<=373) {
       miss1++;
       y1[i]=600;
     }
     if (y2[i]>=351&&y2[i]<=373) {
       miss2++;
       y2[i]=600;
     }
     if (miss1==1) {
       breakEgg(160);
     }
     if (miss2==1) {
       breakEgg(340);
     }
     if (miss1==2) {
       breakEgg(160);
     }
     if (miss2==2) {
       breakEgg(340);
     }
     if (miss1==3) {
       breakEgg(160);
     }
     if (miss2==3) {
       breakEgg(340);
     }

     fill(255, 228, 196);
     text("Score"+" "+sum, 50, 50);
     text("Miss", 200, 50);
     i++;
     if ((miss1+miss2)>=3) {
       textSize(50);
       fill(255, 0, 0);
       text("Game over", 120, 150);
       fill(255);
       textSize(30);
       take=0;
       
     }
   }

   if (keyPressed) {
     if (key == 'q' || key == 'Q') {
       xSS=1;
     }
     if (key == 'e' || key == 'E') {
       xSS=2;
     }
     if (key == 'z' || key == 'Z') {
       xSS=3;
     }
     if (key == 'c' || key == 'C') {
       xSS=4;
     }
   }
   switch(xSS) {
   case 1:  
     fill(255);
     drawArms (-35, 53, 67, -85, 22, 18, 20);
     break;
   case 2:      
     fill(255);
     drawArms (+35, 53, 67, 85, 22, 18, 20);
     break;
   case 3:    
     fill(255);
     drawArms (-35, 73, 87, -85, 122, 118, 120);
     break;
   case 4:  
     fill(255);
     drawArms (35, 73, 87, 85, 122, 118, 120);
     break;
   default :
     fill(255);
     break;
   }
   count++;
   drawChicken();
   if (miss1+miss2>0) {
     int m1=0;
     int k=0;
     while (k<miss1+miss2) {
       fill(255, 228, 196);
       arc(290+m1, 38, 25, 30, 0, PI);
       fill(0);
       triangle(290+m1, 38, 278+m1, 38, 284+m1, 45);
       triangle(290+m1, 38, 302+m1, 38, 296+m1, 45);
       k++;
       m1=m1+30;
     }
   }
 }
}
void BG()
{
 background(0);
 drawTree();
 drawChute();
 drawBush();
}
void drawChute() {
 stroke(255);
 fill(255, 0, 0);
 //left
 quad(85, 200, 85, 215, 150, 260, 150, 245);
 quad(85, 310, 85, 325, 150, 370, 150, 355);
 //right
 quad(415, 200, 415, 215, 350, 260, 350, 245);
 quad(415, 310, 415, 325, 350, 370, 350, 355);
 fill(0, 255, 255);
 //right
 rect(415, 200, 85, 15);
 rect(415, 310, 85, 15);
 //left
 rect(0, 200, 85, 15);
 rect(0, 310, 85, 15);
 rect(0, 330, 20, 50);
 rect(25, 330, 20, 50);
 rect(50, 330, 20, 50);
 rect(480, 330, 20, 50);
 rect(455, 330, 20, 50);
 rect(430, 330, 20, 50);
}
void drawTree() {
 noStroke();
 fill(139, 69, 19);
 rect(475, 50, 30, 55);
 fill(124, 252, 0);
 rect(420, 0, 80, 45);
 ellipse(410, 5, 40, 30);
 ellipse(420, 25, 30, 30);
 ellipse(435, 40, 30, 30);
 ellipse(460, 45, 30, 30);
 ellipse(490, 45, 30, 30);
}
void drawBush() {
 noStroke();
 fill(124, 252, 0);
 ellipse(0, 380, 30, 50);
 ellipse(25, 380, 30, 50);
 ellipse(50, 380, 50, 40);
 ellipse(80, 380, 30, 30);
 ellipse(500, 380, 30, 50);
 ellipse(475, 380, 30, 50);
 ellipse(450, 380, 50, 40);
 ellipse(420, 380, 30, 30);
}
void drawChicken() {
 fill(255, 0, 0);
 //cockscomb
 ellipse(431, 140, 15, 15);
 ellipse(445, 140, 15, 15);
 ellipse(455, 144, 15, 15);
 ellipse(431, 250, 15, 15);
 ellipse(445, 250, 15, 15);
 ellipse(455, 254, 15, 15);
 ellipse(70, 140, 15, 15);
 ellipse(56, 140, 15, 15);
 ellipse(46, 144, 15, 15);
 ellipse(70, 250, 15, 15);
 ellipse(56, 250, 15, 15);
 ellipse(46, 254, 15, 15);
 stroke(255);
 //Chicken's mouth
 triangle(415, 160, 425, 140, 450, 160);
 triangle(415, 270, 425, 250, 450, 270);
 triangle(50, 160, 75, 140, 84, 160);
 triangle(50, 270, 75, 250, 84, 270);

 fill(255, 255, 0);
 rect(425, 140, 30, 25);
 rect(425, 165, 65, 35);
 rect(425, 250, 30, 25);
 rect(425, 275, 65, 35);
 rect(45, 140, 30, 25);
 rect(10, 165, 65, 35);
 rect(45, 250, 30, 25);
 rect(10, 275, 65, 35);
 noStroke();
 //Chicken's eye
 fill(255);
 ellipse(433, 150, 12, 12);//1
 ellipse(433, 260, 12, 12);//2
 ellipse(67, 150, 12, 12);//3
 ellipse(67, 260, 12, 12);//4
 fill(0);
 ellipse(430, 152, 5, 5);//1
 ellipse(430, 262, 5, 5);//2
 ellipse(70, 152, 5, 5);//3
 ellipse(70, 262, 5, 5);//4

 fill(255, 0, 0);
 ellipse(490, 167, 26, 26);
 ellipse(494, 182, 17, 17);
 ellipse(496, 194, 14, 14);

 ellipse(490, 278, 26, 26);
 ellipse(494, 293, 17, 17);
 ellipse(496, 304, 14, 14);

 ellipse(10, 167, 26, 26);
 ellipse(6, 182, 17, 17);
 ellipse(4, 194, 14, 14);

 ellipse(10, 278, 26, 26);
 ellipse(6, 293, 17, 17);
 ellipse(4, 304, 14, 14);
}
void drawArms (int p, int k, int j, int l, int u, int y, int h) {
 fill (0, 0, 255);
 quad((width/2)+p, (height/2)+k, (width/2)+p, (height/2)+j, (width/2)+l, (height/2)+u, (width/2)+l, (height/2)+y);
 ellipse ((width/2)+l, (height/2)+h, 15, 10);
}
void drawBody() {
 noStroke ();
 fill (0, 0, 255);
 triangle((width/2)-20, (height/2)-10, (width/2)-30, (height/2)-40, (width/2), (height/2)-20); //ears
 triangle((width/2)+20, (height/2)-10, (width/2)+30, (height/2)-40, (width/2), (height/2)-20);
 fill (255);
 triangle((width/2)-20, (height/2)-10, (width/2)-30, (height/2)-40, (width/2)-15, (height/2)-20); //in ears
 triangle((width/2)+20, (height/2)-10, (width/2)+30, (height/2)-40, (width/2)+15, (height/2)-20);
 fill (0, 0, 255);
 ellipse ((width/2)-10, (height/2)+130, 10, 70); //legs
 ellipse ((width/2)+10, (height/2)+130, 10, 70);
 rect ((width/2)+20, (height/2)+100, 15, 10, 10);//tail
 ellipse (width/2, (height/2)+70, 70, 100); //body
 fill (102, 204, 255);
 ellipse (width/2, (height/2)+70, 40, 70); //in body
 ellipse ((width/2)-10, height-85, 20, 10); //feet
 ellipse ((width/2)+10, height-85, 20, 10);
 fill (0, 0, 255);
 ellipse (width/2, (height/2), 60, 60); //haed
 noStroke ();
 fill (255);
 ellipse ((width/2)-10, (height/2)-15, 15, 20);//eyes
 ellipse ((width/2)+10, (height/2)-15, 15, 20);
 fill (0);
 ellipse ((width/2)-10, (height/2)-13, 10, 15);//in eyes
 ellipse ((width/2)+10, (height/2)-13, 10, 15);
 fill (204, 255, 255);
 arc((width/2), (height/2)+20, 40, 40, PI, 2*PI);//mouth
 arc((width/2), (height/2)+20, 40, 20, 0, PI);//mouth
 fill (153);
 ellipse (width/2, (height/2)+10, 25, 25); //nose
 stroke (102);
 strokeWeight (1.8);
 line (width/2, (height/2)-5, width/2, (height/2)+4);
 curve((width/2)-3, (height/2)-20, width/2, (height/2)+4, (width/2)-9, (height/2)+12, (width/2)-6, (height/2)-10);
 curve((width/2)+3, (height/2)-20, width/2, (height/2)+4, (width/2)+9, (height/2)+12, (width/2)+6, (height/2)-10);
 noStroke();
 fill (0);
 ellipse (width/2, (height/2), 15, 10);
 stroke (0, 0, 255);
 strokeWeight (3);
 curve((width/2)-25, (height/2)+20, (width/2)-20, (height/2), (width/2)-40, (height/2)+5, (width/2)-35, (height/2)+10);//Mustache
 curve((width/2)+25, (height/2)+20, (width/2)+20, (height/2), (width/2)+40, (height/2)+5, (width/2)+35, (height/2)+10);
 curve((width/2)-25, (height/2)+30, (width/2)-20, (height/2)+10, (width/2)-40, (height/2)+15, (width/2)-35, (height/2)+20);
 curve((width/2)+25, (height/2)+30, (width/2)+20, (height/2)+10, (width/2)+40, (height/2)+15, (width/2)+35, (height/2)+20);
 noStroke();
 strokeWeight (0);
}
void SetEgg() {
 x1[0]=random(-200, -150);
 x2[0]=0-x1[0]+random(100, 200)+500;
 y1[0]=185;
 y2[0]=290;
 while (i<x1.length) {
   x1[i]=0-(x2[i-1]-500+random(0, 100));
   x2[i]=0-x1[i]+random(50, 200)+500;
   if (i%2==0) {
     y1[i]=185;
     y2[i]=185;
   }
   else {
     y1[i]=295;
     y2[i]=295;
   }
   i=i+1;
 }
}



void breakEgg(int x)
{
 fill(255, 204, 153);
 stroke(1);
 arc(x, 450, 20, 15, PI/2, TWO_PI-PI/2);
 arc(x+10, 450, 20, 15, TWO_PI-PI/2, TWO_PI+PI/2);
 fill(255, 100, 0);
 ellipse(x+5, 455, 10, 10);
 fill(0);
 noStroke();
 //left
 triangle(x, 450, x, 442, x-5, 446);
 triangle(x, 450, x, 458, x-5, 454);
 //right
 triangle(x+10, 450, x+10, 442, x+15, 446);
 triangle(x+10, 450, x+10, 458, x+15, 454);
}



ไม่มีความคิดเห็น:

แสดงความคิดเห็น