-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (52 loc) · 1.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ** GLOBAL VARIABLES
//CANVAS SETUP
let canvas = document.querySelector("#my-canvas");
let ctx = canvas.getContext("2d");
//DOM ELEMENTS
let startBtn = document.querySelector("#start-btn")
let restartBtn = document.querySelector("#restart-btn")
let splashScreen = document.querySelector("#splash-screen")
let gameoverScreen = document.querySelector("#gameover-screen")
let score = document.querySelector("span")
//* SOUNDS
let audio = document.createElement("AUDIO")
document.body.appendChild(audio);
audio.src = "./sounds/gameintro.mp3"
let gunShot = new Audio("./sounds/gunshot.mp3")
let gameEnd = new Audio("./sounds/gameover1.mp3")
let gameEnd2 = new Audio("./sounds/gameover2.mp3")
//GAME OBJECT
let game;
//*FUNCTIONS
const startGame = () => {
splashScreen.style.display = "none";
canvas.style.display = "flex";
scorespan.style.display = "flex"
game = new Game();
game.gameLoop();
audio.muted = true;
}
const restartGame = () => {
gameoverScreen.style.display = "none";
canvas.style.display = "flex";
game = new Game();
game.gameLoop();
audio.muted = true;
scorespan.style.display = "flex"
}
//* ADD EVENT LISTENERS
startBtn.addEventListener("click", startGame);
restartBtn.addEventListener("click", restartGame);
window.addEventListener("keydown", (event) => {
if(event.code === "ArrowRight") {
game.soldier.soldierMoveRight();
} else if (event.code === "ArrowLeft") {
game.soldier.soldierMoveLeft();
} else if (event.code === "Space") {
game.spawnBullets();
gunShot.play();
};
});
window.addEventListener("mousemove", function () {
audio.play()
})