xxxxxxxxxx
347
let gridStartX;
let gridStartY;
let backgroundImage;
let instruction;
let sound;
let noSound;
let fishIcon;
let winningSound;
let playedWinningSound = false;
let losingSound;
let winningSign;
let losingSign;
let cols = 14;
let rows = 8;
let sandboxes = [];
let shells = [];
let fishes = [];
let sandboxWidth;
let sandboxHeight;
let padding = 5;
let stitch;
let stitchX, stitchY;
let pixelFont;
let initialX = 70;
let initialY = 105;
let huntTimerValue = 240; // 4 minutes in seconds
let isHuntTimerActive = false;
let showLosingSign = false;
let losingSignTimer = 0;
let losingSignDuration = 10;
let isMusicPlaying = true;
let myColor;
let timerValue = 10; // Initial 10-second timer
let canMove = false;
let numShells = 6;
let shellPositions = [];
let fishPositions = [];
let shellCounter = 0;
let popwindow;
let showHowToPlay = false;
let hasPressedSpace = false;
function preload() {
backgroundImage = loadImage("background.jpg");
shellicon = loadImage("shell.jpeg");
noSound = loadImage("nomusic.png");
fishIcon = loadImage("fish.png");
stitch = loadImage("stitchpixel.png");
instruction = loadImage("instruct.png");
sound = loadImage("sound.png");
popwindow = loadImage("rules.png");
pixelFont = loadFont("PixelifySans.ttf");
music = loadSound("music.mp3");
winningSound = loadSound("win.mp3");
losingSound = loadSound("lost.mp3");
winningSign = loadImage("yoohoobutton.png");
losingSign = loadImage("sorrybutton.png");
}
function setup() {
createCanvas(800, 600);
pixelDensity(1);
music.loop();
imageMode(CENTER);
// SANDBOX GRID
let gridWidth = 630 - (cols - 1) * padding;
let gridHeight = 360 - (rows - 1) * padding;
sandboxWidth = gridWidth / cols;
sandboxHeight = gridHeight / rows;
gridStartX = width / 2 - 630 / 2 + 45;
gridStartY = height / 2 - 360 / 2;
stitchX = initialX;
stitchY = initialY;
for (let i = 0; i < cols; i++) {
sandboxes[i] = [];
for (let j = 0; j < rows; j++) {
let x = gridStartX + i * (sandboxWidth + padding);
let y = gridStartY + j * (sandboxHeight + padding);
sandboxes[i][j] = new Sandbox(x, y, sandboxWidth, sandboxHeight);
shellPositions.push({ x, y });
fishPositions.push({ x, y });
}
}
generateShells();
generateFishes();
}
function draw() {
push();
print(mouseX, mouseY);
imageMode(CORNER);
background(backgroundImage);
pop();
myColor = color(40, 91, 110);
// THE BACKGROUND RECTANGLE
rectMode(CENTER);
fill(246, 225, 169);
noStroke();
rect(width / 2, height / 2 - 25, 720, 420, 25, 25, 25, 25);
// THE BUTTONS
fill(myColor);
rect(width / 2 - 230, height / 2 + 210, 150, 40, 10, 10, 10, 10);
rect(width / 2 + 155, height / 2 + 210, 200, 40, 10, 10, 10, 10);
fill(255);
textFont(pixelFont);
textSize(20);
text("how to play", width / 2 - 290, height / 2 + 215);
text("shells collected: " + shellCounter, width / 2 + 70, height / 2 + 215);
if (isMusicPlaying) {
image(sound, 700, 510);
} else {
image(noSound, 700, 510);
}
// ICONS
sound.resize(35, 35);
noSound.resize(35, 35);
instruction.resize(35, 35);
image(instruction, 70, 510);
// MAIN TIMER (INITIAL TIMER)
if (timerValue > 0) {
fill(myColor);
text("0:" + (timerValue >= 10 ? timerValue : "0" + timerValue), 60, 435);
}
// HUNT TIMER (4 MINUTES) WITH "HUNT!" TEXT DISPLAY
if (timerValue === 0 && !isHuntTimerActive) {
isHuntTimerActive = true;
canMove = true;
}
if (isHuntTimerActive && huntTimerValue > 0) {
let minutes = floor(huntTimerValue / 60);
let seconds = huntTimerValue % 60;
fill(myColor);
text("hunt!", 55, 415);
text(minutes + ":" + (seconds < 10 ? "0" + seconds : seconds), 60, 435);
}
// Check if hunt timer has reached 0:00
if (isHuntTimerActive && huntTimerValue === 0) {
stitchX = initialX;
stitchY = initialY;
showLosingSign = true;
losingSignTimer = losingSignDuration;
isHuntTimerActive = false; // Stop the hunt timer
canMove = false; // Disable Stitch movement when timer hits 0
}
// Display sandboxes and other elements based on the timer value
displayGameElements();
// STITCH CHARACTER MOVEMENT
handleStitchMovement();
let rectLeft = width / 2 - 720 / 2;
let rectRight = width / 2 + 720 / 2;
let rectTop = height / 2 - 420 / 2 - 40;
let rectBottom = height / 2 + 420 / 2 - 40;
stitchX = constrain(stitchX, rectLeft + 30, rectRight - 55);
stitchY = constrain(stitchY, rectTop + 55, rectBottom - 45);
// DISPLAY LOSING SIGN
if (showLosingSign) {
losingSign.resize(200, 100);
image(losingSign, width / 2, height / 2); // Display the losing sign in the center
losingSignTimer--;
if (losingSignTimer <= 0) {
showLosingSign = false; // Hide losing sign after 10 seconds
}
}
checkWinCondition();
}
function displayGameElements() {
if (timerValue > 0) {
// Display sandboxes, fish, and shells
displaySandboxes();
displayFishes();
displayShells();
} else {
// Display fish first (behind sandboxes) and then sandboxes and shells
displayFishes();
displaySandboxes();
displayShells();
}
}
function displaySandboxes() {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
sandboxes[i][j].display();
}
}
}
function displayFishes() {
for (let i = 0; i < fishes.length; i++) {
fishes[i].display();
checkFishCollision(fishes[i], i);
}
}
function displayShells() {
for (let i = 0; i < shells.length; i++) {
shells[i].display();
checkShellCollision(shells[i], i);
}
}
function handleStitchMovement() {
if (canMove) {
stitch.resize(30, 30);
push();
imageMode(CORNER);
image(stitch, stitchX, stitchY, 30, 30);
pop();
} else {
stitch.resize(30, 30);
push();
imageMode(CORNER);
image(stitch, stitchX, stitchY, 30, 30);
pop();
}
}
function keyPressed() {
if (key === " ") {
// Reset Stitch's position to the initial X and Y coordinates
stitchX = initialX;
stitchY = initialY;
// Reset the 10-second timer
timerValue = 10;
isHuntTimerActive = false; // Stop the hunt timer
huntTimerValue = 240; // Reset the 4-minute timer value
// Reset shell counter and regenerate shells and fish
shellCounter = 0; // Reset the shell counter
generateShells(); // Generate new shells
generateFishes(); // Generate new fishes
playedWinningSound = false; // Reset the winning sound flag
canMove = false; // Disable movement until the timer reaches 0
}
if (keyCode === UP_ARROW) {
stitchY -= 5;
}
if (keyCode === DOWN_ARROW) {
stitchY += 5;
}
if (keyCode === LEFT_ARROW) {
stitchX -= 5;
}
if (keyCode === RIGHT_ARROW) {
stitchX += 5;
}
}
function checkFishCollision(fish, index) {
let d = dist(stitchX, stitchY, fish.x, fish.y);
if (d < 30) {
// Reset the Stitch's position
stitchX = initialX;
stitchY = initialY;
// Reset the 10-second timer
timerValue = 10;
isHuntTimerActive = false; // Stop the hunt timer
huntTimerValue = 240; // Reset the 4-minute timer value
// Reset shell counter and regenerate shells and fish
shellCounter = 0; // Reset the shell counter
generateShells(); // Generate new shells
generateFishes(); // Generate new fishes
canMove = false; // Disable movement until the timer reaches 0
}
}
function checkShellCollision(shell, index) {
let d = dist(stitchX, stitchY, shell.x, shell.y);
if (d < 30) {
shells.splice(index, 1); // Remove the collected shell
shellCounter++; // Increase shell counter
}
}
function generateShells() {
shells = [];
for (let i = 0; i < numShells; i++) {
let randomPos = random(shellPositions);
shells.push(new Shell(randomPos.x, randomPos.y, sandboxWidth, sandboxHeight));
}
}
function generateFishes() {
fishes = [];
for (let i = 0; i < 3; i++) {
let randomPos = random(fishPositions);
fishes.push(new Fish(randomPos.x, randomPos.y, sandboxWidth, sandboxHeight));
}
}
function checkWinCondition() {
if (shellCounter === numShells && huntTimerValue > 0 && !playedWinningSound) {
canMove = false; // Disable movement when winning sound plays
winningSign.resize(200, 100);
image(winningSign, width / 2, height / 2);
winningSound.play();
playedWinningSound = true;
}
}
class Shell {
constructor(x, y, w, h) {
this.x = x + w / 2;
this.y = y + h / 2;
}
display() {
image(shellicon, this.x, this.y, 20, 20);
}
}
class Fish {
constructor(x, y, w, h) {
this.x = x + w / 2;
this.y = y + h / 2;
}
display() {
image(fishIcon, this.x, this.y, 40, 30);
}
}