Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ball = []; //Array of balls
- function setup() {
- createCanvas(400, 400);
- }
- function draw() {
- background(100);
- for(var i = 0; i < ball.length; i++)
- {
- ball[i].show();
- ball[i].move();
- }
- }
- //Called whenever the mouse button is pressed
- function mousePressed() {
- var b = new Ball(); //Or var b = new Ball();
- ball.push(b);
- }
- /* Class Ball
- * VARIABLES:
- * Postions (x and y)
- * Velocity (Vx and Vy)
- * Color control (r, g ,b)
- *
- * METHODS:
- * constructor()
- * show()
- * bounce()
- */
- class Ball
- {
- constructor()
- {
- this.x = mouseX;
- this.y = mouseY;
- this.Vx = 0;
- this.Vy = 5;
- this.r = random(0,255); //Between 0 and 255
- this.g = random(0, 255);
- this.b = random(0, 255);
- }
- show()
- {
- fill(this.r, this.g, this.b);
- circle(this.x, this.y, 10);
- }
- move()
- {
- this.bounce();
- this.x += this.Vx;
- this.y += this.Vy;
- }
- bounce()
- {
- if(this.y > height || this.y < 0)
- {
- this.Vy *= -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement