Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js"></script>
    
<script src="https://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                /* PDM Course: Sound Unit

Adding Sound File Playback on Collision
Advanced Sprite Game using the p5.play.js and Tone.js libraries
(Animations, Groups, Collisions, and Interaction)

USE THE ARROW KEYS TO COLLECT ALL OF THE BONES!!

Soundfiles from Freesound.org


Dog, bone, and hydrant sprite pictures used through open license, sourced from opengameart.com

Dog and bone created by user kirard, hydrant by patvanmackelberg

Code by Anthony T. Marasco [2019]
Updated for Tone V14 by Matthew A. Bardin [2021]
*/

let photoDirectory = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2034654/";
let dog_sprites = [];
let hydrants = [];
let bones = [];
let hydrantPic, bonePic, rover, titleSpace, sfPlayer;
let boneCount = 0;


function preload() {
  // for loop to load all of our individual dog pictures and add them into
  // an array
  for (let i = 0; i <= 9; i++) {
    dog_sprites[i] = loadImage(photoDirectory + "dog_" + i + ".png");
  }

  // individual pictures
  hydrantPic = loadImage(photoDirectory + "hydrant_new.png");

  bonePic = loadImage(photoDirectory + "Bone.png");
  
  //create our soundfile player
  sfPlayer = new Tone.Players({
      cash: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2034654/cha-ching.wav",
      beep: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2034654/beep-short.mp3"
  }).toDestination();;
  
}

function setup() {
  createCanvas(windowWidth, windowHeight);

  /*make a new Sprite that hides behind our title text, in order to keep our main character from walking infront/behind it
  */
  titleSpace = createSprite(0, 0, 360, 100);
  titleSpace.shapeColor = color(255, 255, 255, 1);

  //our main character, Rover!
  rover = createSprite(width, height / 2);

  /*Create an animation, made up of 10 individual still frames. By creating a local variable to store our .addAnimation() method in, we can then adjust aspects of how
 the animation behaves by simply addressing the variable name of our animation afterwards.*/
  let dog_animation = rover.addAnimation(
    "walking",
    dog_sprites[0],
    dog_sprites[1],
    dog_sprites[2],
    dog_sprites[3],
    dog_sprites[4],
    dog_sprites[5],
    dog_sprites[6],
    dog_sprites[7],
    dog_sprites[8],
    dog_sprites[9]
  );

  /*Look at the animation assigned to dog_animation and delay its frame rate to be the a quarter of the speed of the draw() loop's frame rate. */
  dog_animation.frameDelay = 4;

  //scale down Rover sprite/images to 9/10's original size
  rover.scale = 0.9;

  /*starts the game with our main character constantly moving to the left at 1.5 pixels each frame*/
  rover.velocity.x = -1.5;
  /*Adding an event-based function to our man character. The use of the generic word "this" is good practice, especially when we do this for Groups. It allows any sprite in a group with a shared label to behave the same way */
  rover.onMousePressed = function() {
    this.rotation = 25;
  };

  rover.onMouseReleased = function() {
    this.rotation = 0;
  };

  hydrants = new Group();

  for (let i = 0; i <= 4; i++) {
    let h = createSprite(random(width), random(height));
    h.addImage(hydrantPic);
    h.scale = 2;
    hydrants.add(h);
  }

  bones = new Group();

  for (let i = 0; i <= 10; i++) {
    let b = createSprite(random(width), random(height));
    b.addImage(bonePic);
    b.scale = 1;
    b.rotation = random(0, 360);
    bones.add(b);
  }

  bones.displace(bones);
  bones.displace(hydrants);
  hydrants.displace(hydrants);

  titleSpace.displace(hydrants);
  titleSpace.displace(bones);
  
}

function draw() {
  background("lightBlue");
  rover.collide(titleSpace);
  //stops our main character from moving through hydrants
  rover.collide(hydrants, stay);

  if (keyIsPressed) {
    if (keyDown(UP_ARROW)) {
      rover.rotation = 90;
      rover.velocity.y = -1.5;
      rover.velocity.x = 0;
    } else if (keyDown(DOWN_ARROW)) {
      rover.rotation = 270;
      rover.velocity.y = 1.5;
      rover.velocity.x = 0;
    } else if (keyDown(LEFT_ARROW)) {
      rover.mirrorX(1);
      rover.rotation = 0;
      rover.velocity.y = 0;
      rover.velocity.x = -1.5;
    } else if (keyDown(RIGHT_ARROW)) {
      rover.mirrorX(-1);
      rover.rotation = 0;
      rover.velocity.y = 0;
      rover.velocity.x = 1.5;
    }
    rover.animation.play();
  }

  if (rover.mouseIsOver) {
    textSize(32);
    strokeWeight(2);
    text("woof!!!", rover.position.x - 20, rover.position.y - 55);
  }

  rover.collide(bones, collectBones);
  

  
  drawSprites();
  
  if (bones.length > 0) {
   fill(0);
   textSize(20);
  text("Bones Collected: " + boneCount, 10, 20);
  }
  else {
    noStroke();
    fill(255, 0, 0);
    textAlign(CENTER);
   textSize(50);
    text("You win!", width/2, height/2);
    
    rover.animation.stop();
    rover.velocity.y = 0;
  rover.velocity.x = 0;
    
  }
}

function collectBones(character, bone) {
  bone.remove();
  boneCount++;
  sfPlayer.player("beep").start();
  if (bones.length === 0) {
   sfPlayer.player("cash").start();
  }
}

function stay(character) {
  character.animation.goToFrame(4);
  character.velocity.y = 0;
  character.velocity.x = 0;
}

              
            
!
999px

Console