@@ -19,10 +19,6 @@ function text(txt, fnt, x, y, c) {
19
19
context . fillText ( txt , x , y ) ;
20
20
}
21
21
22
- function update ( ) {
23
- requestAnimationFrame ( update ) ;
24
- }
25
-
26
22
const storedHighScore = localStorage . getItem ( 'highScore' ) ;
27
23
if ( storedHighScore ) {
28
24
highScore = storedHighScore ;
@@ -64,7 +60,6 @@ const brickWidth = 25;
64
60
const brickHeight = 12 ;
65
61
const wallSize = 12 ;
66
62
const bricks = [ ] ;
67
- const playerLength = 35 ;
68
63
for ( let row = 0 ; row < level1 . length ; row ++ ) {
69
64
for ( let col = 0 ; col < level1 [ row ] . length ; col ++ ) {
70
65
const colorCode = level1 [ row ] [ col ] ;
@@ -108,6 +103,9 @@ function drawBorder() {
108
103
context . fillRect ( canvas . width - wallSize , 0 , wallSize , canvas . height ) ;
109
104
}
110
105
106
+ const playerLength = 35 ;
107
+ var moveLeft = false ;
108
+ var moveRight = false ;
111
109
const player = {
112
110
x : canvas . width / 2 - playerLength / 2 ,
113
111
y : 440 ,
@@ -141,11 +139,11 @@ document.addEventListener('keydown', function(e) {
141
139
switch ( e . which ) {
142
140
case 37 : // left arrow key
143
141
case 65 : // 'A' key
144
- player . dx = - 7 ;
142
+ moveLeft = true ;
145
143
break ;
146
144
case 39 : // right arrow key
147
145
case 68 : // 'D' key
148
- player . dx = 7 ;
146
+ moveRight = true ;
149
147
break ;
150
148
case 32 : //spacebar
151
149
event . preventDefault ( ) ;
@@ -171,13 +169,25 @@ document.addEventListener('keyup', function(e) {
171
169
switch ( e . which ) {
172
170
case 65 :
173
171
case 37 :
172
+ moveLeft = false ;
173
+ break ;
174
174
case 68 :
175
175
case 39 :
176
- player . dx = 0 ;
176
+ moveRight = false ;
177
177
break ;
178
178
}
179
179
} ) ;
180
180
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
+
181
191
182
192
183
193
//(AABB)
@@ -264,6 +274,7 @@ function loop() {
264
274
drawBall ( ) ;
265
275
drawBricks ( ) ;
266
276
drawPlayer ( ) ;
277
+ movement ( ) ;
267
278
if ( bricks . length == 0 ) {
268
279
lives += 5 ;
269
280
resetBricks ( ) ;
0 commit comments