-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (52 loc) · 2.09 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
var container = document.querySelector("#container");
var mainBeyblade = document.querySelector("#mainBeyblade");
var block = document.querySelector("#block");
var road = document.querySelector("#road");
var cloud = document.querySelector("#cloud");
var score = document.querySelector("#score");
var gameOver = document.querySelector("#gameOver");
//declaring variable for score
var interval = null;
var playerScore = 0;
//function for score counter increase
var scoreCounter = () => {
playerScore++;
score.innerHTML = `Score <b>${playerScore}</b>`;
}
//start Game
window.addEventListener("keydown", (start) => {
if (start.code == "Space") {
gameOver.style.display = "none";
block.classList.add("blockActive");
road.firstElementChild.style.animation = "roadAnimate 1.5s linear infinite";
cloud.firstElementChild.style.animation = "cloudAnimate 50s linear infinite";
//score
let playerScore = 0;
interval = setInterval(scoreCounter, 400);
}
});
//jump Your Character
window.addEventListener("keydown", (e) => {
// console.log(e);
if (e.key == "ArrowUp")
if (mainBeyblade.classList != "mainBeybladeActive") {
mainBeyblade.classList.add("mainBeybladeActive");
// remove class after 0.5 seconds
setTimeout(() => {
mainBeyblade.classList.remove("mainBeybladeActive");
}, 500);
}
});
//'Game Over' if 'Character' hit The 'Block'
var result = setInterval(() => {
var BayBottom = parseInt(getComputedStyle(mainBeyblade).getPropertyValue("bottom"));
var blockLeft = parseInt(getComputedStyle(block).getPropertyValue("left"));
if (BayBottom <= 90 && blockLeft >= 20 && blockLeft <= 125) {
gameOver.style.display = "block";
block.classList.remove("blockActive");
road.firstElementChild.style.animation = "none";
cloud.firstElementChild.style.animation = "none";
clearInterval(interval);
playerScore = 0;
}
}, 10);