var image1_up, image2_over,snd1; // var duration; // var slideWidth = 500; function preload() { snd1 = loadSound("sound/bells.mp3"); } // define your p5.play sprites as global objects first. var ghosty; // global manager object var mgr; function setup() { createCanvas(600, 600); // console.log(hell); mgr = new SceneManager(); masterVolume(5); ghosty = createSprite(0, 0); ghosty.addAnimation("normal", "assets/frame_apngframe1.png", "assets/frame_apngframe9.png"); ghosty.addAnimation("stand", "assets/frame_00_delay-0.04s.png", "assets/frame_55_delay-0.04s.png"); // Preload scenes. Preloading is normally optional // ... but needed if showNextScene() is used. mgr.addScene (scene1); mgr.addScene (scene2); mgr.addScene (scene3); mgr.showNextScene(); } function draw() { // passthe current draw function into the SceneManager mgr.draw(); } function mousePressed() { // pass the mousePressed message into the SceneManager mgr.mousePressed(); } function keyPressed() { // You can optionaly handle the key press at global level... switch(key) { case '1': mgr.showScene( scene1 ); break; case '2': mgr.showScene( scene2 ); break; case '3': mgr.showScene( scene3 ); break; } // ... then dispatch via the SceneManager. mgr.keyPressed(); }
For this sketch, I attempted to use new libraries I have never used before such as scenemanager and p5 sound.
var groundSprites; var GROUND_SPRITE_WIDTH = 50; var GROUND_SPRITE_HEIGHT = 50; var numGroundSprites; var GRAVITY = 0.3; var JUMP = -5; var obstacleSprites; var player; var isGameOver; var isHelp; var score; var anim; var toySprite; var choco; var bone; var player2; var anim2; var scribble; // global mode // var bgm; function preload() { bgm = loadSound("bg_music.mp3"); anim = loadAnimation("assets/corg1c.png", "assets/corg2c.png", "assets/corg3c.png", "assets/corg4c.png"); choco = loadImage("assets/chocolate.png"); bone = loadImage("assets/bone.png"); anim2 = loadAnimation("assets/corgsleep1.png", "assets/corgsleep2.png"); bark = loadSound("assets/bark.mp3"); whim = loadSound("assets/pupwhimper.mp3"); } function setup() { createCanvas(1280, 720); background(150, 200, 250); // loadSound("bg_music.mp3"); bgm.play(); masterVolume(2); // scale(0.00001); player = createSprite(100, height - 165); player.addAnimation("default", anim); // player.addAnimation("assets/corg1.png", "assets/corg4.png"); playerHeight = player.position.y; isGameOver = false; score = 0; setGroundSprites(); obstacleSprites = new Group(); toySprite = new Group(); gif = loadGif('assets/corg.gif'); // player2 = createSprite( camera.position.x, camera.position.y); // player2.addAnimation("default", anim2); frameRate(60) createLoop({duration:3, gif:true}) } function draw() { if (isGameOver) { endScreen(); } else if (isHelp) { helpScreen(); } else { background(150, 200, 250); worldMovement(); controls(); obstacleSprites.overlap(player, endGame); toySprite.overlap(player, getToy); drawSprites(); drawObstacles(); drawToys(); score = score + 1; textAlign(CENTER); textSize(24); text ('Score: ', camera.position.x - 600, 24); text(score, camera.position.x - 525, 24); } } function worldMovement() { //Sets the player and ground sprite positions player.position.x = player.position.x + 5; camera.position.x = player.position.x + width / 4; var firstGroundSprite = groundSprites[0]; if (firstGroundSprite.position.x <= camera.position.x - (width / 1.5 + firstGroundSprite.width / 1.5)) { groundSprites.remove(firstGroundSprite); firstGroundSprite.position.x = firstGroundSprite.position.x + numGroundSprites * firstGroundSprite.width; groundSprites.add(firstGroundSprite); } player.velocity.y = player.velocity.y + GRAVITY; if (groundSprites.overlap(player)) { player.velocity.y = 0; playerHeight = height - player.height / 2; } } function setGroundSprites() { //Creates Ground Sprites groundSprites = new Group(); numGroundSprites = width / GROUND_SPRITE_WIDTH + 1; for (var n = 0; n < numGroundSprites; n++) { var groundSprite = createSprite( n * 50, height - 25, GROUND_SPRITE_WIDTH, GROUND_SPRITE_HEIGHT ) groundSprites.add(groundSprite); } } function drawObstacles() { // Spawns Obstacles if (random() > 0.99) { var obstacle = createSprite(camera.position.x + width, random(0, height - 50 - 15), 15, 15); obstacleSprites.add(obstacle); obstacle.addImage(choco); choco.resize(50,60); } var firstObstacle = obstacleSprites[0]; if (obstacleSprites.length > 0 && firstObstacle.position.x <= camera.position.x - (width / 2 + firstObstacle.width / 2)) { removeSprite(firstObstacle); } } function endGame() { //Ends the Game isGameOver = true; console.log('Game Over!'); whim.play(); } function endScreen() { //Game Over screen background(0); fill(255); textAlign(CENTER); textSize(24); text('Game Over! Click anywhere to restart', camera.position.x, camera.position.y); text('Your Score was: ' + score, camera.position.x, camera.position.y - 40); } function helpScreen() { //Help Screen // anim2.play(); // anim2.addAnimation(); background(143, 74, 237); fill(255); textAlign(CENTER); textSize(24); text('Help the Corgi find his Bones!', camera.position.x, camera.position.y); text('Protect the Corgi! Avoid all the candy bars', camera.position.x, camera.position.y + 50); text('Press the Up Arrow to Jump!', camera.position.x, camera.position.y + 100); // player2 = createSprite( camera.position.x, camera.position.y); // translate(width / 2, height / 2) // const radius = height / 3 // const x = cos(animLoop.theta) * radius // const y = sin(animLoop.theta) * radius // anim2.play(); // player2.position.x=500; // player2.position.y=500; } function controls() { //Player Controls if (keyDown(UP_ARROW)) { player.velocity.y = JUMP; } if (keyDown(ENTER)) { isHelp = true; let oldScore = score; } if (keyDown (CONTROL)) { isGameOver = true; } } function mouseClicked() { if (isGameOver) { isGameOver = false; score = 0; restart(); } else if (isHelp) { isHelp = false; // score = oldScore; } } function restart() { //Restart Logic for (var n = 0; n < numGroundSprites; n++) { //Resets Ground Sprites var groundSprite = groundSprites[n]; groundSprite.position.x = n * 50; } player.position.x = 100; //Resets Player Position player.position.y = height - 75; obstacleSprites.removeSprites(); //Clear obstacles } function drawToys() { // Spawns Toys if (random() > 0.95) { var toy = createSprite(camera.position.x + width, random(0, height - 50 - 15), 50, 50); toySprite.add(toy); toy.addImage(bone); bone.resize(50,60); } var firstToy = toySprite[0]; if (toySprite.length > 0 && firstToy.position.x <= camera.position.x - (width / 2 + firstToy.width / 2)) { removeSprite(firstToy); } } function getToy(toyos,player) { toyos.remove(); score += 100; bark.play(); } function clouds() { scribbleEllipse = scribble.scribbleEllipse(100,100, 50, 50); } // function stars() { // particlesJS("particles-js", { // "particles": { // "number": { // "value": 355, // "density": { // "enable": true, // "value_area": 789.1476416322727 // } // }, // "color": { // "value": "#ffffff" // }, // "shape": { // "type": "circle", // "stroke": { // "width": 0, // "color": "#000000" // }, // "polygon": { // "nb_sides": 5 // }, // "image": { // "src": "img/github.svg", // "width": 100, // "height": 100 // } // }, // "opacity": { // "value": 0.48927153781200905, // "random": false, // "anim": { // "enable": true, // "speed": 0.2, // "opacity_min": 0, // "sync": false // } // }, // "size": { // "value": 2, // "random": true, // "anim": { // "enable": true, // "speed": 2, // "size_min": 0, // "sync": false // } // }, // "line_linked": { // "enable": false, // "distance": 150, // "color": "#ffffff", // "opacity": 0.4, // "width": 1 // }, // "move": { // "enable": true, // "speed": 0.2, // "direction": "none", // "random": true, // "straight": false, // "out_mode": "out", // "bounce": false, // "attract": { // "enable": false, // "rotateX": 600, // "rotateY": 1200 // } // } // }, // "interactivity": { // "detect_on": "canvas", // "events": { // "onhover": { // "enable": true, // "mode": "bubble" // }, // "onclick": { // "enable": true, // "mode": "push" // }, // "resize": true // }, // "modes": { // "grab": { // "distance": 400, // "line_linked": { // "opacity": 1 // } // }, // "bubble": { // "distance": 83.91608391608392, // "size": 1, // "duration": 3, // "opacity": 1, // "speed": 3 // }, // "repulse": { // "distance": 200, // "duration": 0.4 // }, // "push": { // "particles_nb": 4 // }, // "remove": { // "particles_nb": 2 // } // } // }, // "retina_detect": true // }); // }; // function drawObstacles() { // Spawns Obstacles // if (random() > 0.95) { // var obstacle = createSprite(camera.position.x + width, random(0, height - 50 - 15), 15, 15); // obstacleSprites.add(obstacle); // // } // var firstObstacle = obstacleSprites[0]; // if (obstacleSprites.length > 0 && firstObstacle.position.x <= camera.position.x - (width / 2 + firstObstacle.width / 2)) { // removeSprite(firstObstacle); // } // }
For project 5, I made a 2D platformer. I created all of the sprites and places them into the game I made with various p5 libraries. The libraries I used were p5 sound, p5 createloop, p5 play. There are 3 different scenes: the help screen, the game, and the endgame screen. The goal of the game is to capture as many bones as possible while avoiding the candy bars.