-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
119 lines (105 loc) · 3.43 KB
/
script.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let points = 0;
let hp = 3;
function updatePoints() {
let result = `${points}`;
while (result.length < 5) result = `0${result}`;
document.querySelector(".score").innerText = result;
document.querySelector(".hp").innerText = "🤍".repeat(Math.max(0, hp));
}
function escapedZombie() {
hp--;
miss();
}
function aim(e) {
document.querySelector(".crosshair").style.left = `${e.pageX}px`;
document.querySelector(".crosshair").style.top = `${e.pageY}px`;
}
function kill(e) {
e.stopPropagation();
points += 12;
updatePoints();
clearInterval(this.getAttribute("data-animation"));
this.parentElement.removeChild(this);
}
function miss(e) {
points -= 6;
if (points < 0) points = 0;
updatePoints();
}
class Zombie {
constructor() {
this.positionX = Math.random() * 25 + 50;
this.positionY = window.innerWidth;
this.size = Math.random() + 1;
this.speed = Math.random() * 0.7 + 0.3;
}
animate(z) {
let movement = setInterval(() => {
if (["", "0%"].includes(z.style.backgroundPositionX)) {
z.style.backgroundPositionX = "900%";
} else {
let c = z.style.backgroundPositionX;
c = c.substring(0, c.length - 1) - 100;
z.style.backgroundPositionX = `${c}%`;
}
let y = z.style.left;
y = y.substring(0, y.length - 2) - 10 * this.size;
z.style.left = `${y}px`;
if (y < -312) {
clearInterval(movement);
z.parentElement.removeChild(z);
escapedZombie();
}
}, 100 * this.speed);
return movement;
}
spawn() {
let zombie = document.createElement("div");
zombie.classList.add("zombie");
zombie.style.transform = `scale(${this.size})`;
zombie.style.left = `${this.positionY}px`;
zombie.style.top = `${this.positionX}vh`;
let animation = this.animate(zombie);
zombie.setAttribute("data-animation", animation);
zombie.addEventListener("click", kill);
return zombie;
}
}
function spawnZombies() {
updatePoints();
setTimeout(() => {
let zombie = new Zombie();
document.querySelector(".board").appendChild(zombie.spawn());
if (hp <= 0) {
clearBoard();
} else {
spawnZombies();
}
}, 1000 * Math.random());
}
function clearBoard() {
for (let z of document.querySelectorAll(".zombie")) {
z.style.opacity = 0;
z.removeEventListener("click", kill);
clearInterval(z.getAttribute("data-animation"));
}
let msg = document.createElement("div");
msg.classList.add("message");
let msgContent = document.createTextNode("You lose!");
msg.appendChild(msgContent);
document.querySelector(".board").appendChild(msg);
document.querySelector(".board").removeEventListener("click", miss);
}
function startGame(e) {
document.querySelector(".board").addEventListener("click", miss);
document.querySelector(".crosshair").style.display = "block";
document.querySelector("a").style.cursor = "none";
document.body.style.cursor = "none";
this.remove();
spawnZombies();
}
document.querySelector("#start").addEventListener("click", startGame);
document.body.addEventListener("mousemove", aim);
document.body.addEventListener("mousedown", (e) => {
e.preventDefault();
});