互动媒体 代码本色

互动媒体 代码本色

代码1 随机游走
float t = 0.007;

PVector p0=new PVector(0,400);

void setup(){
size(800,800);
color(255);
smooth();
}
int value =1;

PVector p = new PVector(400,400);
void draw(){

float sk = random(100);
if (sk>75){
p = new PVector(p.x+skt,p.y+(100-sk)t);
value=0;}
else {
if (sk>50){
p = new PVector(p.x+sk
t,p.y-(100-sk)t);
//p = new PVector(p.x+s
t,p.y);
value=1;}
else {
if (sk>25){
p = new PVector(p.x-sk
t,p.y+(100-sk)*t);
//p = new PVector(p.x,p.y+(1-s)t);
}
else {
p = new PVector(p.x-sk
t,p.y-(100-sk)*t);
//p = new PVector(p.x,p.y);

 }

}
}
if (p.x>800) p.x-=800;
else {
if (p.x<0) p.x+=800;
}
if (p.y>800) p.y-=800;
else {
if (p.y<0) p.y+=800;
}

ellipse(p.x,p.y,20,20);
fill(#7FFFD4);

}
在这里插入图片描述
代码2 向量

Mover mover;

void setup() {
  size(300,300);
  mover = new Mover();
}

void draw() {
  background(255);
  
  mover.update();
  mover.checkEdges();
  mover.display();
}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x  n;
    y = y 
 n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y);
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);
    }
  }
  
   void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
   }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;

  
  Mover() {
    location = new PVector(width/2,0);
    velocity = new PVector(random(-2,2),random(-2,2));
    acceleration = new PVector(-0.001,0.1);
    topspeed = 10;
  }
  
  void update() {
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }
  
  void display() {
    stroke(100,200,200);
    fill(100,200,200);
    ellipse(location.x,location.y,20,20);
  }
  
  void checkEdges() {
    if((location.x > mouseX) || (location.x < 0)) {
    velocity.x = velocity.x  -1; 
  }
  if((location.y > mouseY) || (location.y < 0)) {
    velocity.y = velocity.y 
 -1; 
  }
  }
}


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码3 力

Mover[] movers = new Mover[20];
Mover[] mover2s = new Mover[10];

void setup() {
  size(640,360);
  for (int i = 0; i < movers.length; i++) {
    movers[i] = new Mover(random(0.8,6),0,0);
    //mass=random(0.1,8)质量随机
  }
   for (int i = 0; i < mover2s.length; i++) {
   mover2s[i] = new Mover(random(0.8,4),0,0);
   }
}

void draw() {
  background(255);
  float t = 1;
  
  for (int i = 0; i < movers.length; i++) {
    //PVector wind = new PVector(noise(t)*0.1,0);
    if (mousePressed) {
       PVector wind = new PVector(0.01,noise(t));
       movers[i].applyForce(wind);
         t+=0.1;
    }
    PVector gravity = new PVector(0,0.1);
    movers[i].applyForce(gravity);
    
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }
  
  for (int i = 0; i < mover2s.length; i++) {
    if (mousePressed) {
       PVector wind = new PVector(0.01,noise(t));
       mover2s[i].applyForce(wind);
         t+=0.1;
    }
    PVector gravity = new PVector(0,0.1);
    
    mover2s[i].applyForce(gravity);
    mover2s[i].update();
    mover2s[i].display2();
    mover2s[i].checkEdges2();
    stroke(122,103,238,230);
    strokeWeight(2);
    line(width/2,0,width/2,height/2);
    line(0,height/2,width/2,height/2);
  }
}

//引用的Mover类
class Mover {

  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;

  Mover(float m, float x , float y) {
    mass = m;
    position = new PVector(x,y);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
  }
  
  void applyForce(PVector force) {
    PVector f = PVector.div(force,mass);
    acceleration.add(f);
  }
  
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    stroke(255);
    strokeWeight(1);
    float b = randomGaussian();
    b = constrain(b,100,255);
    fill(30,b,255,b);
    rectMode(CENTER);
    ellipse(position.x,position.y,mass16,mass16);
  }
  void display2() {
    stroke(255);
    strokeWeight(1);
    float g = randomGaussian();
    g = constrain(g,60,180);
    float a = randomGaussian();
    a = constrain(a,200,255);
    fill(255,g,180,a);
    ellipse(position.x,position.y,mass16,mass16);
  }

  void checkEdges() {
    if (position.x > width) {
      position.x = width;
      velocity.x = -1;
    } else if (position.x < 0) {
      velocity.x 
= -1;
      position.x = 0;
    }
    if (position.y > height) {
      velocity.y = -1;
      position.y = height;
    }
  }
  
  void checkEdges2() {
    if (position.x > width/2) {
      position.x = width/2;
      velocity.x 
= -1;
    } else if (position.x < 0) {
      velocity.x = -1;
      position.x = 0;
    }
    if (position.y > height/2) {
      velocity.y 
= -1;
      position.y = height/2;
    }
  }

}


在这里插入图片描述
代码4 振荡

PShape spring;
int mass = 80;
float nForce;
PVector position;
PVector velocity;
PVector acceleration;
float g = 1;
float k = 10;
int state = 1;
float delta = 0;

void setup(){
  size(900,600);
  background(255);
  position = new PVector(width/2, 0);
  velocity = new PVector(0, 0);
  acceleration = new PVector(0, 0);
}

void draw(){
  background(255);
  fill(0);
  ellipse(position.x, position.y, mass, mass);
  int springLength = floor(100 - delta);
  drawSpring(new PVector(width/2, 550), springLength);
  applyForce();
  
  //if dropped on spring
  if (state == 1 && ((position.y+mass) >= height/2 + 150)){//collision happend
    state = 2;
  }
  if (state == 2 && ((position.y+mass) <= height/2 + 150)){
    state = 3;
  }
  if (state == 3 && position.y <= 50){
    state = 1;
  }
}

void applyForce(){
  if (state == 1){
    acceleration = new PVector(0, g);//g = a.
    velocity = PVector.add(velocity, acceleration);
    position = PVector.add(position, velocity);
    acceleration.mult(0);
  }
  if (state == 2){
    delta = position.y+mass-height/2-150;
    nForce = k  delta;
    acceleration = new PVector(0, (mass
g - nForce)/mass);
    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0);
  }
  if (state == 3){
    acceleration = new PVector(0, g);//g = a.
    velocity = PVector.add(velocity, acceleration);
    position = PVector.add(position, velocity);
    acceleration.mult(0);
  }
}

void drawSpring(PVector position, int springLength){
  stroke(0);
  strokeWeight(2);
  noFill();
  
  pushMatrix();
  translate(position.x, position.y);
  beginShape();
  vertex(-25,0);
  vertex(25,0);
  vertex(-25,-1springLength/10);
  vertex(25,-2
springLength/10);
  vertex(-25,-3springLength/10);
  vertex(25,-4
springLength/10);
  vertex(-25,-5springLength/10);
  vertex(25,-6
springLength/10);
  vertex(-25,-7springLength/10);
  vertex(25,-8
springLength/10);
  vertex(-25,-9*springLength/10);
  vertex(25,-springLength);
  vertex(-25,-springLength);
  endShape();
  popMatrix();
}


在这里插入图片描述
代码5 粒子系统

ParticleSystem ps;
 
void setup() {
  size(640, 360);
  ps = new ParticleSystem(new PVector(width/2, 50));
}
void draw() {
 
  background(0);
  ps.addParticle();
  ps.run();
}

 

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
 
  float lifespan;
  Particle(PVector l) {
    // The acceleration
    acceleration = new PVector(0, 0.05);
    // circel’s x and y ==> range
    velocity = new PVector(random(-1, 1), random(-2, 0));
    // apawn’s position
    location = l.copy();
    // the circle life time
    lifespan = 255.0;
  }
  void run() {
    update();
    display();
  }
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan-=1.0;
  }
 
  boolean isDead() {
    if (lifespan <= 0) {
      return true;
    } else {
      return false;
    }
  }
  void display() {
    // border
    stroke(0, lifespan);
    // border’s weight
    strokeWeight(1);
    float r = random(0,255);
    float g = random(0,255);
    float b = random(0,255);
    // random the circle’s color
    fill(r,g,b, lifespan);
    // draw circle
    ellipse(location.x, location.y, 3, 3);
  }
}

// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 
 
class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
 
  ParticleSystem(PVector position) {
    origin = position.copy();
    particles = new ArrayList<Particle>();
  }
 
  void addParticle() {
    particles.add(new Particle(origin));
  }
 
  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}


在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值