[Processing] Lesson Two(4~5) 精華

(用markdown記筆記的時候代碼格式復(fù)制不進來,誰可以告訴我怎么貼代碼格式可以更貼近真實一些,拜謝!)

2.變量

2.1 變量種類(type)

int:整數(shù)
float:小數(shù)
boolean:對或錯

注意

每個變量都要有種類(type),這樣電腦才知道給這個變量分配多少存儲空間;

每個變量都要有名稱。

2.2 變量名稱

系統(tǒng)變量(已存在)

width:窗口寬度(像素);
height:窗口高度(像素);
frameCount:運行過的幀數(shù);

自定義變量

如果較長最好取成"circleX","widthA"之類,不要用"Circlex"這種,因為系統(tǒng)變量很多事首字母大小寫的。

練習(xí):圓圈變大變色
Paste_Image.png

float circleX1=25;
float circleY1=25;
float circleX2=75;
float circleY2=25;
float circleX3=25;
float circleY3=75;
float circleX4=75;
float circleY4=75;
float circleSize=0;
float change=0.5;

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

void draw(){
background(255);
fill(circleX1);
ellipse(circleX1,circleY1,circleSize,circleSize);

fill(circleX2);
ellipse(circleX2,circleY2,circleSize,circleSize);

fill(circleX3);
ellipse(circleX3,circleY3,circleSize,circleSize);

fill(circleX4);
ellipse(circleX4,circleY4,circleSize,circleSize);

circleSize=circleSize+change;
}

2.3 隨機變量

random():注意random默認設(shè)置是小數(shù)變量。
random(255) 意思是 random(0,255);
例:
float w = random(1,100);
int x = int(random(1,100));要給整數(shù)設(shè)置隨機變量范圍時需要用 "int" 對其加以限制。

羞恥的練習(xí)

一直向上但在x軸隨機位置閃爍的程序生物Pia(截圖是動畫問題)
思考:如何讓Pia在x軸上閃爍的不那么頻繁


Paste_Image.png

float PiaX;
float PiaY;

float r;
float g;
float b;

void setup(){
size(200,200);
PiaY=0;
}

void draw(){
background(255);
PiaX = random(width);

//Pia's body
fill(150);
triangle(PiaX,200+PiaY,PiaX-10,160+PiaY,PiaX+10,160+PiaY);

//Pia's ear
fill(255);
line(PiaX-20,150+PiaY,100,150+PiaY);
ellipse(PiaX-20,150+PiaY,10,10);
ellipse(PiaX+20,150+PiaY,10,10);

//Pia's head
ellipse(PiaX,150+PiaY,20,20);

//Pia's eyes
r = random(255);
g = random(255);
b = random(255);
fill(r,g,b);
rectMode(CENTER);
rect(PiaX-5,145+PiaY,5,5);
rect(PiaX+5,145+PiaY,5,5);
PiaY = PiaY-1;
}

2.4 條件判定

15比20大——false;
5等于5——true;

用途

對變量使用條件判定來讓系統(tǒng)可以選擇符合判定的不同路徑運行

2.5 Conditionals:if, else, else if

(翻譯不準(zhǔn)確還是用原文標(biāo)題吧)

例子

Processing 中,我們有這樣邏輯的語句:
If the mouse is on the left side of the screen, draw a rectangle on the left side of the screen.

正式一些,程序可以寫成
if (mouseX < width/2){
fill(255);
rect(0,0,width/2,height/2);
}

Paste_Image.png
通俗化用法

只有 "if" 的:
if (boolean expression) {
// code to execute if boolean expression is true
}

加上 "else" 的情況(2種情況)
if (boolean expression) {
// code to execute if boolean expression is true
} else {
// code to execute if boolean expression is false
}

加上 "else if" 的情況(3種情況以上)
if (boolean expression #1) {
// code to execute if boolean expression #1 is true
} else if (boolean expression #2) {
// code to execute if boolean expression #2 is true
} else if (boolean expression #n) {
// code to execute if boolean expression #n is true
} else {
// code to execute if none of the above
// boolean expressions are true
}

2.6 邏輯操作

(不多敘述,或與非都比較熟悉了)
AND : &&
OR:||
NOT:!

2.7 反彈球編程練習(xí)

Tips

speed = speed * -1;(碰到窗口邊時,速度反向等大)
speed = speed * 0.95;(碰到窗口邊時,速度反向略小,模擬真實環(huán)境)
button = ! button; (用作switch,條件判定后false = true / true = false)

2.8 設(shè)置變量為不同狀態(tài),方便條件判定

通用例

int state = 0;

if (state == 0){
if(//條件){
state = 1;
}
}
現(xiàn)在state就從0變成了1。

練習(xí) Bouncing gravity ball

Paste_Image.png

//Bouncing ball
float x=0;
float y=0;
float speedY=0;
float gravity=0.3;
float speedX=3;
boolean button=false;
int r=0;
int g=255;
int b=0;
int change=1;

void setup(){
size(1200,400);
}

void draw(){
background(255);

//set button
if (button){
//simulate reality
rect(x,y,10,10);
x=x+speedX;
y=y+speedY;
speedY=speedY+gravity;
if (x>width || x<0){
speedX=speedX-1;
}
if (y>height){
speedY=speedY
-0.95;
}
}

if (mousePressed){
button=!button;
}

//change color
fill(r,g,b);
if(mouseX<width/2){
r=r+change;
}else if(mouseX>width/2){
r=r-change;
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容