xxxxxxxxxx
136
let gameState = "start";
let score = 0;
let highScore = 0;
//image and class variables
let gingerbreadman;
let backgroundImg;
let gingerbreadmanImg;
let chimney1Img;
let chimney2Img;
let endImg;
let introImg;
let chimneys = [];
//audio variables
let gameOverSound;
let jump;
let christmasTrack;
function preload() {
//upload and call all the images
backgroundImg = loadImage("Background.png");
gingerbreadmanImg = loadImage("gingerbreadman.png");
chimney1Img = loadImage("Chimney1.png");
chimney2Img = loadImage("Chimney2.png");
endImg = loadImage("gingerbread-skeleton-men-mobile.png");
introImg = loadImage("intropage.png");
//upload and call audio
gameOverSound = loadSound("negative_beeps-6008.mp3");
jump = loadSound("toy-button-105724.mp3");
christmasTrack = loadSound("jingle-bells-orchestra-127418.mp3");
} // end of preload
function setup() {
createCanvas(windowWidth, windowHeight);
gingerbreadman = new Gingerbreadman();
chimney1 = new Chimney1();
chimney2 = new Chimney2();
christmasTrack.loop();
christmasTrack.playMode("restart");
christmasTrack.setVolume(0.7);
gameOverSound.playMode("restart");
jump.playMode("restart");
} // end of setup
function startScreen() {
background(introImg);
if (!christmasTrack.isPlaying()) {
christmasTrack.play();
}
textSize(30);
fill(255);
textFont("PressStart2P-Regular.ttf");
text("Press enter to start", width / 2.7, height / 1.2);
if (keyCode == ENTER) {
chimneys = [];
gameState = "drawGame";
}
} // end of startScreen
function keyPressed() {
//make it so that when you press the up arrow, the gingerbread man will jump
if (key === " ") {
gingerbreadman.jump();
jump.play();
}
} // end of keyPressed
function draw() {
console.log(gameState);
//set start and end screen
if (gameState == "start") {
startScreen();
} else if (gameState == "drawGame") {
drawGame();
} else if (gameState == "gameOver") {
gameOver();
}
} // end of draw
function drawGame() {
//random chance of a new chimney appearing
if (random(1) < 0.004) {
chimneys.push(new Chimney1());
}
if (random(1) < 0.003) {
chimneys.push(new Chimney2());
}
background(backgroundImg);
for (let c of chimneys) {
c.move();
c.show();
if (gingerbreadman.hits(c)) {
gameState = "gameOver";
}
//score
textSize(56);
textFont("PressStart2P-Regular.ttf");
fill(255);
text("Score: " + score, 130, 80);
if ("drawGame") {
score = score + Math.round(frameCount / 600);
}
}
gingerbreadman.show();
gingerbreadman.move();
} // end of drawGame
function gameOver() {
background(endImg);
christmasTrack.stop();
if (!gameOverSound.isPlaying()) {
gameOverSound.play();
gameOverSound.loop();
}
//keeps track of the best score
if (highScore < score) {
highScore = score;
} else {
highScore = highScore;
}
if (keyCode == BACKSPACE) {
gameOverSound.stop();
score = 0;
gameState = "start";
}
//instructions for restarting the game
textSize(24);
textFont("PressStart2P-Regular.ttf");
fill(255);
text("Press backspace to restart", width / 2, height / 2);
} // end of gameOver