-
Notifications
You must be signed in to change notification settings - Fork 162
/
app.js
93 lines (80 loc) · 3.04 KB
/
app.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
var jet = document.getElementById("jet");
var board = document.getElementById("board");
window.addEventListener("keydown", (e) => {
var left = parseInt(window.getComputedStyle(jet).getPropertyValue("left"));
if (e.key == "ArrowLeft" && left > 0) {
jet.style.left = left - 10 + "px";
}
//460 => board width - jet width
else if (e.key == "ArrowRight" && left <= 460) {
jet.style.left = left + 10 + "px";
}
if (e.key == "ArrowUp" || e.keyCode == 32) {
//32 is for space key
var bullet = document.createElement("div");
bullet.classList.add("bullets");
board.appendChild(bullet);
var movebullet = setInterval(() => {
var rocks = document.getElementsByClassName("rocks");
for (var i = 0; i < rocks.length; i++) {
var rock = rocks[i];
if (rock != undefined) {
var rockbound = rock.getBoundingClientRect();
var bulletbound = bullet.getBoundingClientRect();
//Condition to check whether the rock/alien and the bullet are at the same position..!
//If so,then we have to destroy that rock
if (
bulletbound.left >= rockbound.left &&
bulletbound.right <= rockbound.right &&
bulletbound.top <= rockbound.top &&
bulletbound.bottom <= rockbound.bottom
) {
rock.parentElement.removeChild(rock); //Just removing that particular rock;
//Scoreboard
document.getElementById("points").innerHTML =
parseInt(document.getElementById("points").innerHTML) + 1;
}
}
}
var bulletbottom = parseInt(
window.getComputedStyle(bullet).getPropertyValue("bottom")
);
//Stops the bullet from moving outside the gamebox
if (bulletbottom >= 500) {
clearInterval(movebullet);
}
bullet.style.left = left + "px"; //bullet should always be placed at the top of my jet..!
bullet.style.bottom = bulletbottom + 3 + "px";
});
}
});
var generaterocks = setInterval(() => {
var rock = document.createElement("div");
rock.classList.add("rocks");
//Just getting the left of the rock to place it in random position...
var rockleft = parseInt(
window.getComputedStyle(rock).getPropertyValue("left")
);
//generate value between 0 to 450 where 450 => board width - rock width
rock.style.left = Math.floor(Math.random() * 450) + "px";
board.appendChild(rock);
}, 1000);
var moverocks = setInterval(() => {
var rocks = document.getElementsByClassName("rocks");
if (rocks != undefined) {
for (var i = 0; i < rocks.length; i++) {
//Now I have to increase the top of each rock,so that the rocks can move downwards..
var rock = rocks[i]; //getting each rock
var rocktop = parseInt(
window.getComputedStyle(rock).getPropertyValue("top")
);
//475 => boardheight - rockheight + 25
if (rocktop >= 475) {
alert("Game Over");
clearInterval(moverocks);
window.location.reload();
}
rock.style.top = rocktop + 25 + "px";
}
}
}, 450);