xxxxxxxxxx
//Assets Used:
// - Sound effects:https://freesound.org/
// - Backround Image: https://www.vecteezy.com/free-vector/kitchen-background
// - Background Music: https://www.purple-planet.com/tracks/on-the-loose
// Code References:
// - Keyboard Movement: Learned from p5play's "Keyboard Movement" (https://p5play.org/learn/input.html?page=1)
//Learning Resources:
// - ChatGPT
// - p5play Keyboard Movement: https://p5play.org/learn/
// - IMM 120 Class Notes & Presentations
// Instructions:
//You (player) are a cat, trying to collect as much food as
//possible. There are good foods, and bad foods.
//The bad foods are poisonous to cats,so be careful!
// If you collect all the good foods, you win the game!
// If all the poisonous food is collected, you lose!
let cat;
let kitchen;
let sushi;
let shrimp;
let waffle;
let bagel;
let fries;
let banana;
let dumpling;
let chocolateBar;
let garlic;
let grapes;
let score = 0;
let gameOver = false;
let lost = false;
function preload() {
meowSound = loadSound("732519__lukey1028__senior-cat-meow.mp3");
angrycatSound = loadSound("4914__noisecollector__cat2 (1).wav");
kitchen = loadImage("kitchenBackground.jpg");
backgroundMusic = loadSound("On the Loose.mp3");
//catGIF = loadImage("catwalking.gif");
}
function setup() {
//new Canvas(windowWidth, windowHeight);
new Canvas(1000, 775);
// Background Music
backgroundMusic.setVolume(0.5); // 50% of max volume
backgroundMusic.loop();
meowSound.setVolume(2.5); // Cat meow is louder
cat = new Sprite (0, 0);
cat.width = 100;
cat.height = 100;
// cat.image = '🐱'
//cat.addImage(catGIF);
// loadAni(spriteSheet, atlas)
cat.addAni('rb_2149732583.png', {
width: 1000,
height: 1000,
frames: 6
});
//Loading in food sprites
sushi = new Group();
sushi.width = 20;
sushi.height = 20;
sushi.image = '🍣';
for (let i = 0; i < 5; i++) {
let su = new sushi.Sprite();
su.x = random(width);
su.y = random(height);
}
shrimp = new Group();
shrimp.width = 20;
shrimp.height = 20;
shrimp.image = '🍤';
for (let i = 0; i < 5; i++) {
let sh = new shrimp.Sprite();
sh.x = random(width);
sh.y = random(height);
}
waffle = new Group();
waffle.width = 60;
waffle.height = 60;
waffle.image = '🧇';
for (let i = 0; i < 3; i++) {
let w = new waffle.Sprite();
w.x = random(width);
w.y = random(height);
}
bagel = new Group();
bagel.width = 50;
bagel.height = 50;
bagel.image = '🥯';
for (let i = 0; i < 3; i++) {
let b = new bagel.Sprite();
b.x = random(width);
b.y = random(height);
}
fries = new Group();
fries.width = 50;
fries.height = 50;
fries.image = '🍟';
for (let i = 0; i < 4; i++) {
let f = new fries.Sprite();
f.x = random(width);
f.y = random(height);
}
banana = new Group();
banana.width = 40;
banana.height = 40;
banana.image = '🍌';
for (let i = 0; i < 4; i++) {
let ban = new banana.Sprite();
ban.x = random(width);
ban.y = random(height);
}
dumpling = new Group();
dumpling.width = 20;
dumpling.height = 20;
dumpling.image = '🥟';
for (let i = 0; i < 5; i++) {
let d = new dumpling.Sprite();
d.x = random(width);
d.y = random(height);
}
chocolateBar = new Group();
chocolateBar.width = 40;
chocolateBar.height = 40;
chocolateBar.image = '🍫';
for (let i = 0; i < 5; i++) {
let c = new chocolateBar.Sprite();
c.x = random(width);
c.y = random(height);
}
garlic = new Group();
garlic.width = 25;
garlic.height = 25;
garlic.image = '🧄';
for (let i = 0; i < 4; i++) {
let g = new garlic.Sprite();
g.x = random(width);
g.y = random(height);
}
grapes = new Group();
grapes.width = 40;
grapes.height = 40;
grapes.image = '🍇';
for (let i = 0; i < 5; i++) {
let gr = new grapes.Sprite();
gr.x = random(width);
gr.y = random(height);
}
}
function draw() {
if (gameOver) {
displayGameOver();
return; // Stop the game from continuing
}
if (lost) {
displayYouLost();
return;
}
background('rgb(117,164,117)');
image(kitchen, 0, 0);
// clear();
//animation(cat, 100, 100);
// image(cat.spriteSheet, 200, 50, 300, 92);
//Camera POV - I origionally added this aspect, but removed it when I added
//the kitchen background because it made it look like the food was moving.
// camera.x = cat.x
// camera.y = cat.y
// // Keyboard commands
// cat.speed = 5;
// if (kb.pressing('up')) {
// cat.direction = -90;
// } else if (kb.pressing('down')) {
// cat.direction = 90;
// } else if (kb.pressing('left')) {
// cat.direction = 180;
// } else if (kb.pressing('right')) {
// cat.direction = 0;
// } else {
// cat.speed = 0;
// }
// The next few commands I referenced ChatGPT to learn how todo. I enjoyed playing around with
// the tool to see what it is capable of. I asked how to create a "Game over message" and I applied
// the logic to my specific code and variables. I then used what I learned to create a "you lost" message.
// The cat can only move if the game is active
if (!gameOver && !lost) {
handleCatMovement();
// Check if all good foods are collected
checkGameOver();
// Check if the player lost (all poisonous foods or negative score)
checkLost();
// Score Display
fill('rgb(117,164,117)');
textSize(40);
text(`Score: ${score}`, 20, 50);
// Player collections
cat.overlaps(sushi, collectsushi);
cat.overlaps(shrimp, collectshrimp);
cat.overlaps(waffle, collectwaffle);
cat.overlaps(bagel, collectbagel);
cat.overlaps(fries, collectfries);
cat.overlaps(banana, collectbanana);
cat.overlaps(dumpling, collectdumpling);
cat.overlaps(chocolateBar, collectchocolateBar);
cat.overlaps(garlic, collectgarlic);
cat.overlaps(grapes, collectgrapes);
}
}
function checkGameOver() {
if (sushi.length === 0 && shrimp.length === 0 && waffle.length === 0 &&
bagel.length === 0 && fries.length === 0 && banana.length === 0 &&
dumpling.length === 0) {
// All good foods have been collected, trigger game over
gameOver = true; // Game Over message will appear if all good foods are collected
}
} // This checkGameOver went through a lot of trial and error. I referenced ChatGPT for some guding instructions.
// I origionally tried to make the good foods and poisonous foods into seperate groups, but this was challenging
// because they are already within groups in order to randomize placement (as we learned in class).
// Because of this, I referenced ChatGPT to figure out how I could create a message without a second grouping,
// which resulted in the code I currently have. This process also applied to checking to see if all the poisonous foods
// were collected.
function checkLost() {
// Check if all poisonous foods are collected
if (chocolateBar.length === 0 && garlic.length === 0 && grapes.length === 0) {
lost = true; // Trigger "You Lost!" if all poisonous foods are collected
}
}
function displayGameOver() {
// Display a "Game Over" message
fill('rgb(117,164,117)');
textSize(60);
textAlign(CENTER, CENTER);
text("Game Over!", width / 2, height / 2 - 50);
textSize(30);
text(`Final Score: ${score}`, width / 2, height / 2 + 50);
}
function displayYouLost() {
// Display a "You Lost!" message
fill('rgb(117,164,117)');
textSize(60);
textAlign(CENTER, CENTER);
text("You Lost! 😔", width / 2, height / 2 - 50);
textSize(30);
text(`Final Score: ${score}`, width / 2, height / 2 + 50);
}
//Collections
// The scoring of each food group is based upon the size of the food. Ex: Bigger foods have a higher value.
// Good Foods
function collectsushi(p, su) {
meowSound.play();
su.remove();
score += 2;
}
function collectshrimp(p, sh) {
meowSound.play();
sh.remove();
score += 2;
}
function collectwaffle(p, w) {
meowSound.play();
w.remove();
score += 5;
}
function collectbagel(p, b) {
meowSound.play();
b.remove();
score += 4;
}
function collectfries(p, f) {
meowSound.play();
f.remove();
score += 4;
}
function collectbanana(p, ban) {
meowSound.play();
ban.remove();
score += 3;
}
function collectdumpling(p, d) {
meowSound.play();
d.remove();
score += 2;
}
// Poisonous Foods
function collectchocolateBar(p, c) {
angrycatSound.play();
c.remove();
score -= 5;
}
function collectgarlic(p, g) {
angrycatSound.play();
g.remove();
score -= 5;
}
function collectgrapes(p, gr) {
angrycatSound.play();
gr.remove();
score -= 5;
}
function handleCatMovement() {
// I created this section instead of the one above to disable the cat from moving when ended.
cat.speed = 5;
if (kb.pressing('up')) {
cat.direction = -90;
} else if (kb.pressing('down')) {
cat.direction = 90;
} else if (kb.pressing('left')) {
cat.direction = 180;
} else if (kb.pressing('right')) {
cat.direction = 0;
} else {
cat.speed = 0;
}
if (gameOver || lost) {
cat.remove();
}
}