xxxxxxxxxx
287
class Shell {
constructor(x, y, w, h, shellicon) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.img = shellicon;
this.isCollected = false;
}
display() {
if (!this.isCollected) { // Only display if not collected
image(this.img, this.x, this.y, this.w, this.h);
}
}
}
class Fish {
constructor(x, y, w, h, fishicon) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.img = fishicon;
}
display() {
image(this.img, this.x, this.y, this.w, this.h);
}
}
let backgroundImage;
let instruction;
let sound;
let fishIcon;
let winningSound;
let losingSound;
let cols = 16;
let rows = 9;
let sandboxes = [];
let shells = [];
let fishes = [];
let sandboxWidth;
let sandboxHeight;
let padding = 5;
let stitch;
let stitchX, stitchY;
let pixelFont;
let initialX = 30;
let initialY = 80;
let isMusicPlaying = true;
let myColor;
var timerValue = 10;
let canMove = false;
let numShells = 6;
let shellPositions = [];
let fishPositions = [];
let shellCounter = 0;
let popwindow;
let showHowToPlay = false;
function preload() {
backgroundImage = loadImage("background.jpg");
shellicon = loadImage("shell.jpeg");
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");
}
function setup() {
createCanvas(800, 600);
pixelDensity(1);
music.loop();
// SANDBOX GRID //
let gridWidth = 720 - (cols - 1) * padding;
let gridHeight = 420 - (rows - 1) * padding;
sandboxWidth = gridWidth / cols;
sandboxHeight = gridHeight / rows;
let gridStartX = width / 2 - 720 / 2;
let gridStartY = height / 2 - 420 / 2 - 40;
stitchX = gridStartX + sandboxWidth - 10;
stitchY = gridStartY + sandboxHeight - 10;
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() {
background(backgroundImage);
myColor = color(40, 91, 110);
// THE BACKGROUND RECTANGLE //
rectMode(CENTER);
fill(246, 225, 169);
noStroke();
rect(width / 2, height / 2 - 40, 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);
// ICONS //
sound.resize(35, 35);
image(sound, 700, 492);
instruction.resize(35, 35);
image(instruction, 50, 492);
// TIMER //
if (timerValue >= 10) {
fill(myColor);
text("0:" + timerValue, 60, 435);
} else if (timerValue < 10) {
fill(myColor);
text("0:0" + timerValue, 60, 435);
}
if (timerValue == 0) {
fill(myColor);
text("hunt!", 55, 415);
canMove = true; // Allow movement after timer ends
}
for (let i = 2; i < cols; i++) {
for (let j = 1; j < rows; j++) {
sandboxes[i][j].display();
}
}
for (let i = 0; i < shells.length; i++) {
shells[i].display();
checkShellCollision(shells[i], i);
}
for (let i = 0; i < fishes.length; i++) {
fishes[i].display();
checkFishCollision(fishes[i], i); // Check for fish collision
}
// STITCH CHARACTER //
stitch.resize(30, 30);
image(stitch, stitchX, stitchY, 30, 30);
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 + 30, rectBottom - 55);
// SOUND EFFECTS //
checkWinCondition();
checkWinCondition();
//POP-UP INSTRUCTIONS//
if (showHowToPlay) {
push();
background(backgroundImage);
imageMode(CENTER);
image(popwindow, width/2,height/2,630,420);
instruction.resize(35, 35);
image(instruction, 50, 492);
pop();
}
}
function keyPressed() {
if (canMove) {
if (keyCode === UP_ARROW) {
stitchY -= sandboxHeight + padding;
} else if (keyCode === DOWN_ARROW) {
stitchY += sandboxHeight + padding;
} else if (keyCode === LEFT_ARROW) {
stitchX -= sandboxWidth + padding;
} else if (keyCode === RIGHT_ARROW) {
stitchX += sandboxWidth + padding;
}
}
if (key === ' ') {
generateShells();
shellCounter = 0;
}
}
setInterval(timeIt, 1500);
function timeIt() {
if (timerValue > 0) {
timerValue--;
}
}
// SHELL RANDOM GENERATION //
function generateShells() {
shells = [];
let shuffledPositions = shuffle(shellPositions);
for (let i = 0; i < numShells; i++) {
let pos = shuffledPositions[i];
shells.push(new Shell(pos.x, pos.y, sandboxWidth, sandboxHeight, shellicon));
}
}
// FISH BONE RANDOM GENERATION //
function generateFishes() {
fishes = [];
let shuffledPositions = shuffle(fishPositions);
for (let i = 0; i < 3; i++) {
let pos = shuffledPositions[i];
fishes.push(new Fish(pos.x, pos.y, sandboxWidth, sandboxHeight, fishIcon));
}
}
// COLLECTING & COUNTING THE SHELLS //
function checkShellCollision(shell, index) {
let d = dist(stitchX, stitchY, shell.x, shell.y);
if (d < 30 && !shell.isCollected) {
shell.isCollected = true;
shellCounter++;
}
}
// SOUND EFFECTS //
function checkFishCollision(fish, index) {
let d = dist(stitchX, stitchY, fish.x, fish.y);
if (d < 30) {
losingSound.play();
stitchX = initialX;
stitchY = initialY;
}
}
function checkWinCondition() {
let rectBottomX = 720;
let rectBottomY = 430;
print(mouseX,mouseY);
if (shellCounter == numShells && stitchX >= rectBottomX && stitchY >= rectBottomY) {
winningSound.play();
}
}
function mousePressed() {
if (mouseX > 50 && mouseX < 50 + 35 && mouseY > 492 && mouseY < 492 + 35) {
showHowToPlay = !showHowToPlay;
}
}