xxxxxxxxxx
287
/*
p5 rin quiroz
removed p5 generated character sprite & added animated GIF's to replace player sprite
properly coded player entity and fish collatable
tidied up coding and organized lines within their proper lines
added background music
added collision element between player and both fish entities
*/
let gamer = Player();
let fish1, fish2;
let screenCurrent = 0;
//0 is start
//1 is game menu
//2 is retry menu
let collisionRadius = 50;
let points = 0;
let stinkyIdle, stinkyWalk, stinkyEat;
let fishLive;
let titleBG;
let startButtonImg, retryButtonImg;
let backgroundMusic, gigaChomp;
//runs once, before programs starts
function preload() {
stinkyIdle = loadImage("images/stinkyIdle.gif");
stinkyWalk = loadImage("images/stinkyWalk.gif");
stinkyEat = loadImage("images/stinkyEat.gif");
fishLive = loadImage("images/feesh.png");
titleBG = loadImage("interface/menubg.png");
startBut = loadImage("interface/start.png");
retryBut = loadImage("interface/restart.png");
backgroundMusic = loadSound("audio/retro-game-arcade-short-236130.mp3");
gigaChomp = loadSound("audio/gatobite.mp3");
}
function setup() {
createCanvas(500, 500);
imageMode(CENTER);
fish1 = Fish();
fish2 = Fish();
buttonStart = Button(250, 400, startBut);
buttonRetry = Button(250, 400, retryBut);
backgroundMusic.play();
backgroundMusic.loop(true);
backgroundMusic.setVolume(0.05);
gigaChomp.setVolume(0.09);
}
function draw() {
background(220);
if (screenCurrent == 0) {
menuStart();
} else if (screenCurrent == 1){
playingGame();
} else if (screenCurrent == 2){
retryMenu();
}
}
function menuStart() {
image(titleBG, width / 2, height / 2);
buttonStart.display();
if (buttonStart.clicked()) {
screenCurrent = 1;
}
}
function playingGame() {
gamer.move();
gamer.display();
fish1.move();
fish1.display();
fish2.move();
fish2.display();
//detect for collision with fish1 sprites
let fish1Ate = detectCollision(gamer, fish1);
let fish2Ate = detectCollision(gamer, fish2);
//result of fish1ate collision
if (fish1Ate && fish1.isFishAlive()) {
gamer.eat();
fish1.eaten();
gigaChomp.play();
points = points + 1;
}
if (fish2Ate && fish2.isFishAlive()) {
gamer.eat();
fish2.eaten();
gigaChomp.play();
points = points + 1;
}
textSize(40);
fill("red");
text("fish:", 370, 55)
text(points, 460, 55);
if(points == 20){
screenCurrent = 2;
points = points - 20;
}
}
function retryMenu() {
image(titleBG, width / 2, height / 2);
buttonRetry.display();
if (buttonRetry.clicked()) {
screenCurrent = 1;
}
}
function Button(x, y, img) {
let w = 384;
let h = 90;
let isClicked = false;
function display() {
image(img, x, y);
}
function clicked() {
if (mouseIsPressed){
if (mouseX > x - w / 2 &&
mouseX < x + w / 2 &&
mouseY > y - h / 2 &&
mouseY < y + h / 2) {
if (!isClicked) {
isClicked = true;
return true;
}
}
} else if (isClicked) {
isClicked = false;
}
return false;
}
return { display, clicked };
}
function Player() {
let x = 200;
let y = 300;
let radius = 50;
let currentAnim = 0;
function move(){
if (currentAnim != 2) {
currentAnim = 0;
}
if (keyIsDown(RIGHT_ARROW)) {
x = x + 5;
currentAnim = 1;
}
if (keyIsDown(LEFT_ARROW)) {
x = x - 5;
currentAnim = 1;
}
}
function getPosition(){
return {x, y };
}
function eat() {
currentAnim = 2;
}
function display() {
if (currentAnim == 0) {
image(stinkyIdle, x, y);
} else if (currentAnim == 1) {
image(stinkyWalk, x, y);
} else if (currentAnim == 2) {
image(stinkyEat, x, y);
}
//forces idle animation if no keys are pressed
if (keyIsDown) {
currentAnim = 0;
}
//debugging detection area
//circle (x, y, collisionRadius * 2)
}
return { move, display, getPosition, eat };
}
function Fish() {
let x = random(0, width);
let y = 0;
let speed = random (2, 9);
let livefeesh = true;
let currentAnim = 0;
function move() {
y = y + speed;
if (y > height) {
reset();
}
}
function reset() {
y = 0;
x = random(0, width);
speed = random(2, 9);
currentAnim = 0;
livefeesh = true;
}
function display() {
if (currentAnim == 0) {
image(fishLive, x, y);
} else if (currentAnim == 1) {
fishLive.hide();
}
//debugging detection area
//circle (x, y, collisionRadius * 2)
}
function getPosition(){
return {x, y };
}
function eaten(){
currentAnim = 2;
livefeesh = false;
}
function isFishAlive(){
return livefeesh;
}
return { move, display, getPosition, eaten, isFishAlive };
}
function detectCollision(objA, objB) {
let aPosition = objA.getPosition();
let bPosition = objB.getPosition();
let d = dist(aPosition.x, aPosition.y, bPosition.x, bPosition.y);
let isColliding = d < collisionRadius;
return isColliding;
}