Skip to content

Instantly share code, notes, and snippets.

@georgesb
Last active May 3, 2020 00:20
Show Gist options
  • Save georgesb/b789728caa7dd7e05e6d12266ba8cfce to your computer and use it in GitHub Desktop.
Save georgesb/b789728caa7dd7e05e6d12266ba8cfce to your computer and use it in GitHub Desktop.
Bounce
<script src="https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.js"></script>
<script src="script.js"></script>
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