-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
188 lines (169 loc) · 7.28 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var rocket = document.getElementById('rocket');
var board = document.getElementById('board');
var boardWidth = parseInt(board.offsetWidth);
var boardHeight = parseInt(board.offsetHeight);
var score = 0;
var bulletCounts = 200;
var alienCount = 100;
var play = false;
window.addEventListener('keydown', (e) => {
var left = parseInt(window.getComputedStyle(rocket).getPropertyValue("left"));
if(e.key == 'ArrowLeft' && left>0) {
rocket.style.left = left - 15 + "px";
}
else if(e.key == "ArrowRight" && left <= boardWidth-36) {
rocket.style.left = left + 15 + "px";
}
if(e.key == "ArrowUp" || e.keyCode == 32 &&bulletCounts>0 && play===true && alienCount>0) {
var bullet_sound = new Audio('./sound/alien_end.mp3');
bullet_sound.play();
var bullet = document.createElement("div");
bullet.classList.add("bullets");
board.appendChild(bullet);
bulletCounts--;
document.getElementById('display-bullet').innerText=bulletCounts;
if(bulletCounts<=0) {
var gameOver = new Audio('./sound/gameOver.mp3');
gameOver.play();
setTimeout(()=>{
window.alert("No missile left");
window.location.reload();
},4000);
}
var moveBullets = setInterval( ()=> {
var aliens = document.getElementsByClassName('alien');
for(var i=0;i<aliens.length;i++) {
var alien = aliens[i];
var alienBound = alien.getBoundingClientRect();
var bulletBound = bullet.getBoundingClientRect();
if(bulletBound.left>=alienBound.left&&
bulletBound.right<=alienBound.right&&
bulletBound.top<=alienBound.top&&
bulletBound.bottom<=alienBound.bottom) {
var alien_end = new Audio('./sound/alien_end.mp3');
alien_end.play();
alien.parentElement.removeChild(alien);
bullet.parentElement.removeChild(bullet);
score++;
alienCount--;
document.getElementById('display-score').innerText=score;
document.getElementById('display-alien').innerText=alienCount;
}
}
var bulletBottom = parseInt(window.getComputedStyle(bullet).getPropertyValue("bottom"));
//Stops the bullet from moving outside the gamebox
if (bulletBottom >= 1000) {
clearInterval(moveBullets);
}
bullet.style.left = left + 10 +"px";
bullet.style.bottom = bulletBottom + 3 + "px";
});
}
});
document.getElementById('start').addEventListener('click', function() {
document.getElementById('message').style.display = "none";
play=true;
})
var generateAliens = setInterval( ()=>{
if(play===true&&alienCount>=0) {
var alien = document.createElement("div");
alien.classList.add("alien");
// alienCount--;
if(alienCount==0) {
play=false;
var audio = new Audio('./sound/KidsCheering.mp3');
audio.play();
setTimeout(()=>{
alert("You Won!");
window.location.reload();
},4000);
alienCount--;
}
var alienleft = parseInt(window.getComputedStyle(alien).getPropertyValue("left"));
alien.style.left = Math.floor(Math.random()*(boardWidth-60)) + "px";
board.appendChild(alien);
}
},1500);
var moveAlien = setInterval( ()=> {
var aliens = document.getElementsByClassName('alien');
if(aliens!=undefined && play===true) {
for(var i=0;i<aliens.length;i++) {
var alien = aliens[i];
var alienTop = parseInt(window.getComputedStyle(alien).getPropertyValue("top"));
if(alienTop>=boardHeight-60) {
play=false;
var gameOver = new Audio('./sound/gameOver.mp3');
gameOver.play();
clearInterval(moveAlien);
setTimeout(()=>{
window.alert("Game Over");
window.location.reload();
},5000);
}
alien.style.top = alienTop + 20 + "px";
}
}
},750);
var fireButton = document.getElementById('fire');
var leftButton = document.getElementById('left');
var rightButton = document.getElementById('right');
fireButton.addEventListener('click',()=>{
if(bulletCounts>0 && play===true && alienCount>0) {
var left = parseInt(window.getComputedStyle(rocket).getPropertyValue("left"));
var bullet_sound = new Audio('./sound/alien_end.mp3');
bullet_sound.play();
var bullet = document.createElement("div");
bullet.classList.add("bullets");
board.appendChild(bullet);
bulletCounts--;
document.getElementById('display-bullet').innerText=bulletCounts;
if(bulletCounts<=0) {
var gameOver = new Audio('./sound/gameOver.mp3');
gameOver.play();
setTimeout(()=>{
window.alert("No missile left");
window.location.reload();
},5000);
}
var moveBullets = setInterval( ()=> {
var aliens = document.getElementsByClassName('alien');
for(var i=0;i<aliens.length;i++) {
var alien = aliens[i];
var alienBound = alien.getBoundingClientRect();
var bulletBound = bullet.getBoundingClientRect();
if(bulletBound.left>=alienBound.left&&
bulletBound.right<=alienBound.right&&
bulletBound.top<=alienBound.top&&
bulletBound.bottom<=alienBound.bottom) {
var alien_end = new Audio('./sound/alien_end.mp3');
alien_end.play();
alien.parentElement.removeChild(alien);
bullet.parentElement.removeChild(bullet);
score++;
alienCount--;
document.getElementById('display-score').innerText=score;
document.getElementById('display-alien').innerText=alienCount;
}
}
var bulletBottom = parseInt(window.getComputedStyle(bullet).getPropertyValue("bottom"));
//Stops the bullet from moving outside the gamebox
if (bulletBottom >= 1000) {
clearInterval(moveBullets);
}
bullet.style.left = left + 10 +"px";
bullet.style.bottom = bulletBottom + 3 + "px";
});
}
});
leftButton.addEventListener('click',()=>{
var left = parseInt(window.getComputedStyle(rocket).getPropertyValue("left"));
if(left>0) {
rocket.style.left = left - 15 + "px";
}
});
rightButton.addEventListener('click',()=>{
var left = parseInt(window.getComputedStyle(rocket).getPropertyValue("left"));
if(left<=boardWidth-36) {
rocket.style.left = left + 15 + "px";
}
})