Skip to content

Instantly share code, notes, and snippets.

@attractivechaos
Created June 21, 2021 19:39
Show Gist options
  • Save attractivechaos/28b9d0744023331338bea415592af74d to your computer and use it in GitHub Desktop.
Save attractivechaos/28b9d0744023331338bea415592af74d to your computer and use it in GitHub Desktop.
Bouncing ball
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.min.js"></script>
<script type="application/processing">
float posx, posy, r, d = 10, vx, vy, sx, sy;
void setup() {
size(800, 600);
posx = width / 2, posy = height / 2, r = d/2;
vx = vy = 0, sx = sy = 0.1;
}
void draw() {
background(255);
if (keyPressed && key == CODED) {
if (keyCode == LEFT) vx -= sx;
else if (keyCode == RIGHT) vx += sx;
}
if (mousePressed && mouseButton == LEFT) {
background(255);
posx = mouseX, posy = mouseY, vx = vy = 0;
} else {
vy += sy;
posx += vx, posy += vy;
if (posy + r >= height) posy = height - r, vy = -vy;
if (posx - r < 0) posx = r, vx = -vx;
if (posx + r >= width) posx = width - r, vx = -vx;
}
ellipse(posx, posy, d, d);
}
</script>
<canvas></canvas>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment