processing创意编程实例代码
时间: 2023-11-12 18:10:45 浏览: 53
以下是一个用Processing实现的创意编程实例代码:
```
int num = 100;
float[] x = new float[num];
float[] y = new float[num];
float[] speedX = new float[num];
float[] speedY = new float[num];
float[] radius = new float[num];
color[] colors = new color[num];
void setup() {
size(600, 600);
background(0);
smooth();
for (int i = 0; i < num; i++) {
x[i] = random(width);
y[i] = random(height);
speedX[i] = random(-2, 2);
speedY[i] = random(-2, 2);
radius[i] = random(5, 20);
colors[i] = color(random(255), random(255), random(255), 100);
}
}
void draw() {
background(0, 20);
for (int i = 0; i < num; i++) {
x[i] += speedX[i];
y[i] += speedY[i];
if (x[i] < 0 || x[i] > width) {
speedX[i] *= -1;
}
if (y[i] < 0 || y[i] > height) {
speedY[i] *= -1;
}
noStroke();
fill(colors[i]);
ellipse(x[i], y[i], radius[i], radius[i]);
}
}
void mousePressed() {
for (int i = 0; i < num; i++) {
colors[i] = color(random(255), random(255), random(255), 100);
}
}
```
这个例子实现了一个简单的动态圆形效果,每个圆形都有不同的颜色和大小,并且随机移动。当鼠标点击屏幕时,所有圆形的颜色都会随机变化。