1
1
var chance = Math . random ( ) * 11 ;
2
2
var score = 0 ;
3
- var lives = 5 ;
3
+ var lives = '5' ;
4
4
var gameOver = false ;
5
5
var highScore = 0 ;
6
6
var bonusCounter = 0 ;
7
7
8
- function bonus ( ) {
9
- bonusCounter ++ ;
10
- if ( bonusCounter == 20 ) {
11
- lives ++
12
- bonusCounter = 0 ;
13
- }
14
- }
15
-
16
8
function text ( txt , fnt , x , y , c ) {
17
9
context . fillStyle = c ;
18
10
context . font = fnt ;
@@ -119,20 +111,74 @@ function drawPlayer() {
119
111
context . fillRect ( player . x , player . y , player . width , player . height ) ;
120
112
}
121
113
114
+ var balls = [ ] ;
115
+
122
116
const ball = {
123
117
x : Math . random ( ) * 401 ,
124
118
y : 260 ,
125
119
width : 10 ,
126
120
height : 10 ,
127
121
speed : 3.5 ,
128
122
dx : 0 ,
129
- dy : 0
123
+ dy : 0 } ;
124
+
125
+ function ballDirection ( ) {
126
+ if ( Math . random ( ) * 11 > 5 ) {
127
+ return ball . speed ;
128
+ } else return - ball . speed ;
129
+ }
130
+
131
+ function makeBall ( ) {
132
+ balls . push ( {
133
+ x : player . x + player . dx ,
134
+ y : player . y - player . height - ball . height ,
135
+ width : 10 ,
136
+ height : 10 ,
137
+ dx : ballDirection ( ) ,
138
+ dy : - ball . speed }
139
+ ) ;
140
+ }
141
+
142
+ function drawBalls ( ) {
143
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
144
+ let ball = balls [ i ] ;
145
+ if ( ball . dx || ball . dy ) {
146
+ context . fillRect ( ball . x , ball . y , ball . width , ball . height ) ;
147
+ }
148
+ }
130
149
} ;
131
150
132
151
function drawBall ( ) {
133
- if ( ball . dx || ball . dy ) {
152
+ if ( ball . dx || ball . dy ) {
134
153
context . fillRect ( ball . x , ball . y , ball . width , ball . height ) ;
135
154
}
155
+ } ;
156
+
157
+
158
+
159
+ function moveBalls ( ) {
160
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
161
+ let ball = balls [ i ] ;
162
+ ball . x += ball . dx ;
163
+ ball . y += ball . dy ;
164
+ }
165
+ } ;
166
+
167
+ function moveball ( ) {
168
+ ball . x += ball . dx ;
169
+ ball . y += ball . dy ;
170
+ } ;
171
+
172
+ function bonus ( ) {
173
+ bonusCounter ++ ;
174
+ if ( bonusCounter == 15 ) {
175
+ if ( lives < 999 ) {
176
+ lives ++ ;
177
+ lives = lives . toString ( ) ;
178
+ }
179
+ bonusCounter = 0 ;
180
+ makeBall ( ) ;
181
+ }
136
182
}
137
183
138
184
document . addEventListener ( 'keydown' , function ( e ) {
@@ -147,7 +193,7 @@ document.addEventListener('keydown', function(e) {
147
193
break ;
148
194
case 32 : //spacebar
149
195
event . preventDefault ( ) ;
150
- if ( gameOver == false && ball . dx == 0 && ball . dy == 0 ) {
196
+ if ( gameOver == false && ball . dx == 0 && ball . dy == 0 && balls . length == 0 ) {
151
197
ball . dx = ball . speed ;
152
198
ball . dy = ball . speed ;
153
199
if ( chance < 5 ) {
@@ -199,14 +245,21 @@ function collides(obj1, obj2) {
199
245
}
200
246
201
247
function loop ( ) {
248
+ console . log ( ball . dx && ball . dy ) ;
202
249
if ( score > highScore ) {
203
250
highScore = score ;
204
251
}
205
252
localStorage . setItem ( 'highScore' , highScore ) ;
206
253
requestAnimationFrame ( loop ) ;
207
254
context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
208
255
text ( 'Score: ' + score , '30px Cosmic Sans MS' , 20 , 35 , 'white' ) ;
209
- text ( 'Lives: ' + lives , '30px Cosmic Sans MS' , 280 , 35 , 'white' ) ;
256
+ if ( lives . length == 1 ) {
257
+ text ( 'Lives: ' + '00' + lives , '30px Cosmic Sans MS' , 260 , 35 , 'white' ) ;
258
+ } else if ( lives . length == 2 ) {
259
+ text ( 'Lives: ' + '0' + lives , '30px Cosmic Sans MS' , 260 , 35 , 'white' ) ;
260
+ } else {
261
+ text ( 'Lives: ' + lives , '30px Cosmic Sans MS' , 260 , 35 , 'white' ) ;
262
+ }
210
263
if ( gameOver ) {
211
264
player . dx = 0 ;
212
265
text ( 'Game Over' , '30px Cosmic Sans MS' , canvas . width / 2 - 60 , 340 , 'white' ) ;
@@ -220,63 +273,115 @@ function loop() {
220
273
} else if ( player . x + playerLength > canvas . width - wallSize ) {
221
274
player . x = canvas . width - wallSize - playerLength ;
222
275
}
223
-
224
- ball . x += ball . dx ;
225
- ball . y += ball . dy ;
226
-
276
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
277
+ let ball = balls [ i ] ;
227
278
if ( ball . x < wallSize ) {
228
279
ball . x = wallSize ;
229
280
ball . dx *= - 1 ;
230
281
} else if ( ball . x + ball . width > canvas . width - wallSize ) {
231
282
ball . x = canvas . width - wallSize - ball . width ;
232
283
ball . dx *= - 1 ;
233
284
}
234
-
285
+ }
286
+
287
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
288
+ let ball = balls [ i ] ;
289
+ if ( ball . y < wallSize ) {
290
+ ball . y = wallSize ;
291
+ ball . dy *= - 1 ;
292
+ }
293
+ }
294
+
235
295
if ( ball . y < wallSize ) {
236
296
ball . y = wallSize ;
237
297
ball . dy *= - 1 ;
238
298
}
239
299
300
+ if ( ball . x < wallSize ) {
301
+ ball . x = wallSize ;
302
+ ball . dx *= - 1 ;
303
+ } else if ( ball . x + ball . width > canvas . width - wallSize ) {
304
+ ball . x = canvas . width - wallSize - ball . width ;
305
+ ball . dx *= - 1 ;
306
+ }
307
+
240
308
if ( gameOver == false && ball . y > canvas . height ) {
241
309
ball . x = Math . random ( ) * 401 ;
242
310
ball . y = 260 ;
243
311
ball . dx = 0 ;
244
312
ball . dy = 0 ;
245
313
chance = Math . random ( ) * 11 ;
314
+ bonusCounter = 0 ;
246
315
lives -- ;
316
+ lives = lives . toString ( ) ;
247
317
if ( lives == 0 ) {
248
318
gameOver = true ;
249
319
}
250
320
}
251
321
322
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
323
+ let ball = balls [ i ]
324
+ if ( ball . y > canvas . height ) {
325
+ balls . splice ( i , 1 ) ;
326
+ }
327
+ }
328
+
329
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
330
+ let ball = balls [ i ] ;
252
331
if ( collides ( ball , player ) ) {
253
332
ball . dy *= - 1 ;
254
333
ball . y = player . y - ball . height ;
334
+ }
255
335
}
256
336
257
- for ( let i = 0 ; i < bricks . length ; i ++ ) {
258
- const brick = bricks [ i ] ;
259
- if ( collides ( ball , brick ) ) {
260
- bricks . splice ( i , 1 ) ;
261
- score ++ ;
262
- bonus ( ) ;
263
- if ( ball . y + ball . height - ball . speed <= brick . y ||
264
- ball . y >= brick . y + brick . height - ball . speed ) {
265
- ball . dy *= - 1 ;
266
- } else {
267
- ball . dx *= - 1 ;
337
+ if ( collides ( ball , player ) ) {
338
+ ball . dy *= - 1 ;
339
+ ball . y = player . y - ball . height ;
340
+ }
341
+
342
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
343
+ let ball = balls [ i ] ;
344
+ for ( let j = 0 ; j < bricks . length ; j ++ ) {
345
+ let brick = bricks [ j ] ;
346
+ if ( collides ( ball , brick ) ) {
347
+ bricks . splice ( j , 1 ) ;
348
+ score ++ ;
349
+ bonus ( ) ;
350
+ if ( ball . y + ball . height - ball . speed <= brick . y || ball . y >= brick . y + brick . height - ball . speed ) {
351
+ ball . dx *= - 1 ;
352
+ } else {
353
+ ball . dy *= - 1 ;
354
+ }
268
355
}
269
- break ;
270
356
}
271
357
}
358
+
359
+ for ( let j = 0 ; j < bricks . length ; j ++ ) {
360
+ let brick = bricks [ j ] ;
361
+ if ( collides ( ball , brick ) ) {
362
+ bricks . splice ( j , 1 ) ;
363
+ score ++ ;
364
+ bonus ( ) ;
365
+ if ( ball . y + ball . height - ball . speed <= brick . y || ball . y >= brick . y + brick . height - ball . speed ) {
366
+ ball . dy *= - 1 ;
367
+ } else {
368
+ ball . dx *= - 1 ;
369
+ }
370
+ }
371
+ }
272
372
273
373
drawBorder ( ) ;
374
+ drawBalls ( ) ;
274
375
drawBall ( ) ;
376
+ moveBalls ( ) ;
377
+ moveball ( ) ;
275
378
drawBricks ( ) ;
276
379
drawPlayer ( ) ;
277
380
movement ( ) ;
278
381
if ( bricks . length == 0 ) {
279
- lives += 5 ;
382
+ for ( let i = 0 ; i < 5 ; i ++ ) {
383
+ lives ++
384
+ }
280
385
resetBricks ( ) ;
281
386
}
282
387
}
0 commit comments