xxxxxxxxxx
// Variables
let startButton; //start button of the game
let character; //main character
let projectiles = [];
let gameStarted = false;
let gameOver = false; //end of the game
let projectileSpeed = 10; //speed adjustment
let batSpeed = 5; //speed of bats
let numProjectiles = 30; //number of bats
let startTime;
let elapsedTime;
let img1, img2, img3, img4, img5, imgLife; //images used in the game
let ballsOut = 0;
let song; // bloody tears, main theme song of castlevania
let health = 3; //total health count
let lifeImages = [];
// Preload
function preload() {
startButton = loadImage('StartButton.png'); //start button
img1 = loadImage('Background.png'); //background of the game
img2 = loadImage('GameOver.png'); //game over screen
imgLife = loadImage('life.png'); //life heart image
img3 = loadImage('background2.png'); //background 2
img4 = loadImage('background3.png');//background 3
img5 = loadImage('background4.png');//background 4
song = loadSound('Bloody.Tears.mp3'); //bloody tears
}
// Setup function
function setup() {
createCanvas(800, 800); //size of the canvas
character = new Character(); //create new character with each restart
resetGame(); //reset score, health etc
//delete one life for each hit
for (let i = 0; i < health; i++) {
lifeImages.push(imgLife);
}
}
// Draw function
function draw() {
background(255); //background color
// check if it's time to change the background image
if (ballsOut >= 30) {
img1 = img3;
// change the background image
}
// check if it's time to change the background image
if (ballsOut >= 60) {
img3 = img4;
// Change the background image
}
// check if it's time to change the background image
if (ballsOut >= 120) {
img4 = img5;
// change the background image
}
image(img1, 0, 0, width, height); //background image
if (!gameStarted) {
drawStartScreen(); //start screen
} else if (gameOver) {
drawEndScreen(); //game over screen
} else {
character.move();
character.display();
//count time
if (!startTime) {
startTime = millis();
}
//spawn projectiles
if (frameCount % 60 === 0) {
for (let i = 0; i < numProjectiles; i++) {
projectiles.push(new Projectile(batSpeed));
}
projectileSpeed += 0.1;
//increase the number of projectiles by time
numProjectiles += 0.1;
}
//projectile loop
for (let i = projectiles.length - 1; i >= 0; i--) {
projectiles[i].move();
projectiles[i].display();
//hit check
if (character.hits(projectiles[i])) {
// delete projectile that is hit
projectiles.splice(i, 1);
// reduce health
health--;
// check if health is zero in order to trigger end screen
if (health <= 0) {
gameOver = true; // Trigger game over
}
// if life is more than 3 remove 1
else if (lifeImages.length > 3) {
lifeImages.pop();
}
break; //make sure one life is removed each time
}
// remove projectiles offscreen
if (projectiles[i].offscreen()) {
if (ballsOut >= 30) {
projectiles[i].changeImage();
}
projectiles.splice(i, 1);
ballsOut++;
}
}
//display elements
elapsedTime = millis() - startTime;
displayTimer();
displayBallsOut();
displayHealth();
if (!song.isPlaying()) {
song.play();
}
// start playing the music when the game starts
}
}
// Display timer
function displayTimer() {
let seconds = Math.floor(elapsedTime / 1000);
textAlign(RIGHT);
textSize(16);
fill(255, 214, 10);
text('Time: ' + seconds + 's', width - 10, 20);
}
// Display bats out
function displayBallsOut() {
textAlign(RIGHT);
textSize(16);
fill(255, 214, 10);
text('Bats Dodged: ' + ballsOut, width - 10, 40);
}
// Display health
function displayHealth() {
let maxHealthDisplay = min(3, health);
let posX = 10; // x position of the life image
let posY = 10; // x position of the life image
for (let i = 0; i < maxHealthDisplay; i++) {
image(lifeImages[i], posX, posY, 25, 25); //size
posX += 25; // spacing between life images
}
}
// Key pressed function
function keyPressed() {
if (keyCode === ENTER && !gameStarted) {
gameStarted = true;
} else if (keyCode === 32 && gameOver) {
resetGame();
}
}
// Draw start screen
function drawStartScreen() {
textAlign(CENTER);
textSize(32);
fill(255);
text('Press ENTER to', width / 2, height / 2);
image(startButton, width / 2 - 50, height / 2 + 50, 100, 50);
}
// Draw end screen
function drawEndScreen() {
image(img2, 0, 0, width, height);
text('Time: ' + Math.floor(elapsedTime / 1000) + 's', 700, 75);
text('Bats Dodged: ' + ballsOut, 700, 50);
textSize(20);
text('Press SPACE to play again', 500, 725);
if (song.isPlaying()) {
song.stop();
}
// stop playing the music when the game is over
}
// Reset the game
function resetGame() {
character = new Character();
projectiles = [];
gameOver = false;
projectileSpeed = 1;
startTime = null;
elapsedTime = 0;
ballsOut = 0;
health = 3;
numProjectiles = 0.9;
lifeImages = [];
for (let i = 0; i < health; i++) {
lifeImages.push(imgLife);
}
}