//GOLD RUSH BY RAYA TABASSUM
//CHARACTERS: SUPER ONION BOY, GOLD COIN, GOMBA THE ENEMY
//5 POINTS FOR AVOIDING THE ENEMY, 10 POINTS FOR COLLECTING THE COIN
// Preload function for loading assets before the game starts
let img, game_over_bg, sf, coin_sound, jump_sound, enemy_sound, pixel_font;
let xpos = 0, ypos = 0, x1 = 0, x2 = 1000, y = 0, x3 = 0, y2 = 0, score = 0, win = true;
img = loadImage("plt.png"); // Background image
game_over_bg = loadImage("Gameover.png"); // Game over background image
soundFormats('mp3', 'ogg'); // Specify the sound formats to use
sf = loadSound("backgroundmusic.mp3"); // Background music
coin_sound = loadSound("coin.mp3"); // Sound for collecting coins
jump_sound = loadSound("jumping.mp3"); // Sound for jumping
enemy_sound = loadSound("enemy_sound.mp3"); // Sound when colliding with an enemy
pixel_font = loadFont("PixelFont.ttf"); // Custom font for text display
// Player class with properties and methods for the player character
this.playerYOnGround = 550; // Y position of the player on the ground
this.playerSize = 60; // Size of the player character
this.bgGroundHeight = 45; // Height of the ground
this.animationSlowDown = 8; // Slows down the animation frame rate
this.width = 1000; // Width of the canvas
this.jumpHeight = 0; // Current jump height
this.jumpStrength = 0; // Current strength of the jump
this.jumpStrengthMax = 5; // Maximum strength of the jump
this.gravity = 0.1; // Gravity affecting the jump
this.jumping = false; // Is the player jumping?
this.playerImg = []; // Array to hold player images for animation
this.numberPlayerImg = 6; // Number of player images
this.playerImgIndex = 0; // Current index of the player image
for (let i = 1; i <= 3; i++) {
this.playerImg.push(loadImage(`guy-${i}.png`)); // Load player images
xpos = (this.width * 0.5) - (this.playerSize * 0.5); // Initialize player's horizontal position
ypos = this.playerYOnGround; // Initialize player's vertical position
this.jumpStrength = (this.jumpStrength * 0.99) - this.gravity; // Apply gravity
this.jumpHeight += this.jumpStrength; // Update jump height
if (this.jumpHeight <= 0) {
// Reset jump parameters if player is back on ground
ypos = this.playerYOnGround - this.jumpHeight; // Update player's vertical position based on jump
// Display the player image, use jumping image if jumping or animate otherwise
image(this.playerImg[0], xpos, ypos);
image(this.playerImg[this.playerImgIndex], xpos, ypos);
if (frameCount % this.animationSlowDown === 0) {
this.playerImgIndex = (this.playerImgIndex + 1) % 3;
// Enemy class, inherits from Player and represents the enemy character
class Enemy extends Player {
super(); // Call the parent class constructor
this.enemyImg = []; // Array to hold enemy images for animation
for (let i = 1; i <= 3; i++) {
this.enemyImg.push(loadImage(`enemy-${i}.png`)); // Load enemy images
this.enemyX = 800; // Initial horizontal position of the enemy
this.enemyY = 460; // Initial vertical position of the enemy
this.enemySize = 100; // Size of the enemy character
this.enemyOnSky = random(250, 510); // Randomize enemy's vertical position
this.enemyImgIndex = 0; // Current index of the enemy image
this.enemyX -= 2; // Move the enemy horizontally
this.enemyY = this.enemyOnSky; // Update the enemy's vertical position
// Display and animate the enemy character
image(this.enemyImg[this.enemyImgIndex], this.enemyX, this.enemyY, this.enemySize, this.enemySize);
this.enemyX -= 1; // Move the enemy horizontally
this.initEnemy(); // Re-initialize the enemy position
if (frameCount % this.animationSlowDown === 0) {
this.enemyImgIndex = (this.enemyImgIndex + 1) % 3; // Animate enemy images
// Reset enemy position and increase score when it moves off-screen
this.enemyOnSky = random(250, 510);
score += 5; // Increase score
// Check for collision with the player
if (dist(this.enemyX, this.enemyY, xpos, ypos) <= (this.playerSize / 2 + this.enemySize / 2)) {
win = false; // End the game if collision detected
if (!enemy_sound.isPlaying()) {
sf.stop(); // Stop the background music
enemy_sound.loop(); // Play the enemy sound on loop
// Coin class, inherits from Player and represents collectible coins
class Coin extends Player {
super(); // Call the parent class constructor
this.coinImg = []; // Array to hold coin images for animation
for (let i = 1; i <= 5; i++) {
this.coinImg.push(loadImage(`coin-${i}.png`)); // Load coin images
this.coinX = 800; // Initial horizontal position of the coin
this.coinY = 460; // Initial vertical position of the coin
this.coinSize = 48; // Size of the coin
this.coinImgIndex = 0; // Current index of the coin image
this.coinX = width; // Place the coin at the edge of the screen
this.coinY = random(250, 510); // Randomize the coin's vertical position
// Display and animate the coin
image(this.coinImg[this.coinImgIndex], this.coinX, this.coinY, this.coinSize, this.coinSize);
this.coinX -= 1; // Move the coin horizontally
if (frameCount % this.animationSlowDown === 0) {
this.coinImgIndex = (this.coinImgIndex + 1) % 5; // Animate coin images
// Check if the coin has been collected
if (dist(this.coinX, this.coinY, xpos, ypos) <= (this.playerSize / 2 + this.coinSize / 2)) {
this.initCoin(); // Re-initialize the coin position for it to reappear again
score += 10; // Increase score
if (!coin_sound.isPlaying()) {
coin_sound.play(); // Play the coin collection sound
// Check if the coin has moved off the left edge of the screen without being collected
else if (this.coinX < 0) {
this.initCoin(); // Re-initialize the coin position for it to reappear again
createCanvas(1000, 750); // Set up the canvas
sf.loop(); // Loop the background music if not already playing
player = new Player(); // Instantiate the player
enemy = new Enemy(); // Instantiate the enemy
coin = new Coin(); // Instantiate the coin
player.initPlayer(); // Initialize the player
coin.initCoin(); // Initialize the coin
enemy.initEnemy(); // Initialize the enemy
background(220); // Set the background color
image(img, x1, y); // Draw the background image twice for a scrolling effect
x1 -= 1; // Move the background images to create a scrolling effect
if (x1 <= -1000) { // Reset the background images positions for continuous scrolling
// Animate player, coin, and enemy
// Display the score and instructions
text(`Score: ${score}`, 450, 100);
text("Use UP Key to Jump", 375, 25);
text("Collect the coins and avoid hitting the enemy!", 250, 45);
image(game_over_bg, x3, y2); // Display the game over background
text("Press SHIFT Key to Play Again", 285, 25);
//noLoop(); // Stop the draw loop
// Jump when UP key is pressed
if (keyCode === UP_ARROW && win) {
player.jumping = true; // Enable jumping
if (!jump_sound.isPlaying()) {
jump_sound.play(); // Play jump sound
player.jumpStrength = player.jumpStrengthMax; // Set jump strength to maximum
// Reset game when SHIFT key is pressed after losing
if (keyCode === SHIFT && !win) {
win = true; // Reset game status
score = 0; // Reset score
if (enemy_sound.isPlaying()) {
enemy_sound.stop(); // Stop the enemy sound if playing
loop(); // Resume the draw loop
setup(); // Reinitialize game setup
//GOLD RUSH BY RAYA TABASSUM
//CHARACTERS: SUPER ONION BOY, GOLD COIN, GOMBA THE ENEMY
//5 POINTS FOR AVOIDING THE ENEMY, 10 POINTS FOR COLLECTING THE COIN
// Preload function for loading assets before the game starts
let img, game_over_bg, sf, coin_sound, jump_sound, enemy_sound, pixel_font;
let xpos = 0, ypos = 0, x1 = 0, x2 = 1000, y = 0, x3 = 0, y2 = 0, score = 0, win = true;
let player, enemy, coin;
function preload() {
img = loadImage("plt.png"); // Background image
game_over_bg = loadImage("Gameover.png"); // Game over background image
soundFormats('mp3', 'ogg'); // Specify the sound formats to use
sf = loadSound("backgroundmusic.mp3"); // Background music
coin_sound = loadSound("coin.mp3"); // Sound for collecting coins
jump_sound = loadSound("jumping.mp3"); // Sound for jumping
enemy_sound = loadSound("enemy_sound.mp3"); // Sound when colliding with an enemy
pixel_font = loadFont("PixelFont.ttf"); // Custom font for text display
}
// Player class with properties and methods for the player character
class Player {
constructor() {
this.playerYOnGround = 550; // Y position of the player on the ground
this.playerSize = 60; // Size of the player character
this.bgGroundHeight = 45; // Height of the ground
this.animationSlowDown = 8; // Slows down the animation frame rate
this.width = 1000; // Width of the canvas
this.jumpHeight = 0; // Current jump height
this.jumpStrength = 0; // Current strength of the jump
this.jumpStrengthMax = 5; // Maximum strength of the jump
this.gravity = 0.1; // Gravity affecting the jump
this.jumping = false; // Is the player jumping?
this.playerImg = []; // Array to hold player images for animation
this.numberPlayerImg = 6; // Number of player images
this.playerImgIndex = 0; // Current index of the player image
for (let i = 1; i <= 3; i++) {
this.playerImg.push(loadImage(`guy-${i}.png`)); // Load player images
}
}
initPlayer() {
xpos = (this.width * 0.5) - (this.playerSize * 0.5); // Initialize player's horizontal position
ypos = this.playerYOnGround; // Initialize player's vertical position
}
animatePlayer() {
// Handle jumping logic
if (this.jumping) {
this.jumpStrength = (this.jumpStrength * 0.99) - this.gravity; // Apply gravity
this.jumpHeight += this.jumpStrength; // Update jump height
if (this.jumpHeight <= 0) {
// Reset jump parameters if player is back on ground
this.jumping = false;
this.jumpHeight = 0;
this.jumpStrength = 0;
}
}
ypos = this.playerYOnGround - this.jumpHeight; // Update player's vertical position based on jump
// Display the player image, use jumping image if jumping or animate otherwise
if (this.jumping) {
image(this.playerImg[0], xpos, ypos);
} else {
image(this.playerImg[this.playerImgIndex], xpos, ypos);
// Animate player images
if (frameCount % this.animationSlowDown === 0) {
this.playerImgIndex = (this.playerImgIndex + 1) % 3;
}
}
}
}
// Enemy class, inherits from Player and represents the enemy character
class Enemy extends Player {
constructor() {
super(); // Call the parent class constructor
this.enemyImg = []; // Array to hold enemy images for animation
for (let i = 1; i <= 3; i++) {
this.enemyImg.push(loadImage(`enemy-${i}.png`)); // Load enemy images
}
this.enemyX = 800; // Initial horizontal position of the enemy
this.enemyY = 460; // Initial vertical position of the enemy
this.enemySize = 100; // Size of the enemy character
this.enemyOnSky = random(250, 510); // Randomize enemy's vertical position
this.enemyImgIndex = 0; // Current index of the enemy image
}
initEnemy() {
this.enemyX -= 2; // Move the enemy horizontally
this.enemyY = this.enemyOnSky; // Update the enemy's vertical position
}
animateEnemy() {
// Display and animate the enemy character
image(this.enemyImg[this.enemyImgIndex], this.enemyX, this.enemyY, this.enemySize, this.enemySize);
this.enemyX -= 1; // Move the enemy horizontally
this.initEnemy(); // Re-initialize the enemy position
if (frameCount % this.animationSlowDown === 0) {
this.enemyImgIndex = (this.enemyImgIndex + 1) % 3; // Animate enemy images
}
// Reset enemy position and increase score when it moves off-screen
if (this.enemyX <= 0) {
this.enemyX = 1000;
this.enemyOnSky = random(250, 510);
this.initEnemy();
score += 5; // Increase score
}
// Check for collision with the player
if (dist(this.enemyX, this.enemyY, xpos, ypos) <= (this.playerSize / 2 + this.enemySize / 2)) {
win = false; // End the game if collision detected
if (!enemy_sound.isPlaying()) {
if (sf.isPlaying()) {
sf.stop(); // Stop the background music
}
enemy_sound.loop(); // Play the enemy sound on loop
}
}
}
}
// Coin class, inherits from Player and represents collectible coins
class Coin extends Player {
constructor() {
super(); // Call the parent class constructor
this.coinImg = []; // Array to hold coin images for animation
for (let i = 1; i <= 5; i++) {
this.coinImg.push(loadImage(`coin-${i}.png`)); // Load coin images
}
this.coinX = 800; // Initial horizontal position of the coin
this.coinY = 460; // Initial vertical position of the coin
this.coinSize = 48; // Size of the coin
this.coinImgIndex = 0; // Current index of the coin image
}
initCoin() {
this.coinX = width; // Place the coin at the edge of the screen
this.coinY = random(250, 510); // Randomize the coin's vertical position
}
animateCoin() {
// Display and animate the coin
image(this.coinImg[this.coinImgIndex], this.coinX, this.coinY, this.coinSize, this.coinSize);
this.coinX -= 1; // Move the coin horizontally
if (frameCount % this.animationSlowDown === 0) {
this.coinImgIndex = (this.coinImgIndex + 1) % 5; // Animate coin images
}
// Check if the coin has been collected
if (dist(this.coinX, this.coinY, xpos, ypos) <= (this.playerSize / 2 + this.coinSize / 2)) {
this.initCoin(); // Re-initialize the coin position for it to reappear again
score += 10; // Increase score
if (!coin_sound.isPlaying()) {
coin_sound.play(); // Play the coin collection sound
}
}
// Check if the coin has moved off the left edge of the screen without being collected
else if (this.coinX < 0) {
this.initCoin(); // Re-initialize the coin position for it to reappear again
}
}
}
function setup() {
createCanvas(1000, 750); // Set up the canvas
if (!sf.isPlaying()) {
sf.loop(); // Loop the background music if not already playing
}
player = new Player(); // Instantiate the player
enemy = new Enemy(); // Instantiate the enemy
coin = new Coin(); // Instantiate the coin
player.initPlayer(); // Initialize the player
coin.initCoin(); // Initialize the coin
enemy.initEnemy(); // Initialize the enemy
}
function draw() {
background(220); // Set the background color
image(img, x1, y); // Draw the background image twice for a scrolling effect
image(img, x2, y);
x1 -= 1; // Move the background images to create a scrolling effect
x2 -= 1;
if (x1 <= -1000) { // Reset the background images positions for continuous scrolling
x1 = 1000;
}
if (x2 <= -1000) {
x2 = 1000;
}
// Animate player, coin, and enemy
player.animatePlayer();
coin.animateCoin();
enemy.animateEnemy();
// Display the score and instructions
textSize(25);
textFont(pixel_font);
fill(255, 215, 0);
text(`Score: ${score}`, 450, 100);
text("Use UP Key to Jump", 375, 25);
textSize(20);
text("Collect the coins and avoid hitting the enemy!", 250, 45);
if (!win) {
image(game_over_bg, x3, y2); // Display the game over background
textSize(30);
text("Press SHIFT Key to Play Again", 285, 25);
//noLoop(); // Stop the draw loop
}
}
function keyPressed() {
// Jump when UP key is pressed
if (keyCode === UP_ARROW && win) {
player.jumping = true; // Enable jumping
if (!jump_sound.isPlaying()) {
jump_sound.play(); // Play jump sound
}
player.jumpStrength = player.jumpStrengthMax; // Set jump strength to maximum
}
// Reset game when SHIFT key is pressed after losing
if (keyCode === SHIFT && !win) {
win = true; // Reset game status
score = 0; // Reset score
if (enemy_sound.isPlaying()) {
enemy_sound.stop(); // Stop the enemy sound if playing
}
loop(); // Resume the draw loop
setup(); // Reinitialize game setup
}
}