Ani­m­a­t­i­n­g with Code: FINAL DESIGN

by Sonia White
  • Show Sketch
/** @peep sketchcode */
/* @pjs font=/uploads/16006/controllertwooblique.otf; */
Ball[] balls;
 
//-------------setup array of Ball: as balls---------------
void setup() {
  size(400, 400);
  balls = new Circle[100];
  for (int i = 0; i < balls.length; i++) {
    balls[i] = new Ball(width/2, height/2, random((width/35), (width/15)), color(random(250,255),random(230,255),random(60,80)), random(TWO_PI), random(0.3, 2.5));
  }
}
 
//------------draw the array--------------
void draw() {
  background(255);
  for (int i = 0; i < balls.length; i++) {
    Ball b = balls[i];
    if(b.x<0 || b.x>width || b.y<0 || b.y>height){
      //if Ball goes beyond screen, reset position to the centre
      b.x=width/2;
      b.y=height/2;
      b.speed=random(0.3,0.5);
      b.angle=random(TWO_PI);
    }
    b.draw();
  }
  PFont font = createFont("/uploads/16006/controllertwooblique.otf",width/20+3);
textFont(font);
fill(0); 
text("Design Lab", (width/2-(width/4.5)), (height/2)+8)
}
 
//--------------------------------CLASS BALL------------------------------------
 
class Ball {
  float x;
  float y;
  float radius;
  int colour;
  float angle;
  float speed;
 
  Ball(float _x, float _y, float _radius, float _colour, float _angle, float _speed) {
    x = _x;
    y = _y;
    radius = _radius;
    colour = _colour;
    angle = _angle;
    speed = _speed;
  }
 
 //----------------calculate the movement of individual ball-----------
  void move() {
    float dx = cos(angle) * speed;
    float dy = sin(angle) * speed;
    x += dx;
    y += dy;
  }
 
 //---------------update position--------------
  void draw() {
    move();
    noStroke();
  fill(colour);
    ellipse(x, y, 2*radius, 2*radius);
    }
}

refresh to explode!

Comments

Nobody has said anything yet.