Skip to content

Commit 2b3b512

Browse files
authored
Merge pull request #20 from NotStonee/testing
Testing
2 parents 8181ee1 + 4b35181 commit 2b3b512

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

script.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ function text(txt, fnt, x, y, c) {
1919
context.fillText(txt, x, y);
2020
}
2121

22-
function update() {
23-
requestAnimationFrame(update);
24-
}
25-
2622
const storedHighScore = localStorage.getItem('highScore');
2723
if (storedHighScore) {
2824
highScore = storedHighScore;
@@ -64,7 +60,6 @@ const brickWidth = 25;
6460
const brickHeight = 12;
6561
const wallSize = 12;
6662
const bricks = [];
67-
const playerLength = 35;
6863
for (let row = 0; row < level1.length; row++) {
6964
for (let col = 0; col < level1[row].length; col++) {
7065
const colorCode = level1[row][col];
@@ -108,6 +103,9 @@ function drawBorder() {
108103
context.fillRect(canvas.width - wallSize, 0, wallSize, canvas.height);
109104
}
110105

106+
const playerLength = 35;
107+
var moveLeft = false;
108+
var moveRight = false;
111109
const player = {
112110
x: canvas.width / 2 - playerLength / 2,
113111
y: 440,
@@ -141,11 +139,11 @@ document.addEventListener('keydown', function(e) {
141139
switch (e.which) {
142140
case 37: // left arrow key
143141
case 65: // 'A' key
144-
player.dx = -7;
142+
moveLeft = true;
145143
break;
146144
case 39: // right arrow key
147145
case 68: // 'D' key
148-
player.dx = 7;
146+
moveRight = true;
149147
break;
150148
case 32: //spacebar
151149
event.preventDefault();
@@ -171,13 +169,25 @@ document.addEventListener('keyup', function(e) {
171169
switch (e.which) {
172170
case 65:
173171
case 37:
172+
moveLeft = false;
173+
break;
174174
case 68:
175175
case 39:
176-
player.dx = 0;
176+
moveRight = false;
177177
break;
178178
}
179179
});
180180

181+
function movement() {
182+
if (moveLeft) {
183+
player.dx = -7;
184+
} else if (moveRight) {
185+
player.dx = 7;
186+
} else if (moveLeft == false && moveRight == false) {
187+
player.dx = 0;
188+
}
189+
}
190+
181191

182192

183193
//(AABB)
@@ -264,6 +274,7 @@ function loop() {
264274
drawBall();
265275
drawBricks();
266276
drawPlayer();
277+
movement();
267278
if (bricks.length == 0) {
268279
lives += 5;
269280
resetBricks();

0 commit comments

Comments
 (0)