Processing 创建运动的物体
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);}

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 判断语句,就是判断是否超出了设置的窗口,如果超出窗口,就重新设置物体的坐标,再次从起点开始运动;窗口坐标示意如下图所示:

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


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