Skip to content

Instantly share code, notes, and snippets.

@claytical
Created November 17, 2015 17:10
Show Gist options
  • Save claytical/8cc8deee1ff86350073e to your computer and use it in GitHub Desktop.
Save claytical/8cc8deee1ff86350073e to your computer and use it in GitHub Desktop.
Simple Bouncing Ball
var balls = [];
var gravity = .1;
function setup() {
createCanvas(windowWidth,windowHeight);
}
function draw() {
background(255,255,255);
for (var i = 0; i < balls.length; i++) {
balls[i].display();
}
}
function mousePressed() {
balls.push(new Ball());
}
var Ball = function() {
this.x = mouseX;
this.y = mouseY;
this.speedY = 1;
this.diameter = random(20,50);
}
Ball.prototype.display = function() {
ellipse(this.x, this.y, this.diameter, this.diameter);
this.y += this.speedY;
if (this.y >= height) {
this.speedY *= -.8;
}
this.speedY += gravity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment