xxxxxxxxxx
103
/***********************************************************
10.13.22 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) but I still needed to add the instruction & start screen, end screen, and to make the game restart without playing the program.
************************************************************/
let gameState = "start";
let wonGame = false;
let zombie;
let pImg;
let zImg;
let bImg;
let pumpkins = [];
function preload() {
const options = {
probabilityThreshold: 0.95,
};
pImg = loadImage("pumpkint.png");
zImg = loadImage("zombiet.png");
bImg = loadImage("background.jpg");
}
function mousePressed() {
pumpkins.push(new Pumpkin());
}
function setup() {
createCanvas(700, 450);
zombie = new Zombie();
restartGame();
}
function keyPressed() {
if (key == " ") {
zombie.jump(); // if key is space bar then he jumps
}
if (gameState == 'start') {
// Start the game
gameState = 'playing';
} else if (gameState == 'playing') {
// Magically win game on space bar
if (key == ' ') {
// Won game!
wonGame = true;
gameState = 'end';
}
} else if (gameState == 'end') {
restartGame();
}
}
function restartGame() {
print ('Resetting game state');
wonGame = false;
gameState = 'start';
}
function draw() {
if (random(1) < 0.005) {
pumpkins.push(new Pumpkin());
// random chance of adding a new train
}
background(bImg);
for (let p of pumpkins) {
// for all of the trains that exist... move and show
p.move();
p.show();
if (zombie.hits(p)) {
console.log("game over");
noLoop(); // stop the sketch completely
}
}
zombie.show();
zombie.move();
}
function drawEndScreen() {
if (wonGame) {
background('black');
text('You WON!', 100, 200);
text('Press any key to restart', 100, 230);
} else {
background('red');
text('You LOST. Sorry.', 100, 200);
text('Press any key to restart', 100, 230);
}
}