In recitation 6, we developed our own animation. Following is my work and the codes:
int x=0; int y=300; int angle=0; int state=0; void setup() { size(600, 600); background(0); } void draw() { background(0); rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; rectMode(CORNER); fill(20, 120, 150); rect(100, 100, 120, 120); rect(100, 400, 120, 120); rect(400, 100, 120, 120); rect(400, 400, 120, 120); if (x>width) { x=0; rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; fill(20, 120, 150); rect(100, 100, 120, 120); rect(100, 400, 120, 120); rect(400, 100, 120, 120); rect(400, 400, 120, 120); } if (keyPressed==true) { if (key==CODED) { if (keyCode==UP) { noStroke(); rect(100, 100, 120, 120); rect(100, 400, 120, 120); rect(400, 100, 120, 120); rect(400, 400, 120, 120); y=y-10; fill(245, 228, 146); circle(x, y, 20); x+=3; } if (keyCode==DOWN) { noStroke(); y=y+10; fill(245, 228, 146); circle(x, y, 20); x+=3; } if (keyCode==LEFT) { noStroke(); x=x-10; fill(245, 228, 146); circle(x, y, 20); x+=3; } if (keyCode==RIGHT) { noStroke(); x=x+150; fill(245, 228, 146); circle(x, y, 20); x+=3; } } } if (dist(x, y, 160, 160)<=60) { state=1; } if (state==1) { background(0); rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; fill(20, 120, 150); rect(100, 400, 120, 120); rect(400, 100, 120, 120); rect(400, 400, 120, 120); pushMatrix(); translate(160, 160); rotate(radians(++angle)); fill(20, 120, 150); noStroke(); rectMode(CENTER); rect(0, 0, 120, 120); popMatrix(); } if (dist(x, y, 160, 460)<=60) { state=2; } if (state==2) { background(0); rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; fill(20, 120, 150); rectMode(CORNER); rect(100, 100, 120, 120); rect(400, 100, 120, 120); rect(400, 400, 120, 120); pushMatrix(); translate(160, 460); rotate(radians(++angle)); fill(20, 120, 150); noStroke(); rectMode(CENTER); rect(0, 0, 120, 120); popMatrix(); } if (dist(x, y, 460, 160)<=60) { state=3; } if (state==3) { background(0); rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; fill(20, 120, 150); rectMode(CORNER); rect(100, 400, 120, 120); rect(100, 100, 120, 120); rect(400, 400, 120, 120); pushMatrix(); translate(460, 160); rotate(radians(++angle)); fill(20, 120, 150); noStroke(); rectMode(CENTER); rect(0, 0, 120, 120); popMatrix(); } if (dist(x, y, 460, 460)<=60) { state=4; } if (state==4) { background(0); rectMode(CORNER); fill(0); stroke(255); rect(160, 160, 300, 300); noStroke(); fill(245, 228, 146); circle(x, y, 20); x+=3; fill(20, 120, 150); rectMode(CORNER); rect(100, 400, 120, 120); rect(400, 100, 120, 120); rect(100, 100, 120, 120); pushMatrix(); translate(460, 460); rotate(radians(++angle)); fill(20, 120, 150); noStroke(); rectMode(CENTER); rect(0, 0, 120, 120); popMatrix(); } }
Because the picture I drew in last recitation is too complicated, so I started from beginning. My initial idea was to create a game that likes Super Mario, in which the player can use keyboard to control a rolling ball, and there will be wood blocks randomly appear on the path. Once the ball hits the block, the ball and block will shine together. But through practice, I found that it was almost impossible to realize merely by using what we’ve learned so far, so I changed it to this animation at last.
Basically, it’s a ball rolling across the screen repetitively, and it can also be controlled by arrows on keyboard. The interaction is that once the ball hits a square, the square will rotate until the ball hits the next square. It’s also simple functions but complicated in coding. But thanks to this complexity that acquaint me with a lot of interesting functions. They are listed below.
pushMatrix(); popMatrix();
A set of functions help to keep the square rotating in a fixed position. They must be used together.
translate(160, 460);
his function can translate the figures, it can be seen as translating the origin to (160,460), so that sometimes it can simplify the codes. But one thing needs to be noticed is that the following coordinates must be in accordance with the translated position.
rotate(radians(++angle));
Rotating function. The figure you want to rotate will rotate around a point outside the figure. So if you want to make rotate in a certain position, use Mode function and pushMatrix/popMatrix.
rotate(radians(++angle));
A function that can map degrees to radians. It will be useful in rotating.
As for homework, the final works and codes are listed as follows:
int r1=100; int r2=160; int state=0; int c=0; int k=0; int x=300; int y=300; void setup(){ size(600,600); } void draw(){ background(255); if(keyPressed==true){ if(keyCode==UP){ y-=5;} if(keyCode==DOWN){ y+=5;} if(keyCode==LEFT){ x-=5;} if(keyCode==RIGHT){ x+=5;} } if(r1>=250||r2>=500){ state=1;} if(r1<=50||r2<=100){ state=0;} if(state==0){ r1+=2.9; r2+=2.9;} if(state==1){ r1-=2; r2-=2;} colorMode(HSB,360,100,100); if(c<=0){ c=0; k=0;} if(c>=360){ c=360; k=1;} if(k==0){ c++;} if(k==1){ c--;} noStroke(); fill(c,100,100); circle(x,y,r2); colorMode(RGB,255,255,255); fill(255); circle(x,y,r1); }