Processing系列 雨

2015-08-087338
AI 快速预览详细 收起
本文介绍了如何使用Processing自带的粒子系统创建雨效果。通过调整粒子总数,可以在低配置电脑上实现流畅的视觉效果。代码示例如下:首先设置画布大小,然后创建一个粒子列表,在每一帧中更新粒子的位置并绘制它们。如果粒子数量超过200个,则添加新的粒子。每个粒子都有自己的位置、速度和其他属性,通过不断更新这些属性来模拟雨滴的运动。

代码如下:
使用了Processing自带的粒子系统,系统配置较低的电脑如果卡就调低一下粒子总数

  1. ArrayList<Particle> particles;
  2. void setup() {
  3.   size(300, 300);
  4.   ellipseMode(RADIUS);
  5.   noFill();
  6.   particles = new ArrayList<Particle>();
  7. }
  8. void draw() {
  9.   background(0);
  10.   if (particles.size() < 200) particles.add(new Particle());
  11.   for (Particle p : particles) {
  12.     p.update();
  13.     p.display();
  14.   }
  15. }
  16. class Particle {
  17.   float x, y, vy, ty, r, tr, w;
  18.   Particle() {
  19.     reset();
  20.   }
  21.   void reset() {
  22.     x = random(width);
  23.     y = random(-150, 0);
  24.     vy = random(2, 4);
  25.     ty = y + height;
  26.     r = 0;
  27.     tr = 50;
  28.     w = random(.1, 2);
  29.   }
  30.   void update() {
  31.     if (y < ty) {
  32.       y += vy;
  33.     } else {
  34.       r++;
  35.     }
  36.     if (r > tr) reset();
  37.   }
  38.   void display() {
  39.     strokeWeight(w);
  40.     if (y < ty) {
  41.       stroke(255);
  42.       point(x, y);
  43.     } else {
  44.       stroke(255, map(r, 0, tr, 255, 0));
  45.       ellipse(x, y, r, r*.5);
  46.     }
  47.   }
  48. }
复制代码
授权转载自 任远媒体实验室

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -