Skip to content

Commit bccde1e

Browse files
authored
Merge pull request #11 from NotStonee/testing
Testing
2 parents 28bea73 + a7a3a04 commit bccde1e

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

script.js

+39-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
var score = 0;
33
var lives = 5;
44
var gameOver = false;
5+
function update() {
6+
// update game state
7+
// draw game
8+
requestAnimationFrame(update);
9+
}
510

611
const canvas = document.getElementById('game');
712
const context = canvas.getContext('2d');
@@ -203,6 +208,12 @@ const canvas = document.getElementById('game');
203208
// draw paddle
204209
context.fillStyle = 'white';
205210
context.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
211+
212+
if (bricks.length == 0) {
213+
lives +=6
214+
resetBricks();
215+
ball.y = canvas.height+100
216+
}
206217
}
207218

208219
function resetBricks() {
@@ -224,25 +235,31 @@ const canvas = document.getElementById('game');
224235
}
225236
}
226237
}
227-
// listen to keyboard events to move the paddle
228-
document.addEventListener('keydown', function(e) {
229-
// left arrow key
230-
if (e.which == 65 && gameOver == false) {
231-
paddle.dx = -7;
232-
}
233238

234-
if (e.which == 37 && gameOver == false) {
235-
paddle.dx = -7;
236-
}
237-
// right arrow key
238-
if (e.which == 68 && gameOver == false) {
239-
paddle.dx = 7;
240-
}
239+
let brickCount = bricks.length;
241240

242-
if (e.which == 39 && gameOver == false) {
243-
paddle.dx = 7;
244-
}
241+
245242

243+
document.addEventListener('keydown', function(e) {
244+
if (gameOver) return;
245+
246+
switch (e.which) {
247+
case 37: // left arrow key
248+
case 65: // 'A' key
249+
paddle.dx = -7;
250+
requestAnimationFrame(update);
251+
break;
252+
case 39: // right arrow key
253+
case 68: // 'D' key
254+
paddle.dx = 7;
255+
requestAnimationFrame(update);
256+
break;
257+
case 32: //spacebar
258+
event.preventDefault();
259+
break;
260+
}
261+
262+
document.addEventListener('keydown', function(e) {
246263
if (e.which == 82 && gameOver == true) {
247264
lives = 5;
248265
score = 0;
@@ -251,6 +268,7 @@ const canvas = document.getElementById('game');
251268
paddle.x = canvas.width / 2 - brickWidth / 2;
252269
paddle.y = 440;
253270
}
271+
});
254272

255273
// space key
256274
// if they ball is not moving, we can launch the ball using the space key. ball
@@ -264,19 +282,14 @@ const canvas = document.getElementById('game');
264282
}
265283
});
266284

267-
// listen to keyboard events to stop the paddle if key is released
268-
document.addEventListener('keyup', function(e) {
269-
if (e.which == 65 || e.which == 37) {
285+
// listen to keyboard events to stop the paddle if key is released
286+
document.addEventListener('keyup', function(e) {
287+
if (e.which == 65 || e.which == 37 || e.which == 68 || e.which == 39) {
270288
paddle.dx = 0;
289+
requestAnimationFrame(update);
271290
}
272291
});
273292

274-
document.addEventListener('keyup', function(e) {
275-
if(e.which == 68 || e.which == 39) {
276-
paddle.dx = 0
277-
}
278-
279-
})
280293

281294
function text(txt, fnt, x, y, c) {
282295
context.fillStyle = c;
@@ -285,5 +298,6 @@ const canvas = document.getElementById('game');
285298

286299
}
287300

301+
288302
// start the game
289303
requestAnimationFrame(loop);

0 commit comments

Comments
 (0)