Processing
02 Condition 條件式
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); }
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; }
Moving
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; }
Void draw() is a FOR LOOP What we gonna use A LOT!!! 我們會用 很多
Save the File 先存檔八
CONDITION 條件式
Are you good at MATH?! 你數學老師有常常請假嗎?
NAME
EXAMPLE
IF_Else
If the sun is coming out, (I will play tennis) Else (I will do my design)
While
While I am in STUDIO (I am always doing my design)
Case
Case 1 (I am sleeping) Case 2 (I am eating) Case 3 (I am DESIGNING)
IF_Else 如果…其餘
Logic
condition
open
if (it’s sunny){ I will go swimming; } else{ I will stay at home; } close
open
close
If ( it’s sunny, ) {I will play tennis} All the conditions
else If (it’s cloudy,) {I will read} else if (it’s raining,) {I will do my lundry}
rest
else {I will do my design} {I will be killed}
Logic 邏輯等式
> < >= <= == !=
(greater than) (less than) (greater than or equal to) (less than or equal to) (equality) (inequality)
|| && !
(logical OR) (logical AND) (logical NOT)
That’s Enough!!! 夠了!!!
0
500
x < width / 2
500
x > width / 2
if your mouse is on the right side of the screen, The background will be “RED” 如果滑鼠移到螢幕右邊 背景為紅 else The background will be “GREEN” 其餘的話 背景為綠
How about 4 colors 四色呢?
0
500
x < width / 2 y < height / 2
x < width / 2 y > height / 2
500
x > width / 2 y < height / 2
x > width / 2 y > height / 2
Now you should understand it
void setup(){ size(500,500); smooth(); } void draw(){ if(mouseX<(width/2) && mouseY <(height/2)){ background(255,0,0); } if(mouseX<(width/2) && mouseY >(height/2)){ background(0,255,0); } if(mouseX>(width/2) && mouseY <(height/2)){ background(0,0,255); } if(mouseX>(width/2) && mouseY >(height/2)){ background(255,255,255); } }
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; }
Moving
Go all the way out! 跑掉了!
How about Bouncing 如何反彈
if(x > width){ speed = -speed; }
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; if( x > width ){ speed = -speed; } }
if(x < 0){ speed = -speed; }
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; if( x > width ){ speed = -speed; } if(x < 0){ speed = -speed; } }
if(x > width || x< 0){ speed = -speed; } || (shift+\ = |)
int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; if(x > width || x< 0){ speed = -speed; } }