วันพฤหัสบดีที่ 19 กันยายน พ.ศ. 2556

-Game Egg-(s)




void setup() {
  size(500, 500);
  background(255);
  textSize(30);
  fill(0);

  text("Click to play", 160, 250);
}

float a = 250;
float b = 250;
float posX = a;
float posY = b;
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 take=0;
void draw () {
  if (take==0) {
    if (mousePressed && mouseButton==LEFT) {
      take=1;
      i=1;
      sum=0;
      miss1=0;
      miss2=0;
      SetEgg();
    }
  }
  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) {
        x1[i]++;
      }
      if (x2[i]>340) {
        x2[i]--;
      }
      ellipse(x1[i], y1[i], 20, 25);
      ellipse(x2[i], y2[i], 20, 25);
      if (x1[i]>=100) {
        y1[i]++;
      }
      if (x2[i]<=400) {
        y2[i]++;
      }
      ///////chack get point
      if (y1[i]==(height/2)+20) {
        if (key=='q') {
          sum++;
          x1[i]=1000;
          y1[i]=1000;///Delete egg
        }
      }
      if (y2[i]==(height/2)+20) {
        if (key=='e') {
          sum++;
          x2[i]=1000;
          y2[i]=1000;///Delete egg
        }
      }
      if (y1[i]==(height/2)+120) {
        if (key=='z') {
          sum++;
          x1[i]=1000;
          y1[i]=1000;///Delete egg
        }
      }
      if (y2[i]==(height/2)+120) {
        if (key=='c') {
          sum++;
          x2[i]=1000;
          y2[i]=1000;///Delete egg
        }
      }
      text("Score"+" "+sum, 50, 50);
      /////check miss point
      if (y1[i]==275) {
        miss1++;
        y1[i]=600;
      }
      if (y2[i]==275) {
        miss2++;
        y2[i]=600;
      }
      if (y1[i]==375) {
        miss1++;
        y1[i]=600;
      }
      if (y2[i]==375) {
        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;
      }
    }

    fill (0, 0, 255);
    stroke (0, 0, 255);
    strokeWeight (10);
    line(width/2, (height/2)+70, posX, posY);
    ellipse (posX, posY, 15, 10);

    posX += (a - posX)/15;
    posY += (b - posY)/15;
    if (keyPressed) {
      if (key == 'c') {
        a = width/2 + 80;
        b = height/2 + 100;
      }
      if (key == 'z') {
        a = width/2 - 80;
        b = height/2 + 100;
      }
      if (key == 'q') {
        a = width/2 - 80;
        b = height/2;
      }
      if (key == 'e') {
        a = width/2 + 80;
        b = height/2;
      }
    }
    noStroke ();
    drawBody();
    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() {
  noStroke();
  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() {
  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);
  noStroke();
  //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
}

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 (1);
  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);
}

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

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