xxxxxxxxxx
338
// SCENE 3
// THE GAME OBJECTS
// ---santa class---
class Santa {
constructor() {
this.x = width / 2;
this.y = height - 100;
this.w = santaWidth;
this.h = santaHeight;
this.moving = false;
this.idleFrame = 2;
this.speed = santaSpeed;
this.facingRight = true;
this.health = santaHealth;
}
move() {
if (keyIsDown(LEFT_ARROW)) {
this.x -= this.speed;
this.moving = true;
this.facingRight = false;
} else if (keyIsDown(RIGHT_ARROW)) {
this.x += this.speed;
this.moving = true;
this.facingRight = true;
} else {
this.moving = false;
}
// constrain to the size of canvas
this.x = constrain(this.x, 0, width - this.w);
// itirating through the sprite
if (this.moving && frameCount % 8 === 0) {
this.idleFrame = (this.idleFrame + 1) % 3;
} else if (this.moving === false) {
this.idleFrame = 2; // set back to idle when not moving
}
}
display() {
push();
if (!this.facingRight) {
translate(this.x + this.w, this.y); // utilizing translate and scale to mirror the santa image
scale(-1, 1);
image(santaSprite, 0, 0, this.w, this.h, this.idleFrame * santaWidth, 0, santaWidth, santaHeight);
} else {
image(santaSprite, this.x, this.y, this.w, this.h, this.idleFrame * santaWidth, 0, santaWidth, santaHeight);
}
pop();
}
}
// ---santa---
let santa;
let santaWidth = 49;
let santaHeight = 47;
let santaSpeed = 5;
let santaHealth = 3; // healthbar - 3 candies
let santaSprite;
// ---gifts---
let gifts = [];
let giftWidth = 30;
let giftHeight = 27;
let giftFrequency = 100; // how frequent the gifts will spawn
let giftCounter = 0; // counting how many gifts caught
let giftToWin = 15; // winning condition
let giftSprite;
let giftCollectedSound;
// ---candies---
let candySprite;
// ---icicles---
let icicles = [];
let icicleWidth = 30;
let icicleHeight = 50;
let icicleFrequency = 20; // frequency of icicles
let icicleHitSound; // if hits santa
let icicleSprite;
// THE GAME SETTINGS
let gameStarted = false;
let jingleBellsMusic;
let gameOver = false;
let gameOverSoundCharged = true;
let gameOverSound;
let gameWon = false;
let gameWonSoundCharged = true;
let gameWonSound;
function preload() {
jingleBellsMusic = loadSound('jingleBellsMusic.mp3');
santaSprite = loadImage('santaSpriteSheet.png');
giftSprite = loadImage('giftsSpriteSheet.png');
icicleSprite = loadImage('icicleSpriteSheet.png');
candySprite = loadImage('healthbarSpriteSheet.png');
icicleHitSound = loadSound('icicleHitSound.mp3');
giftCollectedSound = loadSound('giftCollectedSound.mp3');
gameOverSound = loadSound('gameOverSound.mp3');
gameWonSound = loadSound('gameWonSound.mp3');
}
function setup() {
jingleBellsMusic.setLoop(true);
jingleBellsMusic.play();
createCanvas(600, 700);
// initialize santa
santa = new Santa();
}
function draw() {
if (!gameStarted) {
displayRulesScreen(); // rules
} else if (gameOver) {
displayGameOverScreen(); //game over
} else if (gameWon) {
displayWinScreen(); // win
} else {
playGame(); // game state
}
}
// defining rules
function displayRulesScreen() {
background('rgb(0,147,255)');
fill('white');
noStroke();
rect(0, 600 + santaHeight, 600, 100);
textAlign(CENTER, CENTER);
fill(255);
textSize(48);
text("HELP THE SANTA!", 300, 100);
textSize(35);
fill('red');
text("Rules:", 300, 250);
textSize(18);
fill('white');
text("Christmas is coming! Your goal is simple - collect 15 gifts", 300, 300);
text("Beware of icicles that are flying from the sky!", 300, 350);
text("Use LEFT and RIGHT arrow keys to move Santa and dodge icicles", 300, 400);
text("You have only 3 candy-health-points. Once they're gone, you lose", 300, 450);
textSize(28);
text("Press ENTER to Start", 300, 550);
}
// game state
function playGame() {
background(0, 147, 255);
fill('white');
noStroke();
rect(0, 600 + santaHeight, 600, 100);
// gift counter
fill(255);
textSize(32);
text("Gifts: " + giftCounter, 65, 35);
// displaying candies as health
displayHealth();
// launch santa
santa.move();
santa.display();
// launch gifts
if (frameCount % giftFrequency === 0) {
gifts.push(createGift());
}
// launch icicles
if (frameCount % icicleFrequency === 0) {
icicles.push(createIcicle());
}
// updating gifts state
for (let i = gifts.length - 1; i >= 0; i--) {
let gift = gifts[i];
gift.y += gift.speed;
// sprite for gift
image(giftSprite, gift.x, gift.y, giftWidth, giftHeight, gift.frame * giftWidth, 0, giftWidth, giftHeight);
// collision detection for gifts
if (checkCollision(gift.x, gift.y, giftWidth, giftHeight)) {
giftCollectedSound.play();
gifts.splice(i, 1); // Remove the gift
giftCounter++; // Increment the gift counter
// winning condition
if (giftCounter >= giftToWin) {
gameWon = true;
if (gameWonSoundCharged) {
gameWonSound.play();
gameWonSoundCharged = false;
}
}
}
// deleting the gifts that are out of the borders
if (gift.y > 600 + santaHeight - giftHeight) {
gifts.splice(i, 1);
}
}
// updating icicles
for (let i = icicles.length - 1; i >= 0; i--) {
let icicle = icicles[i];
icicle.y += icicle.speed;
// sprite for icicles
image(icicleSprite, icicle.x, icicle.y, icicleWidth, icicleHeight);
// collision detection for icicles
if (checkCollision(icicle.x, icicle.y, icicleWidth, icicleHeight)) {
icicleHitSound.play();
icicles.splice(i, 1); // removing from the array
santa.health -= 1; // decreasing health by 1
if (santa.health <= 0) {
gameOver = true; // game over condition
if (gameOverSoundCharged) {
gameOverSound.play();
gameOverSoundCharged = false; // setting to false to not repeat infinitely many times
}
}
}
// deleting icicles if out of border
if (icicle.y > 600 + santaHeight - icicleHeight) {
icicles.splice(i, 1);
}
}
}
// game over screen function
function displayGameOverScreen() {
jingleBellsMusic.stop();
background(0, 147, 255); // Blue sky
fill('white');
noStroke();
rect(0, 600 + santaHeight, 600, 100);
textAlign(CENTER, CENTER);
fill(255);
textSize(70);
text("GAME OVER", 300, 350);
textSize(35);
text("Press Enter to Restart", 300, 450);
}
// win screen function
function displayWinScreen() {
jingleBellsMusic.stop();
background(0, 147, 255);
fill('white');
noStroke();
rect(0, 600 + santaHeight, 600, 100);
textAlign(CENTER, CENTER);
fill(255);
textSize(70);
text("YOU WON!", 300, 350);
textSize(35);
text("Press Enter to Restart", 300, 450);
}
// simple collision detection with santa being inside the box with boundaries
function checkCollision(objX, objY, objW, objH) {
return objY + objH > santa.y && objX + objW > santa.x && objX < santa.x + santa.w;
}
// health as candies - one sprite that contains three of them, very simple
function displayHealth() {
let candyX = width - 190;
let candyWidth = 51;
let candyHeight = 41;
for (let i = 0; i < santa.health; i++) {
image(candySprite, candyX + i * (candyWidth + 10), 10, candyWidth, candyHeight, i * candyWidth, 0, candyWidth, candyHeight);
}
}
// gift creator
function createGift() {
return {
x: random(giftWidth, width - giftWidth),
y: 0,
speed: random(2, 4),
frame: floor(random(0, 3)) // randomly select the gift sprite part - three colors available
};
}
// icicle creator
function createIcicle() {
return {
x: random(icicleWidth / 2, width - icicleWidth / 2),
y: 0,
speed: random(3, 5)
};
}
// restarting the game by resetting all the variables to their initial states
function restartGame() {
gameOver = false;
gameWon = false;
santa.health = 3; // Reset health
giftCounter = 0; // Reset gift counter
gifts = []; // Clear gifts
icicles = []; // Clear icicles
santa.idleFrame = 2; // Reset to idle frame
gameOverSoundCharged = true;
gameWonSoundCharged = true;
jingleBellsMusic.setLoop(true);
jingleBellsMusic.play();
}
// using ENTER for starting and restarting the game
function keyPressed() {
if (!gameStarted && keyCode === ENTER) {
gameStarted = true; // game start
} else if (keyCode === ENTER && (gameOver || gameWon)) {
restartGame(); // restart
}
// fulscreen
if (key === 'f' || key === 'F') {
let fs = fullscreen();
fullscreen(!fs);
}
}