ball=new BouncBall(width/2,height/2,20,2,2.5); bang=new bangbang(120); bks=new brick[24];/// int k=0;
for(int i=0;i<6;i++) {
for(int j=0;j<4;j++) {
bks[k]=new brick(i*80,j*20,80,20); k++; } } }
void draw() {
for(int i=0;i bks[i].display(); ball.crash(bks[i],ball); } ball.move(bang); fill(255,10); noStroke(); rect(0,0,width,height-20); noStroke(); fill(255,255); rect(0,height-20,width,20); fill(255,0,0); //stroke(255); ball.display(); bang.display(); } 3.学习使用noise()函数,修改自《代码本色》中的一个例子:一个随机游走的小球,颜色随时间变化 class Walker{ float x,y; float tx,ty; float r,b,g; float tr,tb,tg; Walker() { tx=0; ty=10000; tr=0; tb=1000; tg=2000; } void step() { x=map(noise(tx),0,1,0,width); y=map(noise(ty),0,1,0,height); tx+=0.01; ty+=0.01; } void c() //color { r=map(noise(tr),0,1,0,255); b=map(noise(tb),0,1,0,255); g=map(noise(tg),0,1,0,255); tr+=0.01; tb+=0.01; tg+=0.01; } } Walker w; void setup() { size(480,480); w=new Walker(); } void draw() { w.step(); w.c(); fill(w.r,w.b,w.g); ellipse(w.x,w.y,20,20); } 4.学习《代码本色》后编的两个相互吸引运动的小球,在边界会转到另一边去: class Mover { PVector location1,location2; PVector v1,v2; PVector acceleration1,acceleration2; Mover() { location1=new PVector(random(width),random(height)); v1=new PVector(random(-2,2),random(-2,2)); acceleration1=PVector.random2D(); location2=new PVector(random(width),random(height)); v2=new PVector(random(-2,2),random(-2,2)); acceleration2=PVector.random2D(); } void update() { // PVector mouse=new PVector(mouseX,mouseY); PVector dir1=PVector.sub(location2,location1); PVector dir2=PVector.sub(location1,location2); dir1.normalize(); dir1.mult(0.5); acceleration1=dir1; v1.add(acceleration1); v1.limit(10); location1.add(v1); //acceleration2=PVector.mult(dir1,-0.1); dir2.normalize(); dir2.mult(0.5); acceleration2=dir2; v2.add(acceleration2); v2.limit(10); location2.add(v2); } void display() { stroke(0); fill(175); ellipse(location1.x,location1.y,16,16); ellipse(location2.x,location2.y,16,16); } void checkEdges() { if(location1.x>width) { location1.x=0; } else if(location1.x<0) { location1.x=width; } if(location1.y>height) { location1.y=0; } else if(location1.y<0) { location1.y=height; } } } Mover mover;//s=new Mover[5]; void setup() { mover=new Mover(); size(480,480); smooth(); //mover.acceleration.x=0.05; //mover.acceleration.y=0.05; } void draw() { mover.update(); mover.checkEdges(); mover.display(); } 5.学习数组,把上面两个小球的改为任意个数小球: int geshu=20; //修改个数,可实现任意多小球相互运动,边界条件随机。 class Mover { PVector[] location=new PVector[geshu]; PVector[] v=new PVector[geshu]; PVector[] acceleration=new PVector[geshu]; Mover() { for(int i=0;i location[i]=new PVector(random(width),random(height)); v[i]=new PVector(random(-2,2),random(-2,2)); acceleration[i]=PVector.random2D(); } } void update() { for(int i=0;i PVector dir=new PVector(0,0); for(int j=0;j if(i==j) { } else { PVector tempdir=PVector.sub(location[j],location[i]); dir.add(tempdir); } } dir.normalize(); dir.mult(0.5); acceleration[i]=dir; v[i].add(acceleration[i]); v[i].limit(10); } for(int i=0;i location[i].add(v[i]); } } void display() {