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

Lab 2 - Turtle

เต่า เต่า เต่า
เต่ามันมีสี่ขา
สี่ตีนเดินมา มันทำหัวผลุบๆโผล่ๆ




void setup()
{
  //Initialization
  size(400,400);
  background(0,238,255);
  frameRate(60);
}

float I=0;

float J=0;
int CheckStateI = 0;
int CheckStateJ = 0;
void draw(){
    background(0,238,255);
    DrawTurtle(mouseX,mouseY);
    
    if(CheckStateI == 0) { 
        I=I+0.5; 
    } else { 
        I=I-0.5; 
        }
    if(I >= 40) { 
        CheckStateI = 1; 
        }
    if(I <= 0) { 
        CheckStateI = 0; 
        }
        
    if(CheckStateJ == 0) { 
        J=J+0.5; 
    } else { 
        J=J-0.5; 
        }
    if(J >= 20) { 
        CheckStateJ = 1; 
        }
    if(J <= -20) { 
        CheckStateJ = 0; 
        }
        
}

void DrawTurtle(int RefX,int RefY){

    //variable
    float WBack,HBack,dBack;
    float HHead;
    float LArm;
    float LTail;
    float WEye,HEye;
    //assign
    WBack = 80;
    HBack = 120;
    dBack = 15;
    HHead = 20+I;
    LArm = 30;
    LTail = 40;
    WEye = 5;
    HEye = 5;
    
    fill(37,184,0);
    //draw head turtle
    beginShape();
    vertex(RefX+(WBack/4),RefY-(HBack/4));
    vertex(RefX-(WBack/4),RefY-(HBack/4));
    vertex(RefX-(WBack/4),RefY-(HBack/2)-dBack-HHead);
    bezierVertex(RefX-(WBack/8),RefY-(HBack/2)-dBack-HHead-20,RefX+(WBack/8),RefY-(HBack/2)-dBack-HHead-20,RefX+(WBack/4),RefY-(HBack/2)-dBack-HHead);
    vertex(RefX+(WBack/4),RefY-(HBack/4));
    endShape();
    
    //draw eye
    fill(0);
    //left eye
    ellipse(RefX-(WBack/8),RefY-(HBack/2)-dBack-HHead,WEye,HEye);
    //right eye
    ellipse(RefX+(WBack/8),RefY-(HBack/2)-dBack-HHead,WEye,HEye);
    
    fill(37,184,0);
    //draw arm turtle
    //left arm top
    beginShape();
    vertex(RefX-(WBack/4),RefY-(HBack/8));
    vertex(RefX-(WBack/2)-dBack-LArm,RefY-(HBack/8));
    bezierVertex(RefX-(WBack/2)-dBack-LArm-20,RefY-(1.5*HBack/8),RefX-(WBack/2)-dBack-LArm-20,RefY-(2*HBack/8),RefX-(WBack/2)-dBack-LArm,RefY-(2.5*HBack/8));
    vertex(RefX-(WBack/4),RefY-(2.5*HBack/8));
    vertex(RefX-(WBack/4),RefY-(HBack/8));
    endShape();
    //left arm bottom
    beginShape();
    vertex(RefX-(WBack/4),RefY+(HBack/8));
    vertex(RefX-(WBack/2)-dBack-LArm,RefY+(HBack/8));
    bezierVertex(RefX-(WBack/2)-dBack-LArm-20,RefY+(1.5*HBack/8),RefX-(WBack/2)-dBack-LArm-20,RefY+(2*HBack/8),RefX-(WBack/2)-dBack-LArm,RefY+(2.5*HBack/8));
    vertex(RefX-(WBack/4),RefY+(2.5*HBack/8));
    vertex(RefX-(WBack/4),RefY+(HBack/8));
    endShape();
    //right arm top
    beginShape();
    vertex(RefX+(WBack/4),RefY-(HBack/8));
    vertex(RefX+(WBack/2)+dBack+LArm,RefY-(HBack/8));
    bezierVertex(RefX+(WBack/2)+dBack+LArm+20,RefY-(1.5*HBack/8),RefX+(WBack/2)+dBack+LArm+20,RefY-(2*HBack/8),RefX+(WBack/2)+dBack+LArm,RefY-(2.5*HBack/8));
    vertex(RefX+(WBack/4),RefY-(2.5*HBack/8));
    vertex(RefX+(WBack/4),RefY-(HBack/8));
    endShape();
    //right arm top
    beginShape();
    vertex(RefX+(WBack/4),RefY+(HBack/8));
    vertex(RefX+(WBack/2)+dBack+LArm,RefY+(HBack/8));
    bezierVertex(RefX+(WBack/2)+dBack+LArm+20,RefY+(1.5*HBack/8),RefX+(WBack/2)+dBack+LArm+20,RefY+(2*HBack/8),RefX+(WBack/2)+dBack+LArm,RefY+(2.5*HBack/8));
    vertex(RefX+(WBack/4),RefY+(2.5*HBack/8));
    vertex(RefX+(WBack/4),RefY+(HBack/8));
    endShape();
    
    //draw tail
    beginShape();
    vertex(RefX+(WBack/4),RefY+(3*HBack/8));
    vertex(RefX-(WBack/4),RefY+(3*HBack/8));
    bezierVertex(RefX-(WBack/8),RefY+(HBack/2)+dBack+(2*LTail/4),RefX-(WBack/16),RefY+(HBack/2)+dBack+(3*LTail/4),RefX+J,RefY+(HBack/2)+dBack+LTail);
    bezierVertex(RefX+(WBack/16),RefY+(HBack/2)+dBack+(2*LTail/4),RefX+(WBack/8),RefY+(HBack/2)+dBack+(3*LTail/4),RefX+(WBack/4),RefY+(3*HBack/8));
    endShape();
    
    //draw back turtle
    ellipse(RefX,RefY,WBack+(2*dBack),HBack+(2*dBack));
    ellipse(RefX,RefY,WBack,HBack);
    
    //draw line back turtle
    line(RefX,RefY-(HBack/2),RefX,RefY+(HBack/2));
    line(RefX-(WBack/2),RefY,RefX+(WBack/2),RefY);
    
}