(用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í):圓圈變大變色

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軸上閃爍的不那么頻繁

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);
}

通俗化用法
只有 "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

//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;
}
}