ต่อไปนี้ จะป็นการวาดรูปแบบใช้ function แต่มีการนำ condition เข้ามาเพิ่มเพื่อให้ ภาพสามารถเคลื่อนไหวได้ ในครั้งนี้เราจะใช้ เงื่อนไขแบบซ้อนเงื่อนไข เพื่อให้มีการเคลื่อนไหวแบบกลับไปกลับมาเหมือนเดิมค่ะ
// ส่วนนี้เป็นการประกาศ ตัวแปรแบบ Global
int e=150;
int r=150;
int a=270;
int b=470;
int c=1;
int d=500;
int i=0;
int x=0;
void setup(){ //}//ต่อไปเป็นส่วนของ function void draw() ฟังก์ชั่นนี้ ก็มีการประกาศ กำหนด แต่ไม่ต้องเรียกใช้ เพราะมันจะเรียกใช้เองโดยอัตโนมัติ
size(500,500);//เป็นคำสั่งที่กำหนด canvas ของภาพนี้ โดย มีการกำหนดค่าโดย (ความกว้าง,ความสูง)
}//function แรกของการทำงาน จะประกาศ กำหนด แต่ไม่ต้องเรียกใช้ เพราะจะเรียกใช้เองอัตโนมัติ
void draw(){
background(245,178,217); //เป็นคำสั่งที่ใช้ในการกำหนดสีของพื้นหลัง
drawNet(a,b,c,d);//เป็นส่วนที่เรียกใช้void drawNet() โดยรับค่า a,b,c,d มาใช้ใน function (แบบ parameter)
drawVolleyball(e,r,x,i);//เป็นส่วนที่เรียกใช้void drawVolleyball()โดยรับค่าe,r,x,i มาใช้ใน function (แบบ parameter)
<blockquote>drawCartoon(i,x);//เป็นส่วนที่เรียกใช้void drawCartoon() โดยรับค่า i,x มาใช้ใน function (แบบ parameter)
if (i<340){
i=i+1;
}else{
if(x<-340){
x=5; i=5;
}else{
x=x-5;
}
}
}//เงื่อนไข i=i+1 ถ้า i<340 คือจะมีการเพิ่มขึ้น ทีละ 1 ขยับไปอีก 1 แต่เงื่อนไข x=x-5 ถ้าx<-340 จะมีการลดลงทีละ 5 คือขยับกลับมา
void drawNet(int a,int b,int c,int d){
stroke(1);
fill(random(13),random(255),random(253));
rect(c,270,d,200);
line(c,290,d,290);//horizental
line(c,330,d,330);
line(c,370,d,370);
line(c,410,d,410);
line(c,450,d,450);
line(20,a,20,b);//line
line(60,a,60,b);
line(100,a,100,b);
line(140,a,140,b);
line(180,a,180,b);
line(220,a,220,b);
line(260,a,260,b);
line(300,a,300,b);
line(340,a,340,b);
line(380,a,380,b);
line(420,a,420,b);
line(460,a,460,b);
line(490,a,490,b);
}//นี้จะเป็นการประกาศและกำหนด user defined function drawNet()ฟังก์ชันที่ใช้วาดตาข่าย
void drawVolleyball(int e,int r,int x,int i){
noStroke();
fill(random(112),random(99),random(240));//random คำสั่งที่ใช้สุ่มค่า ในที่นี้อยู่ใน fillจะทำการ สุ่มค่าสีในfill
ellipse(370,80+i+x,e,r);
fill(random(255),random(255),random(103));
ellipse(370,80+i+x,e,r-50);
fill(random(112),random(99),random(240));
ellipse(370,80+i+x,e,r-100);
}//นี้จะเป็นการประกาศและกำหนด user defined function drawVolleyball()ฟังก์ชันที่ใช้วาดลูกบอล
void drawCartoon(int i,int x){
strokeWeight(4);
fill(255,240,178);
ellipse(70+i+x,310,160,190);
fill(215,255,85);
ellipse(70+i+x,500,220,180);
noStroke();
fill(88,62,19);
ellipse(70+i+x,310,150,200);
}//นี้จะเป็นการประกาศและกำหนด user defined function drawCartoon() ฟังก์ชันที่ใช้วาดคน
ไม่มีความคิดเห็น:
แสดงความคิดเห็น