Last active
May 3, 2020 00:20
-
-
Save georgesb/b789728caa7dd7e05e6d12266ba8cfce to your computer and use it in GitHub Desktop.
Bounce
This file contains 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
<script src="https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.js"></script> | |
<script src="script.js"></script> |
This file contains 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
let speed = 4; | |
let diameter = 32; | |
let radius = diameter / 2; | |
let x = diameter; | |
function setup() { | |
createCanvas(400, 400); | |
noStroke(); | |
} | |
function draw() { | |
background(240); | |
// Add the current speed to the x location. | |
x = x + speed; | |
// Remember, || means "or." | |
if ((x + radius > width) || (x - radius < 0)) { | |
/* If the object reaches either edge, | |
multiply speed by -1 to turn it around. */ | |
speed = speed * -1; | |
} | |
// Display circle at x location | |
fill(255, 0, 0); | |
ellipse(x, height / 2, diameter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment