-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballblast.js
More file actions
493 lines (375 loc) · 12.1 KB
/
ballblast.js
File metadata and controls
493 lines (375 loc) · 12.1 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//canvas variables
var canvas = document.getElementById('gameCanvas');
var canvasContext = canvas.getContext('2d');
const backgroundGradient = canvasContext.createLinearGradient(0,80,0,canvas.height);
backgroundGradient.addColorStop(0,'#171e26');
backgroundGradient.addColorStop(1,'#3f586b');
var backgroundStars = [];
var groundHeight = 70;
var gameOver = false;
var message = document.getElementsByClassName('messageDiv')[0];
//function to draw the canvas
function createMountainRange(mountainAmount,height,colour){
for( let i=0 ; i< mountainAmount ; i++ ){
const mountainWidth = canvas.width / mountainAmount ;
canvasContext.beginPath();
canvasContext.moveTo(i * mountainWidth,canvas.height);
canvasContext.lineTo(i * mountainWidth + mountainWidth + 325,canvas.height);
canvasContext.lineTo(i * mountainWidth + mountainWidth / 2,canvas.height - height);
canvasContext.lineTo(i * mountainWidth - 325,canvas.height);
canvasContext.fillStyle = colour ;
canvasContext.fill();
canvasContext.closePath();
}
}
//used for creating the stars in the canvas
function stars(x,y,radius,colours){
this.x = x;
this.y = y;
this.radius = radius;
this.colour = colours;
this.drawStar = function(){
canvasContext.beginPath();
canvasContext.arc(this.x,this.y,this.radius,0,Math.PI*2,false);
canvasContext.fillStyle= this.colour;
canvasContext.fill();
canvasContext.closePath();
};
}
function createStar(){
for (let i = 0; i < 150; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const radius = Math.random() * 3;
backgroundStars.push(new stars(x,y,radius,'white'));
}
}
function drawStars(){
for (let i = 0; i < 150; i++) {
backgroundStars[i].drawStar();
}
}
//player scores
var playerScore = 0;
var highScore = 0;
var displayHighScore = document.getElementsByTagName('h3')[1];
//function to update playerscore for every frame
function updatePlayerScore(){
canvasContext.beginPath();
canvasContext.font = "20px Assistant";
var displayPlayerScore = "Your score :" + playerScore;
canvasContext.fillStyle = ' #00FF00';
canvasContext.fillText(displayPlayerScore,canvas.width - 150,canvas.height- 40);
}
function updateHighScore(){
if(localStorage.getItem('highScore') != null){
highScore =Number(localStorage.getItem('highScore'));
}
if(playerScore > highScore){
highScore = playerScore;
localStorage.setItem('highScore',JSON.stringify(highScore));
}
}
function HighScore(){
displayHighScore.innerHTML = "High Score :" + highScore;
}
/* if(gameOver){
updateHighScore();
} */
//Paddle variables
var cannonImage = new Image;
cannonImage.src = 'images/cannon.jpg'
var paddleWidth = 50;
var paddleHeight = 60;
var paddleX = (canvas.width-paddleWidth)/2;
var paddleY = canvas.height-paddleHeight - groundHeight;
var paddleSpeed=8;
var leftPressed = false;
var rightPressed = false;
//bullet variables
var bulletImage = new Image();
bulletImage.src = 'images/bullet.jpg' ;
var framesPerSecond=100;
var bulletArray = [];
function bullet(){
//position
this.x = paddleX + paddleWidth /2 -7;
this.y = paddleY- 5;
this.radius = 3;
this.init = function(){
bulletArray.push(this);
};
this.draw = function(){
canvasContext.drawImage(bulletImage,this.x,this.y,10,15);
};
this.update = function(){
this.y += -10;
};
}
function createBullet(){
var bullets = new bullet();
bullets.init();
//increase the speed of bullet with respect to player score
framesPerSecond = 100 - (0.9 *playerScore);
if(framesPerSecond < 50)
framesPerSecond = 50;
}
function updateBulletPos(){
for (var i = bulletArray.length; i--;) {
bulletArray[i].update();
if(bulletArray[i].y < 0)
bulletArray.splice(i, 1);
}
}
function drawBullet(){
for (var i = bulletArray.length; i--;) {
bulletArray[i].draw();
}
}
//function to create random numbers between min and max
function randomIntFromRange(min,max){
return Math.floor(Math.random() * (max - min + 1) + min);
}
//function to create random colours
var coloursArray = ['#929292','#90EE90','#BDB76B','#AFEEEE','#00008B','#708090','#FFC0CB'] ;
function createRandomcolours(){
return coloursArray[randomIntFromRange(0,coloursArray.length)];
}
//Rocks variables
var rockArray = [];
function rock(x,y,speedX,speedY,radius,colour,parent){
this.x = x;
this.y = y;
this.radius = radius;
this.strength = Math.floor(this.radius);
this.colour = colour;
this.parent = parent;
this.speedX = speedX;
this.speedY = speedY;
// this.friction = 0.8 ;
this.gravity = 0.05;
this.drawRock = function(){
canvasContext.beginPath();
canvasContext.arc(this.x,this.y,this.radius,0,Math.PI*2,false);
canvasContext.fillStyle= this.colour;
canvasContext.fill();
canvasContext.beginPath();
canvasContext.font = "15px Assistant";
canvasContext.fillStyle = 'white';
canvasContext.textAlign = "center";
canvasContext.textBaseline = 'middle';
canvasContext.fillText(this.strength, this.x, this.y) ;
canvasContext.fill();
canvasContext.closePath();
};
this.update = function(){
if(this.y+this.radius + this.speedY >canvas.height - groundHeight || this.y - this.radius<0){
this.speedY = -this.speedY ;
}else{
this.speedY += this.gravity;
}
if(this.x + this.radius> canvas.width || this.x - this.radius< 0){
this.speedX = -this.speedX;
}
if((this.x >paddleX) && (this.x < paddleX + paddleWidth) && (this.y + this.radius > paddleY) && (this.y + this.radius < paddleY + paddleHeight)){
gameOver = true ;
}
this.x += this.speedX;
this.y += this.speedY;
};
}
function drawRocks(){
for (var i = rockArray.length; i--;) {
rockArray[i].drawRock();
}
}
function updateRockPos() {
for (var i = rockArray.length; i--;) {
rockArray[i].update();
}
}
var ticker= 0;
var rockSpawnRate = 8000;
function createRock(){
if(ticker % 2 ==0){
const x = canvas.width -70;
const y = canvas.height / 2 -100;
const radius = randomIntFromRange(40,60);
const speedX = randomIntFromRange(-3,-1);
const speedY = randomIntFromRange(-2, 2);
const colour = createRandomcolours();
rockArray.push(new rock(x,y,speedX,speedY,radius,colour,true)) ;
}
else {
const x = 70;
const y = canvas.height / 2 - 100;
const radius = randomIntFromRange(30,40);
const speedX = randomIntFromRange(1,3);
const speedY = randomIntFromRange(-2, 2);
const colour = createRandomcolours();
rockArray.push(new rock(x,y,speedX,speedY,radius,colour,true));
}
rockSpawnRate = 10000 - (5 *playerScore);
if(rockSpawnRate < 4000 )
rockSpawnRate = 10000;
}
//collision detection
function collision(bullet,bulletIndex){
for(let i = 0; i < rockArray.length; i++){
var rocks = rockArray[i];
var strength = rocks.strength;
if(rocks.radius + bullet.radius >= (Math.hypot(rocks.x - bullet.x, rocks.y - bullet.y))){
//When it is obtained from some other rock destroy it on zero value
if(rocks.strength == 1 && rocks.parent == false){
bulletArray.splice(bulletIndex, 1);
rockArray.splice(i, 1);
playerScore += 1;
}
//if the rock is a new rock it splits into two new rocks if the radius is more then 40
else if(rocks.strength == 1 && rocks.parent == true ){
if(rocks.radius> 40){
bulletArray.splice(bulletIndex, 1);
rockArray.push(new rock(rocks.x, rocks.y, rocks.speedX, -rocks.speedY, rocks.radius/2, rocks.colour, false));
rockArray.push(new rock(rocks.x, rocks.y, -rocks.speedX, -rocks.speedY, rocks.radius - (rocks.radius/2), rocks.colour, false));
rockArray.splice(i, 1);
playerScore += 1;
}
//when the radius of the rock is less than 40 the rock is destroyed
else{
bulletArray.splice(bulletIndex,1);
rockArray.splice(i,1);
playerScore += 1;
}
}
//Reduce the strength by 1 on bullet contact
else{
rocks.strength -= 1;
bulletArray.splice(bulletIndex, 1);
playerScore += 1;
}
}
}
}
//buttons
var highScoreButton=document.getElementById('highscore');
var reset=document.getElementById('reset');
var pause = false;
//to pause the game
function togglePause(){
if(!pause){
pause = true;
}
else if(pause){
pause = false;
}
}
document.addEventListener('keydown',function(evt){
if (evt.keyCode === 80){
togglePause();
}
})
//rest the game
reset.addEventListener("click",function(){
document.location.reload();
localStorage.clear();
})
//to display highScore
var highscores = document.getElementsByClassName('highScores')[0];
highScoreButton.addEventListener("click",function(){
updateHighScore();
highscores.style.display = "block";
HighScore();
})
//when the arrow keys or a,d key is leftPressed
function keyDownHandler(evt){
if(evt.key == "Right" || evt.key == "ArrowRight" || evt.keyCode == 68)
rightPressed = true;
else if(evt.key == "Left" || evt.key == "ArrowLeft" || evt.keyCode == 65)
leftPressed = true;
}
function keyUpHandler(evt){
if(evt.key == "Right" || evt.key == "ArrowRight" || evt.keyCode == 68)
rightPressed = false;
else if(evt.key == "Left" || evt.key == "ArrowLeft" || evt.keyCode == 65)
leftPressed = false;
}
document.addEventListener('keydown',keyDownHandler);
document.addEventListener('keyup',keyUpHandler);
//moves everything in the canvas
function moveEverything(){
//paddle movement
if(rightPressed && paddleX < canvas.width - paddleWidth)
paddleX += paddleSpeed;
else if(leftPressed && paddleX > 0)
paddleX -= paddleSpeed;
//bullet movement
updateBulletPos();
//rock movement
updateRockPos();
//displays player score
/* if(collision){
updatePlayerScore();
} */
//collision detection
for(let j = 0; j < bulletArray.length; j++)
{
collision(bulletArray[j], j);
}
}
//function to draw rectangles in canvas
function colourRect(leftX,topY,width,height,drawColour){
canvasContext.fillStyle= drawColour;
canvasContext.fillRect(leftX,topY,width,height);
}
//draws everything in the canvas
function drawEverything(){
canvasContext.clearRect(0,0,canvas.width,canvas.height);
//draws the canvas
colourRect(0,0,canvas.width,canvas.height,backgroundGradient);
drawStars();
createMountainRange(1,canvas.height - 50,'#384551');
createMountainRange(2,canvas.height - 100,'#2B3843');
createMountainRange(3,canvas.height - 300,'#26333E');
colourRect(0,canvas.height - groundHeight,canvas.width,groundHeight,'#182028');
//draws the bullets
drawBullet();
//draws the Rocks
drawRocks();
//display player scores
updatePlayerScore();
//draws the paddle
canvasContext.drawImage(cannonImage,paddleX,paddleY,paddleWidth,paddleHeight);
}
//function to display message when the rock collides with cannon
var messageh3 = document.getElementsByTagName('h3')[3];
function gameOverMessage(){
messageh3.innerHTML += playerScore;
}
//function to restart game after space is pressed
document.addEventListener("keydown",function(evt){
if(gameOver == true && evt.keyCode ==32){
document.location.reload();
}
})
//renders the canvas frame when the gameLoop function is called
function gameLoop(){
if(!gameOver){
if(!pause){
moveEverything();
drawEverything();
}
ticker++;
window.requestAnimationFrame(gameLoop);
}
else{
canvasContext.clearRect(0,0,canvas.width,canvas.height);
message.style.display = "block";
gameOverMessage();
}
}
if(!pause){
setInterval(createBullet,framesPerSecond);
setInterval(createRock, rockSpawnRate);
createStar();
}
gameLoop();