图像与运动对象的结合。
input:
PImage[] dongmei = new PImage[3]; //类图像对象
Bubble[] bubbles = new Bubble[5]; //类创建对象
void setup(){
size(600,400);
for (int i = 0;i < dongmei.length;i++){
dongmei[i] = loadImage("dongmei"+i+".png");
}
for (int i = 0;i < bubbles.length;i++){
int index = int(random(0,dongmei.length)); //设定随机遍历数
bubbles[i] = new Bubble(dongmei[index],random(width),-50,random(40,60));
}
}
void draw(){
background(255);
for (int i = 0;i < bubbles.length;i++){
bubbles[i].ascend();
bubbles[i].display();
bubbles[i].top();
}
}
input附加类Bubble:
class Bubble{
float x;
float y;
float diameter; //定义直径
float yspeed;
PImage img;
Bubble(PImage tempImg,float tempX,float tempY,float tempD){ //对象的特征函数,含部分变量
x = tempX;
y = tempY;
diameter = tempD;
img = tempImg; //获取图像
yspeed = random(0.3,1.0); //气泡下降速度
}
void ascend(){ //对象的功能函数之一
y = y + yspeed;
x = x + random(-0.3,0.3);
}
void display(){ //新对象放置
//stroke(0,0,205,90);
//fill(255,215,0,75);
//ellipse(x,y,diameter,diameter);
imageMode(CENTER);
image(img,x,y,diameter,diameter); //系数1.6保持图像比例
}
void top(){
if (y > height + 50){
y = -50;
}
}
}
output:
参考视频链接:【processing落花】 https://www.bilibili.com/video/BV1rM4y1i7oP/?share_source=copy_web&vd_source=81f6714046d21d930e6f52a77c0d8811