Created
November 17, 2015 17:10
-
-
Save claytical/8cc8deee1ff86350073e to your computer and use it in GitHub Desktop.
Simple Bouncing Ball
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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