-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
268 lines (233 loc) · 6.81 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
let startBtn = document.querySelector(".start");
let restartBtn = document.querySelector(".restart");
let box = document.querySelector(".box");
let canvas = document.querySelector(".board");
let tool = canvas.getContext("2d");
let scoreElemrnt = document.querySelector("span");
let powerlevel = document.querySelector(".meter span")
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
// load earthimg
let score =0;
let fullpower =100;
let spaceImg = new Image();
spaceImg.src = "space (1).jpg";
let earthImg = new Image();
earthImg.src = "earth1.png";
let coronaImg = new Image();
coronaImg.src = "corona.png";
let eheight = 40;
let eWidth = 40;
let ePosX = canvas.width / 2 - 20;
let ePosY = canvas.height / 2 - 20;
class Corona {
constructor(x, y, width, height, velocity) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.velocity = velocity;
}
draw() {
tool.drawImage(coronaImg, this.x, this.y, this.width, this.height);
}
update() {
this.draw();
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
}
}
class Bullet {
constructor(x, y, width, height, velocity) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.velocity = velocity;
}
draw() {
tool.fillStyle = "white";
tool.fillRect(this.x, this.y, this.width, this.height)
}
update() {
this.draw();
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
}
}
class Planet {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
draw() {
//draw krenge
tool.drawImage(earthImg, this.x, this.y, this.width, this.height);
}
}
class Particle {
constructor(x, y, radius, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.velocity = velocity;
this.alpha = 1;
}
draw() {
tool.save();
tool.globalAlpha = this.alpha;
tool.beginPath();
tool.fillStyle = "white";
tool.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
tool.fill();
tool.restore();
}
update() {
this.draw();
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
this.alpha -= 0.01;
}
}
let bullets = [
];
let coronas = [
];
let particles = [
];
let animId;
function animate() {
tool.clearRect(0, 0, canvas.width, canvas.height);
tool.fillRect(0, 0, canvas.width, canvas.height);
//space draw
tool.drawImage(spaceImg, 0, 0, canvas.width, canvas.height);
let earth = new Planet(ePosX, ePosY, eWidth, eheight);
earth.draw();
//particle update
particles.forEach(function (particle, index) {
if( particle.alpha<=0){
setTimeout(function(){
particles.splice(index,1);
},0);
}
else{
particle.update();
}
})
// for updating bullets
let blength = bullets.length
for (let i = 0; i < blength; i++) {
bullets[i].update();
if (bullets[i].x < 0 || bullets[i].y < 0 || bullets[i].x > canvas.width || bullets[i].y > canvas.height) {
setTimeout(function () {
bullets.splice(i, 1);
})
}
}
// for corona
coronas.forEach(function(corona, i){
corona.update();
// collison code with earth
let enemy = corona;
if (colRec(earth, enemy)) {
fullpower-=10;
powerlevel.style.width =`${fullpower}%`;
coronas.splice(i,1);
if(fullpower==0){
cancelAnimationFrame(animId);
// alert("Game Over");
restart();
}
}
// collison of bullet and corona
bullets.forEach(function (bullet, bulletidx) {
if (colRec(enemy, bullet)) {
//explosion
for (let i = 0; i < enemy.width * 4; i++) {
particles.push(new Particle(bullet.x, bullet.y, Math.random() * 2, {
x: (Math.random() - 0.5) * (Math.random() * 5),
y: (Math.random() - 0.5) * (Math.random() * 5)
}))
}
setTimeout(() => {
coronas.splice(i, 1);
bullets.splice(bulletidx, 1);
score++;
scoreElemrnt.innerText= score;
}, 0)
}
})
})
animId = requestAnimationFrame(animate);
}
function createCorona() {
setInterval(function () {
let x = Math.random() * canvas.width;
let y = Math.random() * canvas.height;
let delta = Math.random();
if (delta < 0.5) {
//horizontal
x = Math.random()< 0.5 ? 0 : canvas.width;
y = Math.random() * canvas.height;
} else {
x = Math.random() * canvas.width;
y = Math.random() < 0.5 ? 0 : canvas.height;
}
let angle = Math.atan2(canvas.height / 2 - y, canvas.width / 2 - x);
let velocity = {
x: Math.cos(angle) * 1.5,
y: Math.sin(angle) * 1.5
}
let corona = new Corona(x, y, 40, 40, velocity);
coronas.push(corona);
}, 1000)
}
startBtn.addEventListener("click", function (e) {
e.stopImmediatePropagation();
//box ko hide
box.style.display = "none";
//space load kro
animate();
createCorona();
window.addEventListener("click", function (e) {
console.log(e);
let angle = Math.atan2(e.clientY - canvas.height / 2, e.clientX - canvas.width / 2);
let velocity = {
x: Math.cos(angle) * 3,
y: Math.sin(angle) * 3,
}
let bullet = new Bullet(canvas.width / 2, canvas.height / 2, 7, 7, velocity);
bullet.draw();
bullets.push(bullet);
})
})
function colRec(entity1, entity2) {
let l1 = entity1.x;
let l2 = entity2.x;
let r1 = entity1.x + entity1.width;
let r2 = entity2.x + entity2.width;
let t1 = entity1.y + entity1.height;
let t2 = entity2.y + entity2.height;
let b1 = entity1.y;
let b2 = entity2.y;
if (l1 < r2 && l2 < r1 && t1 > b2 && t2 > b1) {
return true;
}
return false;
}
window.addEventListener("resize",function(){
window.location.reload();
})
function restart(){
restartBtn.style.display="block";
startBtn.style.display="none";
box.style.display="flex";
powerlevel.parentElement.style.display="none";
document.body.style.backgroundColor ="white";
canvas.height="0px";
restartBtn.addEventListener("click",function(){
window.location.reload();
})
}