-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (74 loc) · 2.27 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
var block = document.getElementById("block");
var hole = document.getElementById("hole");
var score = document.getElementById("score");
var start = document.getElementById("start");
var restart = document.getElementById("restart");
var modal = document.getElementById("modal");
var root = document.querySelector(":root");
var character = document.getElementById("character");
var jumping = 0;
var counter = 0;
hole.addEventListener("animationiteration", () => {
var random = Math.random() * 300 + 150;
hole.style.top = random + "px";
counter++;
});
setInterval(() => {
var characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue("top")
);
if (
jumping == 0 &&
parseInt(window.getComputedStyle(character).getPropertyValue("left")) ==
50
) {
character.style.top = characterTop + 3 + "px";
}
var blockLeft = parseInt(
window.getComputedStyle(block).getPropertyValue("left")
);
var holeTop = parseInt(
window.getComputedStyle(hole).getPropertyValue("top")
);
if (
characterTop > 535 ||
(blockLeft < 125 &&
blockLeft > -50 &&
(characterTop < holeTop || characterTop > holeTop + 85))
) {
gameOver();
}
score.innerHTML = counter;
}, 10);
function jump() {
jumping = 1;
let jumpCount = 0;
var jumpInterval = setInterval(function() {
var characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue("top")
);
if (characterTop > 6 && jumpCount < 15) {
character.style.top = characterTop - 5 + "px";
}
if (jumpCount > 20) {
clearInterval(jumpInterval);
jumping = 0;
jumpCount = 0;
}
jumpCount++;
}, 10);
}
function startGame() {
modal.style.display = "none";
character.style.animation = "showchr 0.7s forwards linear";
block.style.animation = "block 3s infinite linear";
hole.style.animation = "block 3s infinite linear";
}
function gameOver() {
character.style.top = 540 + "px";
modal.style.display = "block";
start.style.display = "none";
restart.style.display = "block";
block.style.animationPlayState = "paused";
hole.style.animationPlayState = "paused";
}