xxxxxxxxxx
218
// This code was created with the help of AI
// Global variables
let player;
let charIdleSprite, charMovingSprite, fishSprite;
let titleSprite, startSprite, moveSprite, life3Sprite, life2Sprite, life1Sprite;
let fishes = [];
let waveOffset = 0;
let gameStarted = false; // Track if the game has started
let lives = 3; // Player starts with 3 lives
let bgMusic, impactSound; // Sound variables
function preload() {
// Load game sprites
charIdleSprite = loadImage("Sprites/MAN_IDLE.gif", () => console.log("MAN_IDLE.gif loaded"));
charMovingSprite = loadImage("Sprites/MAN_MOVING.gif", () => console.log("MAN_MOVING.gif loaded"));
fishSprite = loadImage("Sprites/FISHES.gif", () => console.log("FISHES.gif loaded"));
// Load first page sprites
titleSprite = loadImage("Sprites/TITLE.gif", () => console.log("TITLE.gif loaded"));
startSprite = loadImage("Sprites/START.gif", () => console.log("START.gif loaded"));
moveSprite = loadImage("Sprites/MOVE.gif", () => console.log("MOVE.gif loaded"));
// Load life sprites
life3Sprite = loadImage("Sprites/LIFE3.gif", () => console.log("LIFE3.gif loaded"));
life2Sprite = loadImage("Sprites/LIFE2.gif", () => console.log("LIFE2.gif loaded"));
life1Sprite = loadImage("Sprites/LIFE1.gif", () => console.log("LIFE1.gif loaded"));
// Load audio files
bgMusic = loadSound("Audio/BACKGROUNDMUSIC.m4a", () => console.log("Background music loaded"));
impactSound = loadSound("Audio/IMPACT.flac", () => console.log("Impact sound loaded"));
}
function setup() {
createCanvas(400, 400);
player = new Player(); // Initialize player object
// Initialize 3 fish objects
for (let i = 0; i < 3; i++) {
fishes.push(new Fish());
}
// Start background music
bgMusic.loop();
bgMusic.setVolume(0.5); // Adjust volume if necessary
}
function draw() {
if (!gameStarted) {
drawStartPage(); // Display the start page if the game hasn't started
} else {
drawGame(); // Display the main game
}
}
function drawStartPage() {
background(173, 216, 230); // Light blue background
drawSmallWaves(); // Draw waves
// Display title and instructions
image(titleSprite, 20, 50, 400, 300); // Title image
image(startSprite, 150, 200, 100, 100); // Start button
image(moveSprite, 150, 300, 100, 100); // Move instructions (width reduced to half)
}
function drawGame() {
background(173, 216, 230); // Light blue background
drawSmallWaves(); // Draw waves
// Update and display player
player.move();
player.display();
// Update and display each fish
for (let fish of fishes) {
fish.update();
fish.display();
// Check if the player collides with the fish
if (collidesWithFish(player, fish)) {
lives--; // Player loses a life
impactSound.play(); // Play impact sound
fish.y = 0; // Reset fish to top of screen
fish.x = random(0, width); // Randomize fish's x position
}
}
// Display the player's lives
displayLives();
// Check if the game is over
if (lives <= 0) {
gameStarted = false; // End the game and show the start page
lives = 3; // Reset lives
}
}
function mousePressed() {
if (!gameStarted) {
// Check if the start button is clicked
if (mouseX > 150 && mouseX < 250 && mouseY > 200 && mouseY < 250) {
gameStarted = true; // Start the game
}
} else {
// Save an image of the canvas during the game
save("character.jpg");
}
}
function drawSmallWaves() {
noStroke();
fill(135, 206, 250, 150);
let waveHeight = 5;
let waveFrequency = 0.1;
let waveSpeed = 0.2;
waveOffset -= waveSpeed;
for (let y = 0; y < height; y += 20) {
beginShape();
for (let x = 0; x <= width; x++) {
let wave = sin((x * waveFrequency) + waveOffset) * waveHeight;
vertex(x, y + wave);
}
vertex(width, height);
vertex(0, height);
endShape(CLOSE);
}
}
// Function to check if the player collides with a fish at the center of the GIF
function collidesWithFish(player, fish) {
// Get the center of the player's character GIF
let playerCenterX = player.x + 150; // The center of the character (half of the width of the sprite)
let playerCenterY = player.y + 100; // The center of the character (half of the height of the sprite)
// Get the center of the fish
let fishCenterX = fish.x + 50; // The center of the fish (half of the width of the fish sprite)
let fishCenterY = fish.y + 50; // The center of the fish (half of the height of the fish sprite)
// Check if the centers are close enough to trigger the loss of a life
let distance = dist(playerCenterX, playerCenterY, fishCenterX, fishCenterY);
// Only lose a life if the distance between the centers is small enough
if (distance < 50) { // You can adjust the value 50 based on the size of your character or fish
return true;
}
return false;
}
// Display the player's lives in the top left corner
function displayLives() {
let lifeX = 20; // X position for lives
let lifeY = 20; // Y position for lives
if (lives === 3) {
image(life3Sprite, lifeX, lifeY, 50, 50); // Display 3 lives
} else if (lives === 2) {
image(life2Sprite, lifeX, lifeY, 50, 50); // Display 2 lives
} else if (lives === 1) {
image(life1Sprite, lifeX, lifeY, 50, 50); // Display 1 life
}
}
class Player {
constructor() {
this.x = 100;
this.y = 250;
this.animationState = 0; // 0 = idle, 1 = moving
}
move() {
if (keyIsDown(RIGHT_ARROW)) {
this.x += 5;
this.animationState = 1;
} else if (keyIsDown(LEFT_ARROW)) {
this.x -= 5;
this.animationState = 1;
} else {
this.animationState = 0;
}
}
display() {
let spriteWidth = 300;
let spriteHeight = 200;
if (this.animationState === 0) {
image(charIdleSprite, this.x, this.y, spriteWidth, spriteHeight);
} else {
image(charMovingSprite, this.x, this.y, spriteWidth, spriteHeight);
}
}
}
class Fish {
constructor() {
this.x = random(0, width);
this.y = 0;
this.speed = random(2, 5);
}
update() {
this.y += this.speed;
if (this.y > height) {
this.y = 0;
this.x = random(0, width);
}
}
display() {
image(fishSprite, this.x, this.y, 100, 100);
}
}