Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Array of Balls
- /** Array of Balls
- * This will use arrays to handle
- * numberous balls
- */
- /* GLOBAL VARIABLES HERE */
- //POSITION VARIABLES
- var x = []; //[] means This is an array
- var y = [];
- //VELOCITY VARIABLES
- var Vx = [];
- var Vy = [];
- //Colors
- var r = [];
- var g = [];
- var b = [];
- //OTHER VARIABLES
- var rad; //radius
- var numBalls;
- function setup() {
- createCanvas(400, 400);
- rad = 20;
- numBalls = 50;
- //Populate the array
- for(var i = 0; i < numBalls; i++){
- x[i] = random(width); //i = index
- y[i] = random(height);
- r[i] = random(255);
- g[i] = random(255);
- b[i] = random(255);
- Vx[i] = random(-5,5);
- Vy[i] = random(-5,5);
- }
- }
- function draw() {
- background(120);
- //Draw the array of balls
- for(var i = 0; i < numBalls; i++){
- x[i] += Vx[i];
- y[i] += Vy[i];
- fill(r[i], g[i], b[i], 200);
- ellipse(x[i], y[i], rad, rad);
- if(x[i] > width || x[i] < 0){
- Vx[i] *= -1; //REVERSE THE Vx
- }
- if(y[i] > height || y[i] < 0){
- Vy[i] *= -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment