Processing 代碼


size(200,200); 
background(255); 
stroke(0); 
fill(150); 
rect(50,50,75,100); 

長方形代碼

size(200,200); 
smooth(); 
background(255); 
noFill(); 
stroke(0); 
ellipse(60,60,100,100); 

圓形代碼

smooth(); 
background(255); 
noStroke(); 
fill(255,0,0); 
ellipse(20,20,16,16); 
fill(127,0,0); 
ellipse(40,20,16,16); 
fill(255,200,200); 
ellipse(60,20,16,16); 

三個不同顏色的圓圈代碼

size(200,200); 
background(0); 
noStroke(); 

fill(0,0,255); 
rect(0,0,100,200); 

fill(255,0,0,255); 
rect(0,0,200,40); 

fill(255,0,0,191); 
rect(0,50,200,40); 

fill(255,0,0,127); 
rect(0,100,200,40); 

fill(255,0,0,63); 
rect(0,150,200,40); 

這個是長方形透明度代碼

size(200,200); 
background(255); 
smooth(); 
ellipseMode(CENTER); 
rectMode(CENTER); 

stroke(0); 
fill(150); 
rect(100,100,20,100); 

// Head 
fill(255); 
ellipse(100,70,60,60); 

fill(0); 
ellipse(81,70,16,32); 
ellipse(119,70,16,32); 

stroke(0); 
line(90,150,80,160); 
line(110,150,120,160); 

這個是機器人形象代碼

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

void draw() { 

background(255); 


stroke(0); 
fill(175); 
rectMode(CENTER); 

rect(mouseX,mouseY,50,50); 
} 

這個是四方塊跟蹤鼠標的代碼

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 

ellipseMode(CENTER); 
rectMode(CENTER); 

stroke(0); 
fill(175); 

rect(mouseX,mouseY,20,100); 

stroke(0); 
fill(255); 

ellipse(mouseX,mouseY-30,60,60); 

fill(0); 
ellipse(81,70,16,32); 
ellipse(119,70,16,32); 

stroke(0); 
line(90,150,80,160); 
line(110,150,120,160); 
} 
這個是結合了靜止狀態的機器人和機器人外表跟蹤鼠標的結合代碼

void setup() { 
size(200, 200); 
background(255); 
smooth(); 
} 

void draw() { 
stroke(0); 

line(pmouseX, pmouseY, mouseX, mouseY); 
} 

這個是連筆畫的代碼


void setup() { 
size(200,200); 
background(255); 
} 

void draw() { 

} 

void mousePressed() { 
stroke(0); 
fill(175); 
rectMode(CENTER); 
rect(mouseX,mouseY,16,16); 
} 

void keyPressed() { 
background(255); 
} 

這個是當鼠標在任意地方按左鍵時出現四方塊代碼


void setup() { 

size(200,200); 
smooth(); 

frameRate(30); 
} 

void draw() { 

background(255); 


ellipseMode(CENTER); 
rectMode(CENTER); 


stroke(0); 
fill(175); 
rect(mouseX,mouseY,20,100); 


stroke(0); 
fill(255); 
ellipse(mouseX,mouseY-30,60,60); 


fill(mouseX,0,mouseY); 
ellipse(mouseX-19,mouseY-30,16,32); 
ellipse(mouseX+19,mouseY-30,16,32); 


stroke(0); 

line(mouseX-10,mouseY+50,pmouseX-10,pmouseY+60); 
line(mouseX+10,mouseY+50,pmouseX+10,pmouseY+60); 
} 

void mousePressed() { 
println("Take me to your leader!"); 
} 

這個是機器人跟蹤鼠標的基礎上2腳會根據慣性晃盪的代碼

int circleX = 100; 
int circleY = 100; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 

ellipse(circleX,circleY,50,50); 
} 

窗口中間畫了圓形代碼,

int circleX = 0; 
int circleY = 100; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 

ellipse(circleX,circleY,50,50); 


circleX = circleX + 1; 
} 

這是圓形沿着中線從左向右走一遍的代碼

float circleX = 0; 
float circleY = 0; 
float circleW = 50; 
float circleH = 100; 
float circleStroke = 255; 
float circleFill = 0; 
float backgroundColor = 255; 
float change = 0.5; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 

background(backgroundColor); 
stroke(circleStroke); 
fill(circleFill); 
ellipse(circleX,circleY,circleW,circleH); 

circleX = circleX + change; 
circleY = circleY + change; 
circleW = circleW + change; 
circleH = circleH - change; 
circleStroke = circleStroke - change; 
circleFill = circleFill + change; 
} 


這個是一個圓形從左上角到右下角移動一遍的過程中由圓形變成橢圓然後翻轉到圓形 
期間還有由黑色漸變成灰色的代碼

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(100); 
stroke(255); 

fill(frameCount / 2); 
rectMode(CENTER); 

rect(width/2,height/2,mouseX+10,mouseY+10); 
} 

void keyPressed() { 
println(key); 
} 

這個是方塊隨着鼠標移動便形狀,期間黑色變成灰色然後變成白色又變回灰色的代碼

float r = 100; 
float g = 150; 
float b = 200; 
float a = 200; 

float diam = 20; 
float x = 100; 
float y = 100; 

void setup() { 
size(200,200); 
background(255); 
smooth(); 
} 

void draw() { 

stroke(0); 
fill(r,g,b,a); 
ellipse(x,y,diam,diam); 
} 

中間一個藍色黑邊的小圓圈代碼

float r; 
float g; 
float b; 
float a; 

float diam; 
float x; 
float y; 

void setup() { 
size(200,200); 
background(255); 
smooth(); 
} 

void draw() { 

r = random(255); 
g = random(255); 
b = random(255); 
a = random(255); 
diam = random(20); 
x = random(width); 
y = random(height); 


noStroke(); 
fill(r,g,b,a); 
ellipse(x,y,diam,diam); 
} 

各種各樣顏色的小圓圈出現然後一點一點的填滿整個屏幕的代碼

float zoogX; 
float zoogY; 

float eyeR; 
float eyeG; 
float eyeB; 

void setup() { 
size(200,200); 

zoogX = width/2; 
zoogY = height + 100; 
smooth(); 
} 

void draw() { 

background(255); 


ellipseMode(CENTER); 
rectMode(CENTER); 

stroke(0); 
fill(150); 

rect(zoogX,zoogY,20,100); 


stroke(0); 
fill(255); 
ellipse(zoogX,zoogY-30,60,60); 

function. 
eyeR = random(255); 
eyeG = random(255); 
eyeB = random(255); 
fill(eyeR,eyeG,eyeB); 

ellipse(zoogX-19,zoogY-30,16,32); 
ellipse(zoogX+19,zoogY-30,16,32); 


stroke(150); 
line(zoogX-10,zoogY+50,zoogX-10,height); 
line(zoogX+10,zoogY+50,zoogX+10,height); 

zoogY = zoogY - 1; 

} 


機器人由下而上出現,眼睛各種顏色閃,腳還特長,估計看不到底了

float r = 150; 
float g = 0; 
float b = 0; 

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

void draw() { 

background(r,g,b); 
stroke(255); 
line(width/2,0,width/2,height); 

if(mouseX > width/2) { 
r = r + 1; 
} else { 
r = r - 1; 
} 

if (r > 255) { 
r = 255; 
} else if (r < 0) { 
r = 0; 
} 
} 


屏幕被白線一分爲二,鼠標移到左半部分北京變黑,到右半部分背景漸漸變紅


float r = 0; 
float b = 0; 
float g = 0; 

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

void draw() { 

background(r,g,b); 
stroke(0); 
line(width/2,0,width/2,height); 
line(0,height/2,width,height/2); 

if (mouseX > width/2) { 
r = r + 1; 
} else { 
r = r - 1; 
} 

if (mouseY > height/2) { 
b = b + 1; 
} else { 
b = b - 1; 
} 

if (mousePressed) { 
g = g + 1; 
} else { 
g = g - 1; 
} 

r = constrain(r,0,255); 
g = constrain(g,0,255); 
b = constrain(b,0,255); 
} 


這個屏幕一分爲四,不同區域背景顏色變成不同顏色,跟上面類似,不過鼠標移除屏幕是背景顏色變黑

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

void draw() { 
background(255); 
stroke(0); 
line(100,0,100,200); 
line(0,100,200,100); 

noStroke(); 
fill(0); 

if (mouseX < 100 && mouseY < 100) { 
rect(0,0,100,100); 
} else if (mouseX > 100 && mouseY < 100) { 
rect(100,0,100,100); 
} else if (mouseX < 100 && mouseY > 100) { 
rect(0,100,100,100); 
} else if (mouseX > 100 && mouseY > 100) { 
rect(100,100,100,100); 
} 
} 

屏幕四分,鼠標移動到任意一個區域,區域部分變黑,跟上面不同

boolean button = false; 

int x = 50; 
int y = 50; 
int w = 100; 
int h = 75; 

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

void draw() { 

if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) { 
button = true; 
} else { 
button = false; 
} 

if (button) { 
background(255); 
stroke(0); 
} else { 
background(0); 
stroke(255); 
} 

fill(175); 
rect(x,y,w,h); 
} 

屏幕中間有灰色方塊,左鍵點擊北京由黑變白

boolean button = false; 

int x = 50; 
int y = 50; 
int w = 100; 
int h = 75; 

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

void draw() { 
if (button) { 
background(255); 
stroke(0); 
} else { 
background(0); 
stroke(255); 
} 

fill(175); 
rect(x,y,w,h); 
} 

void mousePressed() { 
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { 
button = !button; 
} 
} 

在上面代碼基礎上,這次是點擊一次變顏色,上面代碼是點完了恢復原來顏色 

看得出作者是循循漸進的形式寫代碼。讓我們學習

int x = 0; 
int speed = 1; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 

x = x + speed; 

if ((x > width) || (x < 0)) { 

speed = speed * -1; 
} 

stroke(0); 
fill(175); 
ellipse(x,100,32,32); 
} 

在上面看到過一個圓圈沿着中線移動一遍的代碼吧?這次是基礎上讓圓圈碰到屏幕邊緣後來回的移動

float c1 = 0; 
float c2 = 255; 

float c1dir = 0.1; 

float c2dir = -0.1; 

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

void draw() { 
noStroke(); 

fill(c1,0,c2); 
rect(0,0,100,200); 

fill(c2,0,c1); 
rect(100,0,100,200); 

c1 = c1 + c1dir; 
c2 = c2 + c2dir; 

if (c1 < 0 || c1 > 255) { 
c1dir *= -1; 
} 

if (c2 < 0 || c2 > 255) { 
c2dir *= -1; 
} 
} 

屏幕一分爲二,左邊藍色右邊紅色,按正常來說畫上2個方塊填充顏色就完了 
爲什麼這個代碼這麼複雜?搞不懂高手的用意

int x = 0; 
int y = 0; 

int speed = 5; 


int state = 0; 

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

void draw() { 
background(255); 

stroke(0); 
fill(175); 
rect(x,y,9,9); 

if (state == 0) { 
x = x + speed; 

if (x > width-10) { 
x = width-10; 
state = 1; 
} 
} else if (state == 1) { 
y = y + speed; 
if (y > height-10) { 
y = height-10; 
state = 2; 
} 
} else if (state == 2) { 
x = x - speed; 
if (x < 0) { 
x = 0; 
state = 3; 
} 
} else if (state == 3) { 
y = y - speed; 
if (y < 0) { 
y = 0; 
state=0; 
} 
} 
} 

一個小方塊沿着屏幕四周移動

float x = 100; 
float y = 0; 

float speed = 0; 

float gravity = 0.1; 

void setup() { 
size(200,200); 

} 

void draw() { 
background(255); 

fill(175); 
stroke(0); 
rectMode(CENTER); 
rect(x,y,10,10); 

y = y + speed; 

speed = speed + gravity; 

if (y > height) { 

speed = speed * -0.95; 
} 
} 


在高處的小方塊由於重力落到地面彈起,逐漸減少反彈高度,最終落到地面

float x = 100; 
float y = 100; 
float w = 60; 
float h = 60; 
float eyeSize = 16; 

float xspeed = 3; 
float yspeed = 1; 


void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 

x = x + xspeed; 
y = y + yspeed; 

if ((x > width) || (x < 0)) { 
xspeed = xspeed * -1; 
} 

if ((y > height) || (y < 0)) { 
yspeed = yspeed * -1; 
} 

background(255); 
ellipseMode(CENTER); 
rectMode(CENTER); 

fill(150); 
rect(x,y,w/6,h*2); 

fill(255); 
ellipse(x,y-h/2,w,h); 

fill(0); 
ellipse(x-w/3+1,y-h/2,eyeSize,eyeSize*2); 
ellipse(x+w/3-1,y-h/2,eyeSize,eyeSize*2); 

stroke(0); 
line(x-w/12,y+h,x-w/4,y+h+10); 
line(x+w/12,y+h,x+w/4,y+h+10); 
} 

機器人移動速度不變,碰到四面屏幕邊緣後反彈來回撞

ize(200,200); 
background(255); 

stroke(0); 
line(50,60,50,80); 
line(60,60,60,80); 
line(70,60,70,80); 
line(80,60,80,80); 
line(90,60,90,80); 
line(100,60,100,80); 
line(110,60,110,80); 
line(120,60,120,80); 
line(130,60,130,80); 
line(140,60,140,80); 
line(150,60,150,80); 

很多線

int y = 80; 
int x = 0; 
int spacing = 10; 
int len = 20; 
int endLegs = 150; 

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

void draw() { 
background(0); 
stroke(255); 
. 
while (x <= endLegs) { 
line(x,y,x,y + len); 

x = x + spacing; 
} 
} 

很多線,不用寫出每一條線的座標

size(200,200); 
background(255); 

int y = 80; 
int spacing = 10; 
int len = 20; 


for (int x = 50; x <= 150; x += spacing) { 
line(x,y,x,y + len); 
} 

很多線,精簡代碼

size(200,200); 
background(255); 

stroke(0); 
int y = 80; 
int x = 50; 
int spacing = 10; 
int len = 20; 

line(x,y,x,y + len); 

x = x + spacing; 

line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 
x = x + spacing; 
line(x,y,x,y + len); 

很多線,表達式不同

size(200,200); 
background(255); 

int y = 80; 
int x = 50; 
int spacing = 10; 
int len = 20; 

// A variable to mark where the legs end. 
int endLegs = 150; 
stroke(0); 

while (x <= endLegs) { 
line (x,y,x,y + len); 
x = x + spacing; 
} 

很多線,不同的代碼方式


void setup() { 
size(200,200); 

} 

void draw() { 
background(0); 
int x = 0; 

while (x < width) { 
stroke(255); 
line(x,0,x,height); 
x += 5; 
} 
} 

void mousePressed() { 

println( " The mouse was pressed! " ); 
} 

全屏隔間很多白線,

int y = 0; 

void setup() { 
size(200,200); 
background(255); 

frameRate(5); 
} 

void draw() { 

stroke(0); 

line(0,y,width,y); 

y += 10; 


if (y > height) { 
y = 0; 
} 
} 

在上面代碼的基礎上,讓線一條一條的出現

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

void draw() { 
background(0); 

int i = 0; 

while (i < width) { 
noStroke(); 

float distance = abs(mouseX - i); 

fill(distance); 
rect(i,0,10,height); 

i += 10; 
} 
} 

跟着鼠標移動的顏色區間

int x = 100; 
int y = 100; 
int w = 60; 
int h = 60; 
int eyeSize = 16; 
int speed = 1; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 

x = x + speed; 

if ((x > width) || (x < 0)) { 
speed = speed * -1; 
} 

background(255); // Draw a white background 

ellipseMode(CENTER); 
rectMode(CENTER); 

for (int i = y + 5; i < y + h; i += 10) { 
stroke(0); 
line(x-w/3,i,x + w/3,i); 
} 

// Draw Zoog's body 
stroke(0); 
fill(175); 
rect(x,y,w/6,h*2); 

fill(255); 
ellipse(x,y-h/2,w,h); 

fill(0); 
ellipse(x-w/3+1,y-h/2,eyeSize,eyeSize*2); 
ellipse(x+w/3-1,y-h/2,eyeSize,eyeSize*2); 

stroke(0); 
line(x-w/12,y + h,x-w/4,y + h + 10); 
line(x+w/12,y + h,x + w/4,y + h + 10); 
} 

機器人變成多足蜈蚣,左右來回的運動


int w = 60; 
int h = 60; 
int eyeSize = 16; 

void setup() { 
size(400,200); 
smooth(); 
} 

void draw() { 
background(255); 

ellipseMode(CENTER); 
rectMode(CENTER); 

int y = height/2; 

for (int x = 80; x < width; x += 80) { 


stroke(0); 
fill(175); 
rect(x,y,w/6,h*2); 

fill(255); 
ellipse(x,y-h/2,w,h); 

fill(0); 
ellipse(x-w/3,y-h/2,eyeSize,eyeSize*2); 
ellipse(x+w/3,y-h/2,eyeSize,eyeSize*2); 

stroke(0); 
line(x-w/12,y+h,x-w/4,y+h+10); 
line(x+w/12,y+h,x+w/4,y+h+10); 
} 

} 

四個相同的機器人

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

void draw() { 
background(255); 
drawBlackCircle(); 
} 

void drawBlackCircle() { 
fill(0); 
ellipse(50,50,20,20); 
} 

黑色圓

int x = 0; 
int speed = 1; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
move(); 
bounce(); 
display(); 
} 

void move() { 

x = x + speed; 
} 

void bounce() { 

if ((x > width) || (x < 0)) { 
speed = speed * - 1; 
} 
} 

void display() { 
stroke(0); 
fill(175); 
ellipse(x,100,32,32); 
} 

圓圈來回橫移,記得前面有一個案例

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

void draw() { 
background(0); 
stroke(0); 

fill(distance(0,0,mouseX,mouseY)); 

rect(0,0,width/2 - 1,height/2 - 1); 
// Top right square 
fill(distance(width,0,mouseX,mouseY)); 
rect(width/2,0,width/2 - 1,height/2 - 1); 

fill(distance(0,height,mouseX,mouseY)); 
rect(0,height/2,width/2 - 1,height/2 - 1); 

fill(distance(width,height,mouseX,mouseY)); 
rect(width/2,height/2,width/2 - 1,height/2 - 1); 
} 

float distance(float x1, float y1, float x2, float y2) { 
float dx = x1 - x2; 
float dy = y1 - y2; 
float d = sqrt(dx*dx + dy*dy); 
return d; 
} 

四個區域,鼠標到一個區域,四個區域顏色同時變,之前是變一個

float x = 100; 
float y = 100; 
float w = 60; 
float h = 60; 
float eyeSize = 16; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 

float factor = constrain(mouseX/10,0,5); 


jiggleZoog(factor); 

float d = dist(x,y,mouseX,mouseY); 
color c = color(d); 
drawZoog(c); 
} 

void jiggleZoog(float speed) { 

x = x + random( - 1,1)*speed; 
y = y + random( - 1,1)*speed; 

x = constrain(x,0,width); 
y = constrain(y,0,height); 
} 

void drawZoog(color eyeColor) { 

ellipseMode(CENTER); 
rectMode(CENTER); 

for (float i = y - h/3; i < y + h/2; i += 10) { 
stroke(0); 
line(x - w/4,i,x + w/4,i); 
} 

stroke(0); 
fill(175); 
rect(x,y,w/6,h); 

stroke(0); 
fill(255); 
ellipse(x,y - h,w,h); 

fill(eyeColor); 
ellipse(x - w/3,y - h,eyeSize,eyeSize*2); 
ellipse(x + w/3,y - h,eyeSize,eyeSize*2); 

stroke(0); 
line(x - w/12,y + h/2,x - w/4,y + h/2 + 10); 
line(x + w/12,y + h/2,x + w/4,y + h/2 + 10); 
} 

多足機器人隨機移動,同時震動

Car myCar; 

void setup() { 
size(200,200); 

myCar = new Car(); 
} 

void draw() { 
background(255); 

myCar.move(); 
myCar.display(); 
} 

class Car { 
color c; 
float xpos; 
float ypos; 
float xspeed; 

Car() { 
c = color(175); 
xpos = width/2; 
ypos = height/2; 
xspeed = 1; 
} 

void display() { . 

rectMode(CENTER); 
stroke(0); 
fill(c); 
rect(xpos,ypos,20,10); 
} 

void move() { 
xpos = xpos + xspeed; 
if (xpos > width) { 
xpos = 0; 
} 
} 
} 

無限小方塊從左到右一個一個橫移

Car myCar1; 
Car myCar2; 
void setup() { 
size(200,200); 
myCar1 = new Car(color(255,0,0),0,100,2); 
myCar2 = new Car(color(0,0,255),0,10,1); 
} 

void draw() { 
background(255); 
myCar1.move(); 
myCar1.display(); 
myCar2.move(); 
myCar2.display(); 
} 

class Car { 
color c; 
float xpos; 
float ypos; 
float xspeed; 

Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
c = tempC; 
xpos = tempXpos; 
ypos = tempYpos; 
xspeed = tempXspeed; 
} 

void display() { 
stroke(0); 
fill(c); 
rectMode(CENTER); 
rect(xpos,ypos,20,10); 
} 

void move() { 
xpos = xpos + xspeed; 
if (xpos > width) { 
xpos = 0; 
} 
} 
} 

在上面代碼的情況下,現在2個小方塊在移動,速度不一樣

Catcher catcher; 

void setup() { 
size(400,400); 
catcher = new Catcher(32); 
smooth(); 
} 

void draw() { 
background(255); 
catcher.setLocation(mouseX,mouseY); 
catcher.display(); 
} 

圓圈跟隨鼠標

Ball ball1; 
Ball ball2; 

void setup() { 
size(400,400); 
smooth(); 

ball1 = new Ball(64); 
ball2 = new Ball(32); 
} 

void draw() { 
background(255); 

ball1.move(); 
ball2.move(); 
ball1.display(); 
ball2.display(); 
} 

2個圓圈隨機運動,重疊是有透明

Ball ball1; 
Ball ball2; 

void setup() { 
size(400,400); 
smooth(); 

ball1 = new Ball(64); 
ball2 = new Ball(32); 
} 

void draw() { 
background(255); 

ball1.move(); 
ball2.move(); 

if (ball1.intersect(ball2)) { 
ball1.highlight(); 
ball2.highlight(); 
} 

ball1.display(); 
ball2.display(); 
} 

2個球,隨機不同移動速度運動

int savedTime; 
int totalTime = 5000; 

void setup() { 
size(200,200); 
background(0); 
savedTime = millis(); 
} 

void draw() { 

int passedTime = millis() - savedTime; 

if (passedTime > totalTime) { 
println( " 5 seconds have passed! " ); 
background(random(255)); 
savedTime = millis(); 
} 
} 

背景顏色每5秒換一個顏色

Timer timer; 

void setup() { 
size(200,200); 
background(0); 
timer = new Timer(5000); 
timer.start(); 
} 

void draw() { 
if (timer.isFinished()) { 
background(random(255)); 
timer.start(); 
} 
} 

每5秒背景換一個顏色,跟上面代碼不同

float x,y; 

void setup() { 
size(400,400); 
background(0); 
x = width/2; 
y = 0; 
smooth(); 
} 

void draw() { 
background(255); 

fill(50,100,150); 
noStroke(); 
ellipse(x,y,16,16); 

y++ ; 
} 

小球勻速向下移動

Drop[] drops = new Drop[1000]; 

int totalDrops = 0; 

void setup() { 
size(400,400); 
smooth(); 
background(0); 
} 

void draw() { 
background(255); 

drops[totalDrops] = new Drop(); 
totalDrops++ ; 

if (totalDrops >= drops.length) { 
totalDrops = 0; //Start over 
} 

for (int i = 0; i < totalDrops; i++ ) { 
drops[i].move(); 
drops[i].display(); 
} 

} 

在上面基礎上,做了很多雨滴以不同速度下落

background(255); 
smooth(); 

for (int i = 2; i < 8; i++ ) { 
noStroke(); 
fill(0); 
ellipse(width/2,height/2 + i*4,i*2,i*2); 
} 

雨滴形狀

Catcher catcher; 
Timer timer; 
Drop[] drops; 
int totalDrops = 0; 

void setup() { 
size(400,400); 
smooth(); 
catcher = new Catcher(32); 
drops = new Drop[1000]; 
timer = new Timer(2000); 
timer.start(); 
} 

void draw() { 
background(255); 

catcher.setLocation(mouseX,mouseY); 
catcher.display(); 

if (timer.isFinished()) { 
println( " 2 seconds have passed! " ); 
timer.start(); 
} 

drops[totalDrops] = new Drop(); 

totalDrops ++ ; 

if (totalDrops >= drops.length) { 
totalDrops = 0; // Start over 
} 

for (int i = 0; i < totalDrops; i ++ ) { 
drops[i].move(); 
drops[i].display(); 
} 
} 

雨滴每過2秒減速

Catcher catcher; 
Timer timer; 
Drop[] drops; 
int totalDrops = 0; 

void setup() { 
size(400,400); 
smooth(); 
catcher = new Catcher(32); 
drops = new Drop[1000]; 
timer = new Timer(300); 
timer.start(); 
} 

void draw() { 
background(255); 

catcher.setLocation(mouseX,mouseY); 

catcher.display(); 

if (timer.isFinished()) { 

drops[totalDrops] = new Drop(); 

totalDrops ++ ; 

if (totalDrops >= drops.length) { 
totalDrops = 0; 
} 
timer.start(); 
} 

for (int i = 0; i < totalDrops; i++ ) { 
drops[i].move(); 
drops[i].display(); 
if (catcher.intersect(drops[i])) { 
drops[i].caught(); 
} 
} 
} 

雨滴隨機以不同速度下落,小球跟蹤鼠標,碰到雨滴,雨滴消失。 
屬於小遊戲

float[] randoms = new float[4]; 

int index = 0; 

void setup() { 
size(200,200); 

for (int i = 0; i < randoms.length; i ++ ) { 
randoms[i] = random(0,256); 
} 

frameRate(1); 
} 

void draw() { 

background(randoms[index]); 

index = (index + 1) % randoms.length; 
} 

背景顏色1秒變一次

float[] randomCounts; 

void setup() { 
size(200,200); 
randomCounts = new float[20]; 
} 

void draw() { 
background(255); 

int index = int(random(randomCounts.length)); 
randomCounts[index] ++ ; 

stroke(0); 
fill(175); 
for (int x = 0; x < randomCounts.length; x ++ ) { 
rect(x*10,0,9,randomCounts[x]); 
} 
} 

不同位置的方塊從上而下移動

void setup() { 
size(200,200); 
background(255); 
smooth(); 
noStroke(); 
} 

void draw() { 

float red_prob = 0.60; 
float green_prob = 0.10; 
float blue_prob = 0.30; 

float num = random(1); 

if (num < red_prob) { 
fill(255,53,2,150); 

} else if (num < green_prob + red_prob) { 
fill(156,255,28,150); 

} else { 
fill(10,52,178,150); 
} 


ellipse(random(width),random(height),64,64); 
} 

不同顏色的半透明圓形圖片快速填滿屏幕

float time = 0.0; 
float increment = 0.01; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 

time += increment; 

fill(0); 
ellipse(width/2,height/2,n,n); 
} 

圓圈隨機變大變小

float r = 75; 
float theta = 0; 

void setup() { 
size(200,200); 
background(255); 
smooth(); 
} 

void draw() { 

float x = r * cos(theta); 
float y = r * sin(theta); 

noStroke(); 
fill(0); 
ellipse(x + width/2, y + height/2, 16, 16); 

theta += 0.01; 
} 

圓圈畫圓圈


float theta = 0.0; 

void setup() { 
size(200,200); 
smooth(); 
} 


void draw() { 
background(255); 
ellipse's x location. 
float x = (sin(theta) + 1) * width/2; 

theta += 0.05; 

fill(0); 
stroke(0); 
line(width/2,0,x,height/2); 
ellipse(x,height/2,16,16); 
} 

擺鐘

float theta = 0.0; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 

theta += 0.02; 
noStroke(); 
fill(0); 
float x = theta; 
dimension of the window). 
for (int i = 0; i <= 20; i++) { 

float y = sin(x)*height/2; 

x += 0.2; 
} 
} 

球形波浪動態圖

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 
noFill(); 
drawCircle(width/2,height/2,100); 
} 

void drawCircle(float x, float y, float radius) { 
ellipse(x, y, radius, radius); 
if(radius > 2) { 

drawCircle(x + radius/2, y, radius/2); 
drawCircle(x - radius/2, y, radius/2); 
} 
} 

圓圈裏的圓圈

size(200,200); 
int cols = width; 
int rows = height; 

int[][] myArray = new int[cols][rows]; 

for (int i = 0; i < cols; i ++ ) { 
for (int j = 0; j < rows; j ++ ) { 
myArray[i][j] = int(random(255)); 
} 
} 


for (int i = 0; i < cols; i ++ ) { 
for (int j = 0; j < rows; j ++ ) { 
stroke(myArray[i][j]); 
point(i,j); 
} 
} 

黑白花屏

Cell[][] grid; 

int cols = 10; 
int rows = 10; 

void setup() { 
size(200,200); 
grid = new Cell[cols][rows]; 

for (int i = 0; i < cols; i ++ ) { 
for (int j = 0; j < rows; j ++ ) { 
// Initialize each object 
grid[i][j] = new Cell(i*20,j*20,20,20,i + j); 
} 
} 
} 

void draw() { 
background(0); 
for (int i = 0; i < cols; i ++ ) { 
for (int j = 0; j < rows; j ++ ) { 

grid[i][j].oscillate(); 
grid[i][j].display(); 
} 
} 
} 

屏幕分割很多小方塊,從右下角到左上角顏色斜着漸變移動

float r = 8; 

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

void draw() { 
background(255); 

stroke(0); 
fill(175); 
rectMode(CENTER); 
rect(width/2,height/2,r,r); 

r++ ; 

if (r > width) { 
r = 0; 
} 
} 

中間小方塊從無到有放大直到全屏

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 

int mx = constrain(mouseX,0,width); 
int my = constrain(mouseY,0,height); 

translate(mx,my); 
ellipse(0,0,8,8); 

translate(100,0); 
ellipse(0,0,8,8); 

translate(0,100); 
ellipse(0,0,8,8); 

translate(-100,0); 
ellipse(0,0,8,8); 
} 

四個不同位置的小圓圈 跟隨鼠標同事移動

float z = 0; 

void setup() { 

size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 

translate(width/2,height/2,z); 
rectMode(CENTER); 
rect(0,0,8,8); 

z++ ; 

if (z > 200) { 
z = 0; 
} 



} 

3D方塊從無到有放大

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 

translate(100,100,0); 
drawPyramid(150); 
} 

void drawPyramid(int t) { 

stroke(0); 

beginShape(TRIANGLES); 

fill(255,150); 
vertex(-t,-t,-t); 
vertex( t,-t,-t); 
vertex( 0, 0, t); 

fill(150,150); 
vertex( t,-t,-t); 
vertex( t, t,-t); 
vertex( 0, 0, t); 

fill(255,150); 
vertex( t, t,-t); 
vertex(-t, t,-t); 
vertex( 0, 0, t); 

fill(150,150); 
vertex(-t, t,-t); 
vertex(-t,-t,-t); 
vertex( 0, 0, t); 

endShape(); 
} 

3D雙叉

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

void draw() { 
background(255); 
stroke(0); 
fill(175); 

translate(width/2,height/2); 

float theta = PI*mouseX / width; 

rotate(theta); 

rectMode(CENTER); 
rect(0,0,100,100); 
} 

隨着鼠標移動,方塊翻轉

float theta = 0.0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
translate(width/2, height/2); 
rotateZ(theta); 
rectMode(CENTER); 
rect(0,0,100,100); 
theta += 0.02; 
} 

3D方塊翻轉

float theta = 0.0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
translate(width/2, height/2); 
rotateX(theta); 
rectMode(CENTER); 
rect(0,0,100,100); 
theta += 0.02; 
} 

3D方塊正面翻轉,上面是左右翻轉

float theta = 0.0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
translate(width/2, height/2); 
rotateY(theta); 
rectMode(CENTER); 
rect(0,0,100,100); 
theta += 0.02; 
} 

3D方塊翻轉

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
translate(width/2,height/2); 
rotateX(PI*mouseY/height); 
rotateY(PI*mouseX/width); 
rectMode(CENTER); 
rect(0,0,100,100); 
} 

3D方塊,隨着鼠標翻轉,非常棒

float theta = 0.0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
theta += 0.01; 

translate(100,100,0); 
rotateX(theta); 
rotateY(theta); 
drawPyramid(50); 

translate(50,50,20); 

drawPyramid(10); 
} 

void drawPyramid(int t) { 
stroke(0); 

beginShape(TRIANGLES); 

fill(150,0,0,127); 
vertex(-t,-t,-t); 
vertex( t,-t,-t); 
vertex( 0, 0, t); 

fill(0,150,0,127); 
vertex( t,-t,-t); 
vertex( t, t,-t); 
vertex( 0, 0, t); 

fill(0,0,150,127); 
vertex( t, t,-t); 
vertex(-t, t,-t); 
vertex( 0, 0, t); 

fill(150,0,150,127); 
vertex(-t, t,-t); 
vertex(-t,-t,-t); 
vertex( 0, 0, t); 

endShape(); 
} 


3D 多邊形隨機翻轉

float r = 0.0; 

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

void draw() { 
background(255); 

translate(width/2,height/2); 


scale(r); 

stroke(0); 
fill(175); 
rectMode(CENTER); 
rect(0,0,10,10); 


r += 0.02; 


if (r > 20) { 
r = 0; 
} 


} 

2個方塊同時放大

float theta1 = 0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
rectMode(CENTER); 

translate(50,50); 
rotateZ(theta1); 
rect(0,0,60,60); 

theta1 += 0.02; 
} 

3D翻轉

float theta2 = 0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 

background(255); 
stroke(0); 
fill(175); 
rectMode(CENTER); 

translate(150,150); 
rotateY(theta2); 
rect(0,0,60,60); 

theta2 += 0.02; 
} 

3D翻轉

float theta1 = 0; 
float theta2 = 0; 

void setup() { 
size(200,200,P3D); 
} 

void draw() { 
background(255); 
stroke(0); 
fill(175); 
rectMode(CENTER); 

pushMatrix(); 

translate(50,50); 
rotateZ(theta1); 

rect(0,0,60,60); 

popMatrix(); 

pushMatrix(); 

translate(150,150); 
rotateY(theta2); 

rect(0,0,60,60); 
popMatrix(); 

theta1 += 0.02; 
theta2 += 0.02; 
} 

23D翻轉

Rotater[] rotaters; 

void setup() { 
size(200,200); 

rotaters = new Rotater[20]; 

// Rotaters are made randomly 
for (int i = 0; i < rotaters.length; i++ ) { 
rotaters[i] = new Rotater(random(width),random(height),random(-0.1,0.1),random(48)); 
} 
} 

void draw() { 
background(255); 

// All Rotaters spin and are displayed 
for (int i = 0; i < rotaters.length; i++ ) { 
rotaters[i].spin(); 
rotaters[i].display(); 
} 
} 

非常多的方塊翻轉

float theta = 0; 

void setup() { 
size(200,200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 

translate(width/2,height/2); 
fill(255,200,50); 
ellipse(0,0,20,20); 

pushMatrix(); 
rotate(theta); 
translate(50,0); 
fill(50,200,255); 
ellipse(0,0,10,10); 

pushMatrix(); 
rotate(-theta*4); 
translate(15,0); 
fill(50,255,200); 
ellipse(0,0,6,6); 
popMatrix(); 

pushMatrix(); 
rotate(theta*2); 
translate(25,0); 
fill(50,255,200); 
ellipse(0,0,6,6); 
popMatrix(); 

popMatrix(); 

theta += 0.01; 
} 

小型星球軌跡動態


float theta = 0; 

void setup() { 
size(200, 200); 
smooth(); 
} 

void draw() { 
background(255); 
stroke(0); 

translate(width/2,height/2); 

for(float i = 0; i < TWO_PI; i += 0.2) { 


pushMatrix(); 
rotate(theta + i); 
line(0,0,100,0); 

for(float j = 0; j < TWO_PI; j += 0.5) { 

pushMatrix(); 
translate(100,0); 
rotate(-theta-j); 
line(0,0,50,0); 

popMatrix(); 
} 


popMatrix(); 
} 
endShape(); 


theta += 0.01; 
} 

鳥巢一樣的圓形動態圖


Planet[] planets = new Planet[8]; 

void setup() { 
size(200,200); 
smooth(); 

for (int i = 0; i < planets.length; i++ ) { 
planets[i] = new Planet(20 + i*10,i + 8); 
} 
} 

void draw() { 
background(255); 

pushMatrix(); 
translate(width/2,height/2); 
stroke(0); 
fill(255); 
ellipse(0,0,20,20); 

for (int i = 0; i < planets.length; i++ ) { 
planets[i].update(); 
planets[i].display(); 
} 
popMatrix(); 
} 

太陽系動態圖


PImage img; 

void setup() { 
size(320,240); 

img = loadImage("mysummervacation.jpg"); 
} 

void draw() { 
background(0); 

image(img,0,0); 
} 

這裏開始講背景圖片插入 
先插入圖片,方式是點菜單蘭裏的Sketch——然後點擊Add File...... 打開圖片 
然後把mysummervacation.jpg改成你插入的圖片名字


PImage head; 
float x,y; 
float rot; 

void setup() { 
size(200,200); 

head = loadImage("face.jpg"); 
x = 0.0; 
y = width/2.0; 
rot = 0.0; 
} 

void draw() { 
background(255); 

translate(x,y); 
rotate(rot); 

image(head,0,0); 

x += 1.0; 
rot += 0.02; 
if (x > width+head.width) { 
x = -head.width; 
} 
} 

頭像弧線翻轉移動

int maxImages = 10; 
int imageIndex = 0; 


PImage[] images = new PImage[maxImages]; 

void setup() { 
size(200,200); 

for (int i = 0; i < images.length; i ++ ) { 
images[i] = loadImage( "animal" + i + ".jpg" ); 
} 
} 

void draw() { 

image(images[imageIndex],0,0); 
} 

void mousePressed() { 

imageIndex = int(random(images.length)); 
} 

背景圖切換,左鍵按一次換一次


int maxImages = 10; 
int imageIndex = 0; 

PImage[] images = new PImage[maxImages]; 

void setup() { 
size(200,200); 

for (int i = 0; i < images.length; i ++ ) { 
images[i] = loadImage( "animal" + i + ".jpg" ); 
} 
frameRate(5); 
} 

void draw() { 

background(0); 
image(images[imageIndex],0,0); 

imageIndex = (imageIndex + 1) % images.length; 
} 

圖片自動快速切換


size(200,200); 

loadPixels(); 

for (int i = 0; i < pixels.length; i++ ) { 

float rand = random(255); 

color c = color(rand); 

pixels[i] = c; 
} 

updatePixels(); 

黑白花屏


size(200,200); 
loadPixels(); 

for (int x = 0; x < width; x++ ) { 

for (int y = 0; y < height; y++ ) { 


int loc = x + y * width; 

if (x % 2 == 0) { 
pixels[loc] = color(255); 
} else { 
pixels[loc] = color(0); 
} 

} 
} 

updatePixels(); 

豎線花屏


PImage img; 

void setup() { 
size(200,200); 
img = loadImage("sunflower.jpg"); 
} 

void draw() { 
loadPixels(); 

img.loadPixels(); 
for (int y = 0; y < height; y++ ) { 
for (int x = 0; x < width; x++ ) { 
int loc = x + y*width; 

float r = red(img.pixels [loc]); 
float g = green(img.pixels[loc]); 
float b = blue(img.pixels[loc]); 


pixels[loc] = color(r,g,b); 
} 
} 

updatePixels(); 
} 


背景圖片


PImage img; 

void setup() { 
size(200,200); 
img = loadImage("sunflower.jpg"); 
} 

void draw() { 
loadPixels(); 
img.loadPixels(); 
for (int x = 0; x < img.width; x++ ) { 
for (int y = 0; y < img.height; y++ ) { 

int loc = x + y*img.width; 

float r = red (img.pixels[loc]); 
float g = green (img.pixels[loc]); 
float b = blue (img.pixels[loc]); 

float adjustBrightness = ((float) mouseX / width) * 8.0; 
r *= adjustBrightness; 
g *= adjustBrightness; 
b *= adjustBrightness; 

r = constrain(r,0,255); 
g = constrain(g,0,255); 
b = constrain(b,0,255); 

color c = color(r,g,b); 
pixels[loc] = c; 
} 
} 

updatePixels(); 
} 

隨着鼠標移動,畫面亮度變化

PImage img; 

void setup() { 
size(200,200); 
img = loadImage( "sunflower.jpg" ); 
} 

void draw() { 
loadPixels(); 

img.loadPixels(); 
for (int x = 0; x < img.width; x++ ) { 
for (int y = 0; y < img.height; y++ ) { 

int loc = x + y*img.width; 

float r = red (img.pixels[loc]); 
float g = green (img.pixels[loc]); 
float b = blue (img.pixels[loc]); 

float distance = dist(x,y,mouseX,mouseY); 

float adjustBrightness = (50-distance)/50; 
r *= adjustBrightness; 
g *= adjustBrightness; 
b *= adjustBrightness; 

// Constrain RGB to between 0-255 
r = constrain(r,0,255); 
g = constrain(g,0,255); 
b = constrain(b,0,255); 

color c = color(r,g,b); 
pixels[loc] = c; 
} 
} 

updatePixels(); 
} 

透明圓圈跟隨鼠標,鼠標經過的地方亮,其他地區全黑


PImage source; 
PImage destination; 
void setup() { 
size(200,200); 
source = loadImage("sunflower.jpg"); 
destination = createImage(source.width, source.height, RGB); 
} 

void draw() { 
float threshold = 127; 

// We are going to look at both image's pixels 
source.loadPixels(); 
destination.loadPixels(); 

for (int x = 0; x < source.width; x++ ) { 
for (int y = 0; y < source.height; y++ ) { 
int loc = x + y*source.width; 

if (brightness(source.pixels[loc]) > threshold){ 
destination.pixels[loc] = color(255); 
} else { 
destination.pixels[loc] = color(0); 
} 
} 
} 


destination.updatePixels(); 

image(destination,0,0); 
} 

圖片黑白畫


PImage img; 

void setup() { 
size(200,200); 
img = loadImage("sunflower.jpg"); 
} 

void draw() { 

image(img,0,0); 

filter(THRESHOLD,0.5); 
} 

圖片黑白畫 精簡版代碼


PImage img; 
PImage destination; 

void setup() { 
size(200,200); 
img = loadImage("sunflower.jpg"); 
destination = createImage(img.width, img.height, RGB); 
} 

void draw() { 

img.loadPixels(); 
destination.loadPixels(); 

for (int x = 1; x < width; x++ ) { 
for (int y = 0; y < height; y++ ) { 

int loc = x + y*img.width; 
color pix = img.pixels[loc]; 

int leftLoc = (x - 1) + y*img.width; 
color leftPix = img.pixels[leftLoc]; 

float diff = abs(brightness(pix) - brightness(leftPix)); 
destination.pixels[loc] = color(diff); 
} 
} 

destination.updatePixels(); 

image(destination,0,0); 
} 

圖片黑炭化

PImage img; 
int w = 80; 

float[][] matrix = { { -1, -1, -1 } , 
{ -1, 9, -1 } , 
{ -1, -1, -1 } } ; 

void setup() { 
size(200,200); 
img = loadImage( "sunflower.jpg" ); 
} 

void draw() { 


image(img,0,0); 

int xstart = constrain(mouseX-w/2,0,img.width); 
int ystart = constrain(mouseY-w/2,0,img.height); 
int xend = constrain(mouseX + w/2,0,img.width); 
int yend = constrain(mouseY + w/2,0,img.height); 
int matrixsize = 3; 

loadPixels(); 

for (int x = xstart; x < xend; x++ ) { 
for (int y = ystart; y < yend; y++ ) { 

color c = convolution(x,y,matrix,matrixsize,img); 
int loc = x + y*img.width; 
pixels[loc] = c; 
} 
} 
updatePixels(); 
stroke(0); 
noFill(); 
rect(xstart,ystart,w,w); 
} 

color convolution(int x, int y, float[][] matrix, int matrixsize, PImage img) { 
float rtotal = 0.0; 
float gtotal = 0.0; 
float btotal = 0.0; 
int offset = matrixsize / 2; 

for (int i = 0; i < matrixsize; i++ ) { 
for (int j = 0; j < matrixsize; j++ ) { 

int xloc = x + i-offset; 
int yloc = y + j-offset; 
int loc = xloc + img.width*yloc; 

loc = constrain(loc,0,img.pixels.length-1); 

rtotal += (red(img.pixels[loc]) * matrix[i][j]); 
gtotal += (green(img.pixels[loc]) * matrix[i][j]); 
btotal += (blue(img.pixels[loc]) * matrix[i][j]); 
} 
} 

rtotal = constrain(rtotal,0,255); 
gtotal = constrain(gtotal,0,255); 
btotal = constrain(btotal,0,255); 

return color(rtotal,gtotal,btotal); 
} 

圖片方塊跟隨鼠標,鼠標移動到的地方像素畫,其他地方不變


PImage img; 
int pointillize = 16; 

void setup() { 
size(200,200); 
img = loadImage("sunflower.jpg"); 
background(255); 
smooth(); 
} 

void draw() { 

int x = int(random(img.width)); 
int y = int(random(img.height)); 
int loc = x + y*img.width; 

loadPixels(); 
float r = red(img.pixels[loc]); 
float g = green(img.pixels[loc]); 
float b = blue(img.pixels[loc]); 
noStroke(); 

fill(r,g,b,100); 

ellipse(x,y,pointillize,pointillize); 
} 

圓形小球不斷飄落,最終形成圖片模樣

PImage img; 
int cellsize = 2; 
int cols, rows; 

void setup() { 
size(200,200,P3D); 
img = loadImage( "sunflower.jpg" ); 
cols = width/cellsize; 
rows = height/cellsize; 

void draw() { 
background(255); 
img.loadPixels(); 


for (int i = 0; i < cols; i++ ) { 

for (int j = 0; j < rows; j++ ) { 
int x = i*cellsize + cellsize/2; 
int y = j*cellsize + cellsize/2; 
int loc = x + y*width; 
color c = img.pixels[loc]; 

float z = (mouseX/(float)width) * brightness(img.pixels[loc])- 100.0; 

pushMatrix(); 
translate(x,y,z); 
fill(c); 
noStroke(); 
rectMode(CENTER); 
rect(0,0,cellsize,cellsize); 
popMatrix(); 

} 
} 
} 

圖片以3D模式破碎


PFont f; 

void setup() { 
size(200,200); 


f = loadFont( "ArialMT-16.vlw" ); 
} 

void draw() { 
background(255); 
textFont(f,16); 
fill(0); 

text ( "Mmmmm ... Strings ..." ,10,100); 

} 

文字


PFont f; 

void setup() { 
size(400,200); 
f = createFont("Arial", 16, true); 
} 

void draw() { 
background(255); 
stroke(175); 
line(width/2,0,width/2,height); 
textFont(f); 
fill(0); 


textAlign(CENTER); 
text("This text is centered." ,width/2,60); 
textAlign (LEFT) ; 
text("This text is left aligned." ,width/2,100); 
textAlign(RIGHT); 
text("This text is right aligned." ,width/2,140); 
} 

以中線爲住 一個在左 一個在中 一個在右 3個句子


String[] headlines = { 
"Processing downloads break downloading record." , 
"New study shows computer programming lowers cholesterol." , 
}; 

PFont f; 
float x; 
int index = 0; 

void setup() { 
size(400,200); 
f = createFont( "Arial" ,16,true); 


x = width; 
} 

void draw() { 
background(255); 
fill(0); 

textFont(f,16); 
textAlign (LEFT); 

text(headlines[index],x,180); 

x = x - 3; 

float w = textWidth(headlines[index]); 
if (x < -w) { 
x = width; 

index = (index + 1) % headlines.length; 
} 
} 

文字從下面劃過,2句話,一句結束後另一句出現


PFont f; 
String message = "this text is spinning"; 
float theta; 

void setup() { 
size(200,200); 
f = createFont("Arial", 20, true); 
} 

void draw() { 

background(255); 
fill(0); 
textFont(f); 
translate(width/2,height/2); 
rotate(theta); 
textAlign(CENTER) ; 

text(message,0,0); 

theta += 0.05; 
} 

字體360度不停旋轉

learning processing書中的所有代碼

感謝一直關注着禾灮成長進步的朋友們。你們的信任、支持和鼓勵,鞭策着我們一路走到了今天。

感謝所有的合作伙伴,我們相互促進,共同見證了彼此的成長。

感謝所有曾經在禾灮彼此倚靠、相互鼓勵、攜手同心、砥礪同行的兄弟姐妹。這裏承載了我們的青春與熱血。

            禾灮,感謝有你。

未來,我們將一如既往,砥礪前行。

                                    禾灮·小楊
                                   2018.08.23
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章