xxxxxxxxxx
371
// Aisha Alketbi
// Midterm Project : Floor Is Lava
// The aim of the game is to get from one side of the map through the other whilst jumping on platforms to avoid falling into the 'lava'.
let gameMode = 0; // game hasn't started
let bgImg;
let x1 = 0;
let x2;
let scrollSpeed = 2;
let myFont;
let groundImg;
let platImg;
let song;
let gravity = 1.5;
//Preloads all files
function preload() {
bgImg = loadImage("gameback.JPG");
myFont = loadFont("font.otf");
groundImg = loadImage("ground.png");
platImg = loadImage("plat.png");
song = loadSound("lava1.mp3");
}
class Player {
constructor() {
this.x = 100;
this.y = 100;
this.d = 30;
// constant velocity of the ball
this.v = 0.001;
this.xg = 0;
this.yg = 0;
}
draw() {
fill(15, 192, 252);
stroke(0, 255, 255);
ellipse(this.x, this.y, this.d);
}
update() {
this.draw();
this.y += this.yg;
// ensures that ball doesnt go past screen
if (this.y + this.d + this.yg <= windowHeight) {
this.yg += gravity;
}
}
}
class Ground {
constructor({ x, y }) {
this.x = x;
this.y = y;
this.width = 495;
this.height = 150;
this.image = groundImg;
}
draw() {
image(this.image, this.x, this.y, this.width, this.height);
}
}
class Platform {
constructor({ x, y }) {
this.x = x;
this.y = y;
this.width = 194;
this.height = 108;
this.image = platImg;
}
draw() {
image(this.image, this.x, this.y, this.width, this.height);
}
}
let player;
let ground;
let platforms;
let scrollOffset = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
x2 = width;
song.loop();
player = new Player();
// An array of where I want the platform/ground to be at
ground = [
new Ground({ x: 0, y: 500 }),
new Ground({ x: 500, y: 500 }),
new Ground({ x: 1100, y: 500 }),
new Ground({ x: 2600, y: 500 }),
new Ground({ x: 3300, y: 500 }),
new Ground({ x: 5100, y: 500 }),
new Ground({ x: 5850, y: 500 }),
];
platforms = [
new Platform({ x: 1700, y: 450 }),
new Platform({ x: 2000, y: 350 }),
new Platform({ x: 2300, y: 270 }),
new Platform({ x: 3900, y: 450 }),
new Platform({ x: 4100, y: 350 }),
new Platform({ x: 4300, y: 250 }),
new Platform({ x: 4500, y: 350 }),
new Platform({ x: 4700, y: 450 }),
];
}
function draw() {
clear();
//if game hasn't started
if (gameMode == 0) {
textAlign(CENTER, CENTER);
textFont(myFont, 80);
fill(155, 50, 10);
stroke(254, 177, 109);
strokeWeight(6);
text("FLOOR IS LAVA", width / 2, 200);
textFont(myFont, 45);
text("Press 1 for easy", width / 2, 300);
textFont(myFont, 45);
text("Press 2 for Medium", width / 2, 350);
textFont(myFont, 45);
text("Press 3 for hard", width / 2, 400);
textFont(myFont, 45);
text('Press "I" for instructions', width / 2, 500);
}
if (gameMode == 1) {
//scrolls background image
image(bgImg, x1, 0, width, height);
image(bgImg, x2, 0, width, height);
x1 -= scrollSpeed;
x2 -= scrollSpeed;
if (x1 < -width) {
x1 = width;
}
if (x2 < -width) {
x2 = width;
}
// goes through the array
player.update();
ground.forEach((ground) => {
ground.draw();
});
platforms.forEach((platforms) => {
platforms.draw();
});
// stops ball at x = 450 to create an illusion of scrolling
if (player.x < 450) {
player.x += player.xg;
player.xg += player.v;
ground.forEach((ground) => {
//moves the ground to the left to create the illusion of scrolling
ground.x -= 10;
scrollOffset += 5;
});
platforms.forEach((platforms) => {
//moves the ground to the left to create the illusion of scrolling
platforms.x -= 10;
});
}
// collision (ball stops above platorm)
ground.forEach((ground) => {
if (
player.y + player.d <= ground.y &&
player.y + player.d + player.yg >= ground.y &&
player.x + player.d >= ground.x &&
player.x <= ground.x + ground.width
) {
player.yg = 0;
}
});
platforms.forEach((platforms) => {
if (
player.y + player.d <= platforms.y &&
player.y + player.d + player.yg >= platforms.y &&
player.x + player.d >= platforms.x &&
player.x <= platforms.x + platforms.width
) {
player.yg = 0;
}
});
//If players falls into lava display Lose screen
if (player.y > windowHeight) {
gameMode = 3;
restartGame();
}
//if Player wins display win screen
if (scrollOffset > 20900) {
gameMode = 4;
restartGame();
}
}
// Instruction screen
if (gameMode == 2) {
textAlign(CENTER, CENTER);
textFont(myFont, 80);
text("INSTRUCTIONS", width / 2, 200);
textFont(myFont, 25);
text("Help the Ball reach from the end of the map", width / 2, 300);
textFont(myFont, 25);
text("Avoid falling into the lava!", width / 2, 350);
textFont(myFont, 25);
text("Use the Up Arrow to jump", width / 2, 400);
textFont(myFont, 15);
text("Press esc to return back to main menu", width / 2, 450);
}
//Lose Screen
if (gameMode == 3) {
textAlign(CENTER, CENTER);
textFont(myFont, 80);
fill(155, 50, 10);
stroke(254, 177, 109);
strokeWeight(6);
text("you lose", width / 2, 200);
textFont(myFont, 32);
text("Press esc to return back to main menu", width / 2, 300);
}
//Win Screen
if (gameMode == 4) {
textAlign(CENTER, CENTER);
textFont(myFont, 80);
fill(155, 50, 10);
stroke(254, 177, 109);
strokeWeight(6);
text("you win", width / 2, 200);
textFont(myFont, 32);
text("Press esc to return back to main menu", width / 2, 300);
scrollOffset = 0;
}
}
function keyPressed() {
if (keyCode === 27) {
gameMode = 0;
}
if (keyCode === 49) {
gameMode = 1;
}
if (keyCode === 73) {
gameMode = 2;
}
if (keyCode === UP_ARROW) {
player.yg -= 20;
}
if (keyCode === 50) {
gameMode = 5;
Level2();
gameMode = 1;
}
if (keyCode === 51) {
gameMode = 6;
scrollOffset = 8350;
Level3();
gameMode = 1;
}
}
function restartGame() {
// Puts everything back to position
scrollSpeed = 3;
player = new Player();
scrollOffset = 0;
ground = [
new Ground({ x: 0, y: 550 }),
new Ground({ x: 500, y: 550 }),
new Ground({ x: 1100, y: 550 }),
new Ground({ x: 2600, y: 550 }),
new Ground({ x: 3300, y: 550 }),
new Ground({ x: 5100, y: 550 }),
new Ground({ x: 5850, y: 550 }),
];
platforms = [
new Platform({ x: 1700, y: 450 }),
new Platform({ x: 2000, y: 350 }),
new Platform({ x: 2300, y: 270 }),
new Platform({ x: 3900, y: 450 }),
new Platform({ x: 4100, y: 350 }),
new Platform({ x: 4300, y: 250 }),
new Platform({ x: 4500, y: 350 }),
new Platform({ x: 4700, y: 450 }),
];
}
function Level2() {
player = new Player();
ground = [
new Ground({ x: 0, y: 550 }),
new Ground({ x: 1900, y: 550 }),
new Ground({ x: 4700, y: 550 }),
new Ground({ x: 7800, y: 550 }),
new Ground({ x: 8500, y: 550 }),
];
platforms = [
new Platform({ x: 550, y: 450 }),
new Platform({ x: 1000, y: 450 }),
new Platform({ x: 1450, y: 450 }),
new Platform({ x: 2550, y: 450 }),
new Platform({ x: 2900, y: 350 }),
new Platform({ x: 3250, y: 250 }),
new Platform({ x: 3600, y: 150 }),
new Platform({ x: 3950, y: 150 }),
new Platform({ x: 4300, y: 150 }),
new Platform({ x: 5400, y: 550 }),
new Platform({ x: 5800, y: 550 }),
new Platform({ x: 6200, y: 550 }),
new Platform({ x: 6500, y: 450 }),
new Platform({ x: 6900, y: 450 }),
new Platform({ x: 7300, y: 400 }),
new Platform({ x: 8100, y: 550 }),
];
}
function Level3() {
player = new Player();
ground = [
new Ground({ x: 4100, y: 550 }),
new Ground({ x: 5200, y: 550 }),
new Ground({ x: 8700, y: 550 }),
];
platforms = [
new Platform({ x: 100, y: 200 }),
new Platform({ x: 500, y: 200 }),
new Platform({ x: 900, y: 300 }),
new Platform({ x: 1300, y: 200 }),
new Platform({ x: 1700, y: 100 }),
new Platform({ x: 2100, y: 400 }),
new Platform({ x: 2500, y: 500 }),
new Platform({ x: 2900, y: 400 }),
new Platform({ x: 3200, y: 270 }),
new Platform({ x: 3600, y: 270 }),
new Platform({ x: 4800, y: 550 }),
new Platform({ x: 5900, y: 450 }),
new Platform({ x: 6300, y: 350 }),
new Platform({ x: 6700, y: 450 }),
new Platform({ x: 7100, y: 350 }),
new Platform({ x: 7400, y: 250 }),
new Platform({ x: 7800, y: 150 }),
new Platform({ x: 8200, y: 550 }),
];
}