代码在序列帧的运动

    1. void setup(){
    2. }
    3. void draw(){
    4. }

    先运行setup里的内容 一次,然后再以60帧/秒的频率运行。

    例子:让一个小球匀速运动

    float x=0;
    
     void setup(){
       size(600,800);
    
     }
    
     void draw(){
       background(0);
    
       x+=1;
       noStroke();
       fill(255,255,0);
       ellipse(x,300,60,60); //x每一帧 走一步
     }
    

    image.pngimage.pngimage.png
    扔掉background,使用半透明方块实现小球拖动效果

     float x=0;
    
     void setup(){
       size(800,600);
    
     }
    
     void draw(){
       //background(0);
       fill(0,20);
       rect(0,0,width,height);
    
       x+=1;
       noStroke();
       fill(255,255,0);
       ellipse(x,300,60,60);
     }
    

    image.png
    让小球的重力加速度效果

     float y=0;  //小球纵坐标
     float gravity=0.2; //小球的纵向速度
     float yspd=0; //重力加速度
    
     void setup(){
       size(600,800);
     }
    
     void draw(){
       fill(0,15);
       rect(0,0,width,height);//和窗口等大的半透明方块营造尾迹
    
       yspd+=gravity; //刷新速度值,加上重力加速度
       y+=yspd; //坐标随速度变化
    
       if(y+15>height){ //如果小球落地,15是算上半径
         y-=yspd; //把位置挪到触地前一刹那的位置
         yspd*=-0.8;//把速度反向,并损耗20%的速度
       }
    
       fill(0,255,255);
       noStroke();
       ellipse(width/2,y,30,30);
     }
    

    用 frameCount 变化背景色

    void setup(){
      size(400,400);
      colorMode(HSB);
    }
    
    void draw(){
      background(frameCount%256,100,255);
    }
    

    expected: the ranges should be now 255, 255, 255 for RGB.
    observed: the ranges are now 360 for R, 100 for G, 100 for