xxxxxxxxxx
154
/***********************************************************
10.13.22
Michael Shiloh
Midterm Project - Zombie Game, Happy Halloween!
This game was inspired by the google dinosaur game https://trex-runner.com/ and The Coding Train video https://www.youtube.com/watch?v=l0HoJHc-63Q
I used the Coding Train video as my base code (excluding the portion where you control the game with your voice). I still needed to add the instruction & start screen, end screen, and to make the game restart without playing the program.
Guidelines:
Make a game using everything you have learned so far
Can be one or more players (Zombie)
Must include
At least one shape (Pumpkin, Cheat Code)
At least one image (Zombie, Pumpkin, Background, Start, End)
At least one sound (Collision)
At least one on-screen text (Start & End Instructions)
Object Oriented Programming
The game must start with a screen giving instructions, and must wait there until a button or key (your choice) is pressed (Press any key)
After the game is won or lost, there must be a way to restart the game without closing and restarting the program (Press any key)
************************************************************/
let gameState = "start";
let wonGame = false;
let zombie;
let pImg;
let zImg;
let bImg;
let hImg;
let lImg;
let sImg;
let pumpkins = [];
let jumps = 0;
function preload() {
const options = {
probabilityThreshold: 0.95,
};
pImg = loadImage("pumpkint.png");
zImg = loadImage("zombiet.png");
bImg = loadImage("background.jpg");
hImg = loadImage("hzombie.jpg");
lImg = loadImage("lpumpkin.jpg");
sImg = loadImage("start.jpg")
}
function setup() {
createCanvas(700, 450);
zombie = new Zombie();
restartGame();
}
function keyPressed() {
print("event");
if (key == " ") {
jumps++;
zombie.jump(); // if key is space bar then he jumps
}
if (gameState == "start") {
print("playing game");
// Start the game
gameState = "playing";
} else if (gameState == "playing") {
if (jumps >= 10) {
wonGame = true;
gameState = "end";
}
} else if (gameState == "end") {
restartGame();
}
}
function restartGame() {
print("Resetting game state");
pumpkins = [];
jumps = 0;
wonGame = false;
gameState = "start";
}
function draw() {
if (gameState == "start") {
drawStartScreen();
return;
}
if (gameState == "end") {
drawEndScreen();
return;
}
if (random(1) < 0.005 && gameState == "playing") {
pumpkins.push(new Pumpkin());
// random chance of adding a new train once game restarts and the player is playing
}
background(bImg);
for (let p of pumpkins) {
// for all of the pumpkins that exist... move and show
p.move();
p.show();
if (zombie.hits(p)) {
gameState = "end";
print("Game ending");
// noLoop(); // stop the sketch completely
}
}
zombie.show();
zombie.move();
}
function drawEndScreen() {
if (wonGame) {
background(hImg);
textSize(20);
fill('white');
text("YOU WON! Zombie is safe from the pumpkins. Happy Halloween!", 30, 370);
text("Press any key to restart", 20, 25);
} else {
background(lImg);
text("You LOST. Zombie died (again) from a pumpkin attack.", 100, 200);
text("Press any key to try again", 100, 230);
}
}
function drawStartScreen() {
background(sImg);
textSize(20);
fill('white')
text("Welcome to the Zombie Game!", 50, 100);
text("Jump over the Pumpkins Until the Zombie has Reached the Safe Zone", 50, 250);
text("Press any Key to Start", 50, 200);
}