xxxxxxxxxx
298
let gameState = "start"; // can be 'start', 'playing', 'end'
let cellWidth = 35;
let themeSong, interSong;
let groundTexture, bushTexture, catImg, bearImg;
let replayBtn, menuBtn, playBtn;
let cells = [];
let confetti = [];
let catSprite = [];
let bearSprite = [];
let oldladySprite = [];
let game;
function preload() {
// SOUNDS
themeSong = loadSound("/assets/sounds/theme.mp3");
interSong = loadSound("/assets/sounds/inter.mp3");
// IMAGES
groundTexture = loadImage("/assets/images/ground2.png");
bushTexture = loadImage("/assets/images/bushes2.png");
catImg = loadImage("/assets/images/cat.png");
bearImg = loadImage("/assets/images/bear.jpeg");
oldladyImg = loadImage("/assets/images/oldlady.png");
}
let catSW;
let catSH;
let bearSW;
let bearSH;
let oldladySW;
let oldladySH;
let curSong = 0;
function setup() {
createCanvas(980, 740);
textFont("retroGaming");
// CREATE BUTTONS
replayBtn = createButton("PLAY AGAIN");
menuBtn = createButton("MAIN MENU");
playBtn = createButton("START");
// HIDE BUTTONS
playBtn.hide();
menuBtn.hide();
replayBtn.hide();
catSW = int(catImg.width / 4);
catSH = int(catImg.height / 4);
bearSW = int(bearImg.width / 4);
bearSH = int(bearImg.height / 4);
oldladySW = int(oldladyImg.width / 6);
oldladySH = int(oldladyImg.height / 1);
game = new Game();
interSong.loop();
}
function draw() {
background(0);
if (game.gameState == "start") {
// check if other sound is playing
// play sound
if (interSong.isPlaying() && curSong == 0) {
interSong.stop();
interSong.loop();
curSong = 1;
}
// show instructions screen
drawInstructions();
} else if (game.gameState == "playing") {
// check if other sound is playing
// play sound
if (interSong.isPlaying() && (curSong == 1 || curSong == 0)) {
interSong.stop();
themeSong.loop();
themeSong.setVolume(0.1);
curSong = 2;
}
// show game screen
game.show();
} else if (game.gameState == "end") {
// check if other sound is playing
// play sound
if (themeSong.isPlaying() && curSong == 2) {
themeSong.stop();
interSong.loop();
curSong = 0;
}
// show end screen
drawEndScreen();
}
}
// START SCREEN
function drawInstructions() {
// STRINGS
let gameTitle = "GRANNYS LOVELY MAZE";
let description =
"Kitty is a royal cat who enjoys spending time in the outdoors maze of his castle. He needs to find way back to his grandma before bear turns him into a dinner.";
let instructionsA =
"Use WASD keys to help Kitty navigate through dark maze and get to the grandma.";
let instructionsB =
"Use ARROW keys to help Bear have a dinner for the first time in a week.";
// BIG BOX
fill(41, 57, 18);
rect(102.5, 210, 320, 380); // left
rect(540, 210, 320, 380); // right
// OUTLINE SMALL BOX
stroke(37, 48, 0);
strokeWeight(5);
rect(120, 295, 285, 150); // left
rect(557.5, 295, 285, 150); // right
// GROUND TEXTURE
noStroke();
// left
for (let y = 0; y < 140; y += 10) {
for (let x = 0; x < 270; x += 10) {
image(groundTexture, 122.5 + x, 295 + y, 20, 20);
}
}
// right
for (let y = 0; y < 140; y += 10) {
for (let x = 0; x < 270; x += 10) {
image(groundTexture, 560 + x, 295 + y, 20, 20);
}
}
// VOLUME LINE
stroke(37, 48, 0);
strokeWeight(5);
strokeCap(SQUARE);
line(102.5, 210, 422.5, 210); // left
line(540, 210, 860, 210); // right
// TEXT DISPLAY
// title style
textAlign(LEFT);
fill(255);
textSize(32);
stroke(192, 194, 31);
strokeWeight(3);
text(gameTitle, width / 4, 100);
// description style
noStroke();
textSize(16);
textAlign(CENTER);
text(description, width / 14, 150, 825, 100);
// "player #" style
textSize(32);
textAlign(LEFT);
text("PLAYER 1", 180, 265);
text("PLAYER 2", 610, 275);
// instructions style
textSize(16);
textAlign(CENTER);
text(instructionsA, 110, 490, 300, 500);
text(instructionsB, 555, 490, 290, 500);
// IMAGES DISPLAY
image(catImg, 190, 340, 72, 72, 0, 0, 32, 32);
image(oldladyImg, 260, 325, 72, 90, 0, 0, 40, 54);
image(bearImg, 665, 335, 72, 72, 0, 0, bearSW, bearSW);
// PLAY BUTTON DISPLAY
playBtn.show();
playBtn.position(width / 2.4175, height / 2 + 260);
playBtn.mousePressed(restart);
playBtn.style("font-size", "24px");
}
// END SCREEN
function drawEndScreen() {
// STRINGS
let player1Win = "GOOD JOB, KITTY!";
let player2Loss = "BEAR WILL GO TO SLEEP HUNGRY TODAY :<";
let player2Win = "HAVE A NICE MEAL, BEAR!";
let player1Loss = "BETTER LUCK IN NEXT LIFE, KITTY ;<";
fill(255);
textAlign(LEFT);
if (game.scores[0] > game.scores[1]) {
textSize(32);
text(player1Win, width / 3, height / 2 - 50);
textSize(16);
text(player2Loss, width / 3.25, height / 2);
} else if (game.scores[0] < game.scores[1]) {
textSize(32);
text(player2Win, width / 4, height / 2 - 50);
textSize(16);
text(player1Loss, width / 3.15, height / 2);
}
// BUTTON DISPLAY
// replay button
replayBtn.show();
replayBtn.position(width / 2 - 175, height / 2 + 150);
replayBtn.mousePressed(restart);
// menu button
menuBtn.show();
menuBtn.position(width / 2 + 25, height / 2 + 150);
menuBtn.mousePressed(viewMenu);
// UPDATE AND DISPLAY CONFETTI
if (frameCount % 10 == 0) {
let c = new Confetti();
confetti.push(c);
}
for (let i = confetti.length - 1; i >= 0; i--) {
confetti[i].update();
confetti[i].display();
if (confetti[i].isOffScreen()) {
confetti.splice(i, 1);
}
}
}
// START GAME ON BUTTON PRESS
// called by 'start' and 'replay' buttons
function restart() {
game = new Game();
game.gameState = "playing";
// hide buttons
replayBtn.hide();
menuBtn.hide();
playBtn.hide();
}
// VIEW MAIN MENU ON BUTTON PRESS
// called by 'menu' button
function viewMenu() {
game.gameState = "start";
// hide end screen buttons
replayBtn.hide();
menuBtn.hide();
}
let x1, x2, y1, y2, curMap;
// PLAYERS CONTROLS
// work only during the game
function keyPressed() {
x1 = game.players[0].x;
y1 = game.players[0].y;
x2 = game.players[1].x;
y2 = game.players[1].y;
curMap = game.maps[game.currentMap];
// PLAYER1 CONTROLS
if (game.gameState == "playing") {
if ((key == "W" || key == "w") && y1 > 0) {
if (!curMap[y1 - 1][x1]) {
game.players[0].y--;
game.players[0].sprite = 2;
}
} else if ((key == "A" || key == "a") && x1 > 0) {
if (!curMap[y1][x1 - 1]) {
game.players[0].x--;
game.players[0].sprite = 3;
}
} else if ((key == "S" || key == "s") && y1 < curMap.length - 1) {
if (!curMap[y1 + 1][x1]) {
game.players[0].y++;
game.players[0].sprite = 0;
}
} else if ((key == "D" || key == "d") && x1 < curMap[0].length - 1) {
if (!curMap[y1][x1 + 1]) {
game.players[0].x++;
game.players[0].sprite = 1;
}
}
// PLAYER2 CONTROLS
else if (keyCode == UP_ARROW && y2 > 0) {
if (!curMap[y2 - 1][x2]) {
game.players[1].y--;
game.players[1].sprite = 3;
}
} else if (keyCode == LEFT_ARROW && x2 > 0) {
if (!curMap[y2][x2 - 1]) {
game.players[1].x--;
game.players[1].sprite = 1;
}
} else if (keyCode == DOWN_ARROW && y2 < curMap.length - 1) {
if (!curMap[y2 + 1][x2]) {
game.players[1].y++;
game.players[1].sprite = 0;
}
} else if (keyCode == RIGHT_ARROW && x2 < curMap[0].length - 1) {
if (!curMap[y2][x2 + 1]) {
game.players[1].x++;
game.players[1].sprite = 2;
}
}
}
}