Skip to content

Commit 5839271

Browse files
authored
Merge pull request #21 from NotStonee/testing
multiple balls
2 parents 21c242d + f375e72 commit 5839271

File tree

1 file changed

+136
-31
lines changed

1 file changed

+136
-31
lines changed

script.js

+136-31
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
var chance = Math.random() * 11;
22
var score = 0;
3-
var lives = 5;
3+
var lives = '5';
44
var gameOver = false;
55
var highScore = 0;
66
var bonusCounter = 0;
77

8-
function bonus() {
9-
bonusCounter++;
10-
if (bonusCounter == 20) {
11-
lives++
12-
bonusCounter = 0;
13-
}
14-
}
15-
168
function text(txt, fnt, x, y, c) {
179
context.fillStyle = c;
1810
context.font = fnt;
@@ -119,20 +111,74 @@ function drawPlayer() {
119111
context.fillRect(player.x, player.y, player.width, player.height);
120112
}
121113

114+
var balls = [];
115+
122116
const ball = {
123117
x: Math.random() * 401,
124118
y: 260,
125119
width: 10,
126120
height: 10,
127121
speed: 3.5,
128122
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+
}
130149
};
131150

132151
function drawBall() {
133-
if (ball.dx || ball.dy) {
152+
if (ball.dx || ball.dy) {
134153
context.fillRect(ball.x, ball.y, ball.width, ball.height);
135154
}
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+
}
136182
}
137183

138184
document.addEventListener('keydown', function(e) {
@@ -147,7 +193,7 @@ document.addEventListener('keydown', function(e) {
147193
break;
148194
case 32: //spacebar
149195
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) {
151197
ball.dx = ball.speed;
152198
ball.dy = ball.speed;
153199
if (chance < 5) {
@@ -199,14 +245,21 @@ function collides(obj1, obj2) {
199245
}
200246

201247
function loop() {
248+
console.log(ball.dx && ball.dy);
202249
if (score > highScore) {
203250
highScore = score;
204251
}
205252
localStorage.setItem('highScore', highScore);
206253
requestAnimationFrame(loop);
207254
context.clearRect(0, 0, canvas.width, canvas.height);
208255
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+
}
210263
if (gameOver) {
211264
player.dx = 0;
212265
text('Game Over', '30px Cosmic Sans MS', canvas.width / 2 - 60, 340, 'white');
@@ -220,63 +273,115 @@ function loop() {
220273
} else if (player.x + playerLength > canvas.width - wallSize) {
221274
player.x = canvas.width - wallSize - playerLength;
222275
}
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];
227278
if (ball.x < wallSize) {
228279
ball.x = wallSize;
229280
ball.dx *= -1;
230281
} else if (ball.x + ball.width > canvas.width - wallSize) {
231282
ball.x = canvas.width - wallSize - ball.width;
232283
ball.dx *= -1;
233284
}
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+
235295
if (ball.y < wallSize) {
236296
ball.y = wallSize;
237297
ball.dy *= -1;
238298
}
239299

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+
240308
if (gameOver == false && ball.y > canvas.height) {
241309
ball.x = Math.random() * 401;
242310
ball.y = 260;
243311
ball.dx = 0;
244312
ball.dy = 0;
245313
chance = Math.random() * 11;
314+
bonusCounter = 0;
246315
lives--;
316+
lives = lives.toString();
247317
if (lives == 0) {
248318
gameOver = true;
249319
}
250320
}
251321

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];
252331
if (collides(ball, player)) {
253332
ball.dy *= -1;
254333
ball.y = player.y - ball.height;
334+
}
255335
}
256336

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+
}
268355
}
269-
break;
270356
}
271357
}
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+
}
272372

273373
drawBorder();
374+
drawBalls();
274375
drawBall();
376+
moveBalls();
377+
moveball();
275378
drawBricks();
276379
drawPlayer();
277380
movement();
278381
if (bricks.length == 0) {
279-
lives += 5;
382+
for (let i = 0; i < 5; i++) {
383+
lives++
384+
}
280385
resetBricks();
281386
}
282387
}

0 commit comments

Comments
 (0)