Skip to content

Instantly share code, notes, and snippets.

@rahji
Created October 18, 2023 15:42
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 rahji/80d1476b9b8c443c2581e29c04aefe57 to your computer and use it in GitHub Desktop.
Save rahji/80d1476b9b8c443c2581e29c04aefe57 to your computer and use it in GitHub Desktop.
2023 Fall p5play in-class demo part 2
let kitty;
let block;
let beep;
let showText = false;
function preload() {
// don't forget to uncomment the line in the HTML that loads
// the p5.sound library if you want to play sounds!
beep = loadSound('assets/beep.wav');
kitty = new Sprite(10, 20, 14, 16);
kitty.spriteSheet = 'assets/Basic Charakter Spritesheet 14x16.png';
kitty.anis.frameDelay = 8;
kitty.addAnis({
front: { row: 0, frames: 4 },
left: { row: 2, frames: 4 },
});
kitty.changeAni('front');
kitty.bounciness = .4;
}
function setup() {
new Canvas(128, 128, 'pixelated x4');
allSprites.pixelPerfect = true;
allSprites.rotationLock = true;
world.gravity.y = 3;
// the block that gets pushed around
block = new Sprite(40,50,10,10);
block.drag = 1;
// the platform
// (I don't need to define the platform variable way at the top
// since it's only referenced inside this setup function)
let platform = new Sprite(0,80,100,4);
platform.collider = "static";
//platform.friction = 0;
}
function draw() {
clear();
if (kb.pressing('left')) {
kitty.changeAni('left');
kitty.mirror.x = false;
kitty.vel.x = -.5;
kitty.vel.y = 0;
} else if (kb.pressing('right')) {
kitty.changeAni('left');
kitty.mirror.x = true;
kitty.vel.y = 0;
kitty.vel.x = .5;
}
// put the showText flag up if the kitty touches the block
if (kitty.collides(block)) {
showText = true;
beep.play();
}
// if the flag is up then show the text
if (showText == true) text("hello", 10, 10);
// put the flag down if the block has stopped moving
// PS: the text wouln't disappear if the block was still falling, off-screen,
// so I added an extra condition here: speed is 0 *OR* it's off the screen!
if (block.speed == 0 || block.y > height)
showText = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment