Skip to content

Instantly share code, notes, and snippets.

@GoSubRoutine
Last active October 21, 2020 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoSubRoutine/cf739128f389766f5d9cb631c9c358a6 to your computer and use it in GitHub Desktop.
Save GoSubRoutine/cf739128f389766f5d9cb631c9c358a6 to your computer and use it in GitHub Desktop.
p5.Play Instance Mode Example
height: 600
scrolling: no
border: yes
license: cc-by-4.0
<meta charset=utf8>
<meta name=viewport content=width=device-width,initial-scale=1>
<script defer src=https://cdn.JsDelivr.net/npm/p5></script>
<script defer src=https://MolleIndustria.GitHub.io/p5.play/lib/p5.play.js></script>
<script defer src=sketch.js></script>
/**
* p5.Play Instance Mode Example (v1.0.1)
* GoToLoop (2019-Apr-04)
*
* https://GitHub.com/molleindustria/p5.play/issues/12
* https://Bl.ocks.org/GoSubRoutine/cf739128f389766f5d9cb631c9c358a6
*/
"use strict";
new p5(p => {
let bg, sprite;
p.setup = function () {
p.createCanvas(800, 600).mousePressed(reset);
bg = p.color(0o350);
sprite = p.createSprite(p.width>>1, p.height>>1, 0o100, 0o100);
reset();
};
p.draw = function () {
p.background(bg);
bounce();
p.drawSprites();
};
function reset() {
sprite.setVelocity(p.random(-5, 5), p.random(-5, 5));
sprite.shapeColor = p.color('#' + p.hex(~~p.random(0x1000), 3));
}
function bounce() {
const { position: { x, y }, width: w, height: h } = sprite,
ww = w >> 1, hh = h >> 1;
if (x < ww || x + ww >= p.width) sprite.velocity.x *= -1;
if (y < hh || y + hh >= p.height) sprite.velocity.y *= -1;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment