-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript1.js
108 lines (86 loc) · 2.84 KB
/
script1.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
var dino = document.querySelector(".dino");
var gameOver = document.querySelector(".gameOver");
var obstacle = document.querySelector(".obstacle");
var playGame = document.getElementById("button");
var newScore = document.getElementById("score");
var playButton = document.getElementById("button2");
var start = document.querySelector(".initial");
var display = document.querySelector(".gameContainer");
score = 0;
cross = true;
playButton.addEventListener("click", run);
game();
function game(){
document.onkeydown = function (e) {
if (e.keyCode == 38) {
dino.classList.add("animateDino");
setTimeout(() => {
dino.classList.remove("animateDino");
}, 700);
}
if (e.keyCode == 39) {
dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue("left"));
dino.style.left = dx + 112 + "px";
}
if (e.keyCode == 37) {
dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue("left"));
dino.style.left = dx - 112 + "px";
}
}
}
setInterval(() => {
dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue("left"));
dy = parseInt(window.getComputedStyle(dino, null).getPropertyValue("top"));
ox = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue("left"));
oy = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue("top"));
offsetX = Math.abs(dx - ox);
offsetY = Math.abs(dy - oy);
if (offsetX < 73 && offsetY < 52)
{
gameOver.innerHTML = "Game Over !!!!";
obstacle.classList.remove("obstacleAni");
playGame.style.visibility = "visible";
playGame.addEventListener("click", );
obstacle.style.display = "none";
dino.style.display = "none";
newScore.innerHTML = "Your Score is " + (score-1);
newScore.style.visibility = "visible";
scoreCont.style.visibility = "hidden";
}
else if (offsetX < 145 && cross) {
score += 1;
updateScore(score);
cross = false;
setTimeout(() => {
cross = true;
}, 2000);
setTimeout(() => {
aniDur = parseFloat(window.getComputedStyle(obstacle, null).getPropertyValue("animation-duration"));
if(aniDur >= 5 )
{
newDur = aniDur - 0.1;
obstacle.style.animationDuration = newDur + "s";
console.log("New animation duration: ", newDur);
}
}, 1000);
}
}, 20);
function updateScore(score) {
scoreCont.innerHTML = "Your Score: " + score;
}
function run() {
if( start.style.display === "block")
{
start.style.display = "none";
display.style.display = "block";
}
else
{
start.style.display = "block";
display.style.display = "none";
}
}
function reset()
{
score = 0 ;
}