码迷,mamicode.com
首页 > 其他好文 > 详细

processing做遨游星空效果

时间:2020-07-18 22:00:58      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:pre   code   stroke   dom   效果图   etc   img   proc   length   

sketch_200718a文件

Star[] stars = new Star[600];
float speed;

void setup(){
  size(400, 400);
  for(int i = 0; i < stars.length; i++){
    stars[i] = new Star();
  }
}

void draw(){
  speed = map(mouseX, 0, width, 0, 20);
  background(0);
  translate(width / 2, height / 2);
  for(int i = 0; i < stars.length; i++){
    stars[i].update();
    stars[i].show();
  }
}

Star文件

class Star{
  float x;
  float y;
  float z;
  float pz;
  
  Star(){
    x = random(-width, width);
    y = random(-height, height);
    z = random(width);
    pz = z;
  }
  
  void update(){
    z = z - speed;
    if(z < 1){
      x = random(-width, width);
      y = random(-height, height);
      z = width;
      pz = z;
    }
  }
  
  void show(){
    fill(255);
    noStroke();
    float sx = map(x / z, 0, 1, 0, width);
    float sy = map(y / z, 0, 1, 0, height);
    float r = map(z, 0, width, 16, 0);
    float px = map(x / pz, 0, 1, 0, width);
    float py = map(y / pz, 0, 1, 0, height);
    pz = z;
    ellipse(sx, sy, r, r);
    stroke(255);
    line(px, py, sx, sy);
  }
}

效果图:
技术图片

processing做遨游星空效果

标签:pre   code   stroke   dom   效果图   etc   img   proc   length   

原文地址:https://www.cnblogs.com/tellw/p/13336692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!