Processing 创建运动的物体

时间:2024-10-12 16:56:25

1、编程实现物体从左到右的平滑移动输入代码:int radius = 40;float x = -radius;float speed = 0.5;void setup() {size(240, 120);smooth();ellipseMode(RADIUS);}void draw() {background(0);x += speed; // Increase the value of xarc(x, 60, radius, radius, 0.52, 5.76);}

Processing 创建运动的物体
Processing 创建运动的物体

2、物体的循环运动输入代艨位雅剖码:int radius = 40;float x = -radius;float speed = 0.5;void setup() {size(240, 120);smooth();ellipseMode(RADIUS);}void draw() {background(0);x += speed;if (x > width+radius) {x = -radius;}arc(x, 60, radius, radius, 0.52, 5.76);}注意这里使用了 if 判断语句,就是判断是否超出了设置的窗口,如果超出窗口,就重新设置物体的坐标,再次从起点开始运动;窗口坐标示意如下图所示:

Processing 创建运动的物体

3、碰到墙壁就翻转,并朝相反的方向移动输入代码:int radius 屏顿幂垂= 40;float x = 110;float spee颊俄岿髭d = 0.5;int direction = 1;void setup() {size(240, 120);smooth();ellipseMode(RADIUS);}void draw() {background(0);x += speed * direction;if ((x > width-radius) || (x < radius)) {direction = -direction; // Flip direction}if (direction == 1) {arc(x, 60, radius, radius, 0.52, 5.76); // Face right} else {arc(x, 60, radius, radius, 3.67, 8.9); // Face left}}

Processing 创建运动的物体

4、从屏幕上一个像素点移动到另一个闻赙酵枭像素点输入代码:int startX = 20; // Initial x-coordinateint stopX = 160; // Final x-coord足毂忍珩inateint startY = 30; // Initial y-coordinateint stopY = 80; // Final y-coordinatefloat x = startX; // Current x-coordinatefloat y = startY; // Current y-coordinatefloat step = 0.005; // Size of each step (0.0 to 1.0)float pct = 0.0; // Percentage traveled (0.0 to 1.0)void setup() {size(240, 120);smooth();}void draw() {background(0);if (pct < 1.0) {x = startX + ((stopX-startX) * pct);y = startY + ((stopY-startX) * pct);pct += step;}ellipse(x, y, 20, 20);}

Processing 创建运动的物体

5、产生随机图形输入代码:void setup() {size(240, 120);smooth();}void draw() {background(204);for (int x = 20; x < width; x += 20) {float mx = mouseX / 10;float offsetA = random(-mx, mx);float offsetB = random(-mx, mx);line(x + offsetA, 20, x - offsetB, 100);}}

Processing 创建运动的物体
Processing 创建运动的物体

6、让物体随机移动到任意位置输入代码:float speed = 2.5;int diameter = 20;float x;float y;void setup() {size(240, 120);smooth();x = width/2;y = height/2;}void draw() {x += random(-speed, speed);y += random(-speed, speed);ellipse(x, y, diameter, diameter);}

Processing 创建运动的物体

7、控制物体的运动时间输入代码:int time1 = 2000;int time2 = 4000;float x = 0;void setup() {size(480, 120);smooth();}void draw() {int currentTime = millis();background(204);if (currentTime > time2) {x -= 0.5;} else if (currentTime > time1) {x += 2;}ellipse(x, 60, 90, 90);}

Processing 创建运动的物体
© 手抄报圈