วันอังคารที่ 24 กันยายน พ.ศ. 2556

-Limited UFO-




void setup ()
{
  size(300, 300);
}

Ufo u = new Ufo (50, 50);
int check=0;

void draw()
{
  background(0);  
  u.display();
}

class Ufo
{
  int x, y, r;

  Ufo(int a, int b)
  {

    this.x=a;
    this.y=b;
    this.r=0;
  }

  void display() {
    if(r<3){
    drawUFO();
    move();
    }
  }
  void move(){
 
    if(this.x<270&&this.y==50){
      this.x=this.x+5;
   
    }
    if(this.x==270&&this.y<270){
     this.y=this.y+5;
   
    }
    if(this.x>30&&this.y==270){
     this.x=this.x-5;
    }
    if(this.x==30&&this.y>50){
      this.y=this.y-5;
    }
    if(this.x==30&&this.y==50){
      r++;
    }
 
 
  }
  void drawUFO() {
   fill(250,255,0);
   triangle(x,y,x-15,y+30,x+15,y+30);
   fill(150);
   ellipse(x,y,30,30);
   fill(30, 144 ,255);
   ellipse(x,y+5,50,15);
 
  }
}

-Class UFO-



void setup ()
{
  size(300, 300);
}

Ufo u = new Ufo (50, 50);
int check=0;

void draw()
{
  background(0);  
  u.display();
}

class Ufo
{
  int x, y, z;

  Ufo(int a, int b)
  {

    this.x=a;
    this.y=b;
    this.z=0;
  }

  void display() {
    drawUFO();
    move();
  }
  void move(){
    if(this.x<270&&this.y==50){
      this.x=this.x+1;
   
    }
    if(this.x==270&&this.y<270){
     this.y=this.y+1;
   
    }
    if(this.x>30&&this.y==270){
     this.x=this.x-1;
    }
    if(this.x==30&&this.y>50){
      this.y=this.y-1;
    }
 
  }
  void drawUFO() {
   fill(250,255,0);
   triangle(x,y,x-15,y+30,x+15,y+30);
   fill(150);
   ellipse(x,y,30,30);
   fill(30, 144 ,255);
   ellipse(x,y+5,50,15);
 
  }
}

=Array=(Medium)

Programming Exercise
Proposition => Write a Function that returns the lowest subscript in an array. Also find a function that returns the highest subscript in an array.


Translated into Thailand => เขียนฟังก์ชั่นที่ รีเทิร์นค่า ที่น้อยที่สุดและค่าที่มากที่สุด ใน Array


Solution=> 
void setup(){
 println("Max :"+Max(x));   //แสดงข้อความบนหน้าจอ
 println("Min :"+Min(x));
}
int[] x={4,2,3,4,5,7,6};   //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 

int Max(int[] y){  //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
  int max=0;       //ประกาศและกำหนดค่า Maxในตอนแรก
  int i;
  for(i=0;i<y.length;i++){   //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMax 
   if(y[i]>max){
    max=y[i];         //เก็บค่าที่มีค่ามากกว่าค่าMaxคอนเริ่มต้นให้เป็นค่าMaxแทน
   }
  }
  return max;   //return max ไปที่ฟังก์ชัน
}

int Min(int[] m){      //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
 int min=m[0];      //ประกาศและกำหนดค่า Min ในตอนแรก
 int i;
 for(i=0;i<m.length;i++){  //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMin
   if(m[i]<min){
    min=m[i];         //เก็บค่าที่มีค่ามากกว่าค่าMinคอนเริ่มต้นให้เป็นค่าMinแทน
   }
  }
  return min;  //return min ไปที่ฟังก์ชัน

}



Out put => Max : 7
                  Min : 2

You can run code in http://processingjs.org/tools/processing-helper.html



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

=Array=(Eazy)

Programming Exercise
Proposition => Write the code to calculate the average of the element included in a one-dimensional double array named rates. The array has five element.

Translated into Thailand => เขียนโค้ดเพื่อคำนวนหาค่าเฉลี่ยของค่าองค์ประกอบที่อยู่ในอะเรย์1มิติ ข้อมูลภายในอะเรย์มี5ข้อมูล


Solution=> 
int[] rate={16,9,8,11,7}; //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 
int i;
float sum=0;
for(i=0;i<rate.length;i++){ //ลูปfor()ใช้ในการวนลูปเพื่อให้โปรแกรมทำงานซ้ำๆเมื่อเงื่อนไขเป็นจริง
  sum=sum+rate[i];              //ใช้ในการรวมผลบวกของทุกจำนวนในarray
}
println(sum/rate.length);   //แสดงค่าออกมาที่หน้าจอ

Out put => 10.2

You can run code in http://processingjs.org/tools/processing-helper.html



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

=Fuction=(Hard)

Programming Exercise
Proposition => Write the code for a function that receives four integers. The function should calculate the average of the four integer, and the return result .name the function calcAverage()

Translated into Thailand =>
เขียนโค้ดสำหรับ ฟังก์ชั่นที่รับจำนวนเต็ม 4 จำนวน,เป็นฟังก์ชั่นที่ใช้คำนวนหาค่าเฉลี่ย ของสี่จำนวนและรีเทิร์นค่า โดยใช้ชื่อฟังก์ชั่นcalcAverage()

Solution=> 

void setup() {
  println(calcAverage(7,18,15,29));  //แสดงค่าออกมาทางหน้าจอโดยค่าที่แสดง
                                                         ได้จากการเรียกใช้ฟังก์ชันcalcAverage( )
}

float calcAverage(int a,int b,int c,int d) { //สร้างฟังก์ชันโดยกำหนดให้มีการ return ค่ากลับ และมีparameter
  return (a+b+c+d)/4;
}


Out put =>  17.0

You can run code in http://processingjs.org/tools/processing-helper.html



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

=While=(Eazy)

Programming Exercise
Proposition =>Write a while clause that stops a posttest loop when the value in the quantity variable in less than the number 0.
Translated into Thailand =>  ให้เขียน while loop แล้วให้หยุดเมื่อค่า ในตัวแปลน้อยกว่า0

Solution=> 
int c=10;
while(c>=0){

 c--;

}





You can run code in http://processingjs.org/tools/processing-helper.html

อธิบายการทำงานของโปรแกรม => ทุกๆครั้งที่มีการวนลูป ค่าcจะลดลงทุกๆครั้งก่อนออกจากลูป
และจะจบการทำงานเมื่อเงื่อนไข c>=0 ไม่เป็นจริง


text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

-Class Button-




void setup() {
  size(200, 200);
  background(0);
}
Button a =new Button();     //ประกาศ object f =new Button( )ขึ้นมาเป็นButton
                                           (ซึ่งเป็นคลาสที่สร้างไว้)เป็นตัวแปรแบบ local variable
Button b =new Button("book");
void draw() {
  a.display();                        //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนดobject
                                             ที่ต้างการใช้ไว้ข้างหน้าmethod
  b.display();
}

class Button {       //เป็นการสร้างClass ขึ้นมาเพื่อเก็บData & Method
                                 ทำให้สะดวกในการใช้งานต่อไปในอนาคต
  String name;   //ประกาศ attribute name ขึ้นมาเป็นข้อความ ซึ่งเป็นตัวแปรแบบlocal variable
                                  แต่ตัวแปรนี้ method ภายในคลาสจะสามารถมองเห็นได้ด้วย
  int x, y, z;    //ประกาศ attribute x,y,z ขึ้นมาเป็นจำนวนเต้ม ซึ่งเป็นตัวแปรแบบlocal variable
                                  แต่ตัวแปรนี้ method ภายในคลาสจะสามารถมองเห็นได้ด้วย
  Button(String n) {     //constructor ใช้ในการกำหนด object
    this.name=n;
    x=100;
    y=100;
    z=55;
  }
  Button() {             //constructor defult ไม่มี parameter
    this.name="No";
    x=50;
    y=20;
    z=30;
  }
  void clicked() {   //method ที่ใช้ในการตรวจสอบพิกัดของปุ่มกับตำแหน่งที่กดเมาส์ว่าตรงกันไหม
    if (mousePressed==true&&mouseX<=x+z&&mouseX>=x&&mouseY>=y&&mouseY<=y+z) {
      println(" "+this.name);              //แสดงข้อคว่ามของปุ่มที่กด
      mousePressed=false;
    }
  }
  void display() {          //method ที่ใช้ในการแสดงภาพขึ้นCanvas
    rect(x, y, z, z);
    if (x==100) {
      fill(0);
      text("My name", x+2, y+30);
      fill(255);
    }
    a.clicked();
    b.clicked();
  }
}


//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

-Chess-




void setup()
{
  size(400, 400);
}
  int x,y,i,j;       //ประกาศตัวแปลเป็น Global variable เพื่อหัวทุกฟังก์ชันมองเห็นตัวแปลนี้
  int[][] Chess= {        //สร้างตัวแปรอาเรย์ 2 มิติ ชนิดจำนวนเต็ม และกำหนดค่า
    {
      1, 2, 3, 4, 5, 3, 2, 1
    }
    , {
      6, 6, 6, 6, 6, 6, 6, 6
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      0, 0, 0, 0, 0, 0, 0, 0
    }
    , {
      6, 6, 6, 6, 6, 6, 6, 6
    }
    , {
      1, 2, 3, 4, 5, 3, 2, 1
    }
  };

void draw(){
drawTa();            //เรียกใช้ฟังกฺชัน drawTa( )
drawChess();      //เรียกใช้ฟังก์ชัน drawwChess( )

}

void drawTa(){   //ฟังก์ชันนี้จะวาดตาราง
  stroke(0);         //กำหนดสีให้กับเส้น
  strokeWeight(2);      //ใส่ความหนาให้เส้น
   y=0;
   for (i=0;i<Chess.length;i++) {    //เป็นการวนลูปในอะเรย์มิติที่1
                                                       //for( )คล้ายกับคำสั่ง while แต่ต่างกันตรงที่
                                                            การกำหนดเงื่อนไขดังนี้
                      for(กำหนดค่าให้ตัวแปล,เงื่อนไขในการวน, เพิ่มค่าให้ตัวแปลทุกครั้งที่มีการวนลูป)
                                                 
    x=0;
    for (j=0;Chess[0].length>j;j++)   //เป็นการวนลูปที่ใช้ในการรันอะเรย์ในมิติที่2 
เมื่อออกจากลูปนี้ก็จะกลับไปวนลูปแรกใหม่  
เป็นการเขียนแบบ ลูปซ้อนลูป
    {
      if ((j+i)%2==0) fill(139, 134, 130);   //เป็นการกำหนดเงื่อนไขการใส่สีของตาราง
      else fill(139, 69, 19);            
      rect(x, y, 100, 100);
      x=x+50;    
    }
    y=y+50;
  }
}
void drawChess(){    //ฟังก์ชันนี้จะใช้วาดตัวหมาก
  y=25;
  for (i=0;i<Chess.length;i++) {   //เป็นการวนลูปในอะเรย์มิติที่1 ใช้ในการตรวจสอบแถว
    x=25;
    for (j=0;j<Chess[0].length;j++)   {  //เป็นการวนลูปที่ใช้ในการรันอะเรย์ในมิติที่2 
ใช้ในการเช็คว่าค่าที่เก็บไว้ข้างต้นเป็นตัวหมากอะไรบ้าง
       if (y<200){
        fill(0);
        stroke(255, 0,0 );
      }
      else if (y>200)
      {
        fill(255);
        stroke(0, 255, 0);
      }

      strokeWeight(3);
      if (Chess[i][j]==1)        
      {rook(x, y);}            

      else if (Chess[i][j]==2)      
      {knight(x, y);}          

      else if (Chess[i][j]==3)        
      {bishop(x, y);}        

      else if (Chess[i][j]==4)      
      {queen(x, y); }        

      else if (Chess[i][j]==5)  
      {king(x, y);}          

      else if (Chess[i][j]==6)  
      {
        if (y<200)  
        {stroke(255);}  
        else if (y>200)  
        {stroke(0);}        
        strokeWeight(5);  
        Chip(x, y);  
      }
      x=x+50;
    }
    y=y+50;
  }
}

void rook(int x, int y)  //ฟังก์ชันที่ใช้วาดเรือ
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}          
  else if (y>200)          
  {fill(0);}        
   text("rook", x-12, y+3);
}


void knight(int x, int y)   //ฟังก์ชันที่ใช้วาดม้า
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)          
  {fill(0);}        
   text("knight", x-16, y+3);
}

void bishop(int x, int y)   //ฟังก์ชันที่ใช้วาดบิชอป
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)        
  {fill(0);}        
   text("bishop", x-17, y+3);
}

void queen(int x, int y)  //ฟังก์ชันที่ใช้วาดควีน
{
  ellipse(x, y, 45, 45);
  if (200>y)
  {fill(255);}
  else if (y>200)
  {fill(0);}
  text("queen", x-15, y+3);
}

void king(int x, int y)  //ฟังก์ชันที่ใช้วาดคิง
{
  ellipse(x, y, 45, 45);
  if (200>y)            
  {fill(255);}            
  else if (y>200)        
  {fill(0);}      
   text("king", x-10, y+3);
}

void Chip(int x, int y) //ฟังก์ชันที่ใช้วาดตัวเบี้ย
{
  ellipse(x, y, 40, 40);  
}


//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

-Class Fraction-




void setup()
{
  Fraction f= new Fraction(2, 7);     //ประกาศ object f =new Fraction(2,7)ขึ้นมาเป็น Fraction
                                                        (ซึ่งเป็นคลาสที่สร้างไว้)เป็นตัวแปรแบบ local variable
  Fraction g= new Fraction(16,8); 
  g.low();                                       //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนดobject
                                                       ที่ต้างการใช้ไว้ข้างหน้าmethod
  text(g.toString(),50,50);           //คำสั่งที่ใช้ในการแสดงข้อความบน Canvas
}

class Fraction          //เป็นการสร้างClass ขึ้นมาเพื่อเก็บData & Method
                                 ทำให้สะดวกในการใช้งานต่อไปในอนาคต
{
  int n;                         //ประกาศ attribute n ขึ้นมาเป็นจำนวนเต้ม ซึ่งเป็นตัวแปรแบบlocal variable
                                  แต่ตัวแปรนี้ method ภายในคลาสจะสามารถมองเห็นได้ด้วย
  int d;  
  Fraction(int a, int b)  //constructor ใช้ในการกำหนด object
  {   
    this.n=a;
    this.d=b;
  }
  Fraction()                  //constructor defult ไม่มี parameter
  {  
    this.n=1;
    this.d=1;
  }
  void add(int z)        //method ที่ใช้ในการบวกเศษส่วนกับจำนวนเต็ม
  {
    this.n=(this.d*z)+this.n;
  }
  void add(Fraction k)    //method ที่ใช้ในการบวกเศษส่วนกับเศษส่วน 
                                      เป็นการเขียนแบบ Overloading ชื่อmethod เหมือนกัน
                                     แต่มีการรับ parameter ต่างกัน
  {
    this.n=(this.n*k.d)+(k.n*this.d);
    this.d=this.d*k.d;
  }
  void mutiply(int z)      //method ที่ใช้ในการคูณเศษส่วนกับจำนวนเต็ม
  {
    this.n=this.n*z;
  }
  void mutiply(Fraction k)      //method ที่ใช้ในการคูณเศษส่วนกับเศษส่วน 
  {
    this.n=this.n*k.n;
    this.d=this.d*k.d;
  }
  void low()                 //method ที่ใช้ในการทำให้เป็นเศษส่วนอย่างต่ำ 
    { int i=this.n;
      while(i>0)
      {
        if(this.n%i==0 && this.d%i==0)
        {
          this.n=this.n/i;
          this.d=this.d/i;
        }
        i--;
      }
    }
  String toString()
  {
    fill(0);
    return this.n+"/"+this.d;    //return ค่ากลับไปยัง method
  }
}


//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition


วันพฤหัสบดีที่ 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);
}

วันพุธที่ 18 กันยายน พ.ศ. 2556

-Reverse

void setup()
{
  String s = "Computer";                //เป็นการประกาศตัวแปลชนิด String หรือข้อความ
                                                     รูปแบบการใช้งาน String ตัวแปล = " ข้อความ";

  print("Reverse :"+reverseString(s));   //เป็นการแสดงค่าออกมาทางหน้าจอ 
                                                 รูปแบบการใช้งาน print("ข้อความที่ต้องการแสดง");
}


String reverseString(String A)           //เป็นการสร้าง function โดยกำหนดให้มีการ return ค่าเป็น String
                                                       หรือ ข้อความ   
{
  String B = "";
  int i;
  for (i=0;i<A.length();i++)      //คล้ายกับคำสั่ง while แต่ต่างกันตรงที่การกำหนดเงื่อนไข
                                                         ดังนี้ for(กำหนดค่าให้ตัวแปล,เงื่อนไขในการวน,
                                                         เพิ่มค่าให้ตัวแปลทุกครั้งที่มีการวนลูป)
  {
    B=B+(A.charAt(A.length()-1-i));
  }
  return B;
}

Out put : retupmoC





//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

-Maxtric Calculation-(Plus)




void setup() {
  int[][] Mtnum1 = {         //เป็นการสร้างตัวแปลArray 2 มิติ ชนิดจำนวนเต็ม
    {
      1, 3, 6
    }
    , {
      5, 2, 3
    }
    , {
      5, 2, 7
    }
  };
  int[][] Mtnum2 = {
    {
      5, 2, 4
    }
    , {
      6, 3, 3
    }
    , {
      4, 2, 2
    }
  };
  int[][] sum = new int[3][3];        //เป็นการกำหนดค่าความกว้างของArrayแต่ละมิติ
                                                    โดยช่องแรกกำนดว่าจะมีความกว้างของ index เ่ท่าไร
                                                     และช่องที่2กำหนดว่าข้างในของแต่ละ index
                                                      จะมีความกว้างเท่าไร
  int y=20,x;
  int i, j;
  for (i=0;i<Mtnum1.length;i++) {      //คล้ายกับคำสั่ง while แต่ต่างกันตรงที่การกำหนดเงื่อนไข
                                                         ดังนี้ for(กำหนดค่าให้ตัวแปล,เงื่อนไขในการวน,
                                                         เพิ่มค่าให้ตัวแปลทุกๆครั้งที่มีการวนลูป)
    x=10;
    for (j=0;j<Mtnum1[i].length;j++) {
      sum[i][j] = Mtnum1[i][j]+Mtnum2[i][j];
      fill(0);
      text(" "+sum[i][j],x,y);                 //เป็นคำสั่งแสดงข้อความโดยกำหนดดังนี้
                                                       ("ข้อความ"+ค่าที่ต้องการแสดง,ตำแน่งแกนx,ตำแหน่งแกนy)
      x=x+20;
    }
    y=y+20;
 
 
  }
}

-Palindrome-

void setup()
{
  String s = "Computer";     //เป็นการประกาศตัวแปลชนิด String หรือข้อความ
                                          รูปแบบการใช้งาน String ตัวแปล = " ข้อความ";
  if (isPalindrome(s)) {
println(s + " is a palindrome");  //เป็นการแสดงค่าออกมาทางหน้าจอ 
                                                 รูปแบบการใช้งาน println("ข้อความที่ต้องการแสดง");
} else {
println(s + " is not a palindrome");
}
  print("Reverse :"+reverseString(s));
}

boolean isPalindrome(String s){    //เป็นการสร้าง function โดยกำหนดให้มีการ return ค่าเป็น true
                                                     หรือ false 
String t=reverseString(s);
int i,sum=0;
for(i=0;i<s.length();i++)                 //คล้ายกับคำสั่ง while แต่ต่างกันตรงที่การกำหนดเงื่อนไข
                                                         ดังนี้ for(กำหนดค่าให้ตัวแปล,เงื่อนไขในการวน,
                                                         เพิ่มค่าให้ตัวแปลทุกครั้งที่มีการวนลูป)
{if(t.charAt(i)==s.charAt(i))
{sum++;}
}

if(sum==s.length()) return true;           //เป็นการส่งค่ากลับไปให้ function
else return false;

}

String reverseString(String A)          //เป็นการสร้าง function โดยกำหนดให้มีการ return ค่าเป็น String
                                                       หรือ ข้อความ                                               
{
  String B = "";
  int i;
  for (i=0;i<A.length();i++)
  {
    B=B+(A.charAt(A.length()-1-i));
  }

  return B;
}
Out put : Computer is not a palindrome
               Reverse : retupmoC




//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

=While=(H)

Programming Exercise
Proposition => In the exercise ,you use the nested loop to display a pattern of asterisk. (9 asterisk,8 asterisk,7 asterisk,6 asterisk,5 asterisk,4 asterisk,3 asterisk,2 asterisk,1 asterisk)

Translated into Thailand =>ให้ใช้ลูปซ้อนลูปในการแสดงวงกลม 9,8,7,6,5,4,3,2,1 วงกลม



Solution=> 
int x;
int y=10;
int r=10;
int n=9;
int count=9;
int c;
while(count<=n){  //ลูปแรกใช้ในการวนตามจำนวนแถวโดยจำนวนแถวขึ้นอยู่กับค่าn
  c=1;x=10;            
  while(c<=count){  //ลูปที่2ใช้ในการวนตามจำนวนoในแต่ล่ะแถว
    ellipse(x,y,r,r);  //คำสั่งใช้วาดวงกลม
    c++;
    x=x+r;  
  }
  y=y+r;
  count--;
}


Out put =>ooooooooo
                 oooooooo
                 ooooooo
                 oooooo
                 ooooo
                 oooo
                 ooo
                 oo
                 o

You can run code in http://processingjs.org/tools/processing-helper.html






text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

วันอาทิตย์ที่ 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);
}