@@ -7,6 +7,7 @@ var pauseBtn = document.getElementById("pause-btn");
7
7
var restartBtn = document . getElementById ( "restart-btn" ) ;
8
8
var animationId ;
9
9
var gameRunning = false ;
10
+ var ballColor = "#FFF" ;
10
11
11
12
startBtn . addEventListener ( "click" , function ( ) {
12
13
if ( ! gameRunning ) {
@@ -172,13 +173,13 @@ function draw() {
172
173
// Clear canvas
173
174
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
174
175
175
- ctx . fillStyle = "#FFF" ;
176
+ ctx . fillStyle = ballColor ;
176
177
ctx . font = "15px Arial" ;
177
178
178
179
ctx . beginPath ( ) ;
179
180
ctx . moveTo ( canvas . width / 2 , 0 ) ;
180
181
ctx . lineTo ( canvas . width / 2 , canvas . height ) ;
181
- ctx . strokeStyle = "#FFF" ; // Set line color to white
182
+ ctx . strokeStyle = ballColor ; // Set line color to white
182
183
ctx . stroke ( ) ;
183
184
ctx . closePath ( ) ;
184
185
@@ -344,11 +345,12 @@ var previousPositionRight = canvas.height / 2 - paddleHeight;
344
345
function actionStart ( e ) {
345
346
//e.preventDefault();
346
347
//判断是鼠标左键点击还是触摸
348
+ if ( ! gameRunning ) { return ; }
347
349
var leftPt = { pageX : canvas . width , pageY : 0 } , rightPt = { pageX : 0 , pageY : 0 } ;
348
350
349
- if ( e . touches && e . touches . length > 1 ) { //如果是触摸
351
+ if ( e . touches && e . touches . length > 1 ) { //如果是触摸
350
352
e . touches . forEach ( element => { //记录左半边,最左边的点
351
- if ( element . pageX - canvas . offsetLeft < canvas . width / 2 ) {
353
+ if ( element . pageX - canvas . offsetLeft < canvas . width / 2 ) {
352
354
leftPt = element . pageX < leftPt . pageX ? element : leftPt ;
353
355
} else { //记录右半边,最右边的点
354
356
rightPt = element . pageX > rightPt . pageX ? element : rightPt ;
@@ -357,14 +359,14 @@ function actionStart(e) {
357
359
if ( touchYstartLeft == null ) { touchYstartLeft = leftPt . pageY ; }
358
360
if ( touchYstartRight == null ) { touchYstartRight = rightPt . pageY ; }
359
361
return ;
360
- } else if ( e . touches && e . touches . length == 1 ) { //如果是单点触摸
362
+ } else if ( e . touches && e . touches . length == 1 ) { //如果是单点触摸
361
363
if ( e . touches [ 0 ] . pageX - canvas . offsetLeft < canvas . width / 2 ) {
362
364
if ( touchYstartLeft == null ) { touchYstartLeft = e . touches [ 0 ] . pageY ; }
363
365
} else {
364
366
if ( touchYstartRight == null ) { touchYstartRight = e . touches [ 0 ] . pageY ; }
365
367
}
366
- } else { //如果是鼠标 touchXstart等于鼠标的x坐标
367
- if ( e . pageX - canvas . offsetLeft < canvas . width / 2 ) {
368
+ } else { //如果是鼠标 touchXstart等于鼠标的x坐标
369
+ if ( e . pageX - canvas . offsetLeft < canvas . width / 2 ) {
368
370
if ( touchYstartLeft == null ) { touchYstartLeft = e . pageY ; }
369
371
} else {
370
372
if ( touchYstartRight == null ) { touchYstartRight = e . pageY ; }
@@ -388,61 +390,66 @@ function actionEnd(e) {
388
390
}
389
391
function actionMove ( e ) {
390
392
e . preventDefault ( ) ; // 防止页面滚动
391
-
392
- var distLeft , distRight ;
393
-
394
- if ( e . touches && e . touches . length > 1 ) { // 多点触摸
395
- let leftPt = e . touches [ 0 ] ;
396
- let rightPt = e . touches [ 0 ] ;
397
-
398
- for ( let i = 1 ; i < e . touches . length ; i ++ ) {
399
- if ( e . touches [ i ] . pageX < leftPt . pageX ) {
400
- leftPt = e . touches [ i ] ;
401
- }
402
- if ( e . touches [ i ] . pageX > rightPt . pageX ) {
403
- rightPt = e . touches [ i ] ;
393
+ if ( touchYstartRight !== null || touchYstartLeft !== null && gameRunning ) {
394
+ var distLeft , distRight ;
395
+ if ( e . touches && e . touches . length > 1 ) { // 多点触摸
396
+ //初始化为画布中间位置
397
+ let leftPt = { pageX :canvas . offsetLeft + canvas . width / 2 , pageY :0 } ;
398
+ let rightPt = { pageX :canvas . offsetLeft + canvas . width / 2 , pageY :0 } ;
399
+ ballColor = 'red' ;
400
+ for ( let i = 1 ; i < e . touches . length ; i ++ ) {
401
+ if ( e . touches [ i ] . pageX < leftPt . pageX ) {
402
+ leftPt = e . touches [ i ] ;
403
+
404
+ }
405
+ if ( e . touches [ i ] . pageX > rightPt . pageX ) {
406
+ rightPt = e . touches [ i ] ;
407
+
408
+ }
404
409
}
405
- }
406
-
407
- if ( touchYstartLeft !== null && leftPt . pageX - canvas . offsetLeft < canvas . width / 2 ) {
408
- distLeft = leftPt . pageY - touchYstartLeft ;
409
- leftPaddleY = previousPositionLeft + distLeft ;
410
- leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
411
- }
412
-
413
- if ( touchYstartRight !== null && rightPt . pageX - canvas . offsetLeft > canvas . width / 2 ) {
414
- distRight = rightPt . pageY - touchYstartRight ;
415
- rightPaddleY = previousPositionRight + distRight ;
416
- rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
417
- }
418
410
419
- } else if ( e . touches && e . touches . length == 1 ) { // 单点触摸
420
- let touch = e . touches [ 0 ] ;
421
- if ( touch . pageX - canvas . offsetLeft < canvas . width / 2 ) {
422
- if ( touchYstartLeft !== null ) {
423
- distLeft = touch . pageY - touchYstartLeft ;
411
+ if ( touchYstartLeft !== null && leftPt . pageX - canvas . offsetLeft < canvas . width / 2 ) {
412
+ distLeft = leftPt . pageY - touchYstartLeft ;
424
413
leftPaddleY = previousPositionLeft + distLeft ;
425
414
leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
415
+ ballColor = 'blue' ;
426
416
}
427
- } else {
428
- if ( touchYstartRight !== null ) {
429
- distRight = touch . pageY - touchYstartRight ;
417
+
418
+ if ( touchYstartRight !== null && rightPt . pageX - canvas . offsetLeft > canvas . width / 2 ) {
419
+ distRight = rightPt . pageY - touchYstartRight ;
430
420
rightPaddleY = previousPositionRight + distRight ;
431
421
rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
422
+ ballColor = 'green' ;
432
423
}
433
- }
434
- } else { // 鼠标移动
435
- if ( e . pageX - canvas . offsetLeft < canvas . width / 2 ) {
436
- if ( touchYstartLeft !== null ) {
437
- distLeft = e . pageY - touchYstartLeft ;
438
- leftPaddleY = previousPositionLeft + distLeft ;
439
- leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
424
+
425
+ } else if ( e . touches && e . touches . length == 1 ) { // 单点触摸
426
+ let touch = e . touches [ 0 ] ;
427
+ if ( touch . pageX - canvas . offsetLeft < canvas . width / 2 ) {
428
+ if ( touchYstartLeft !== null ) {
429
+ distLeft = touch . pageY - touchYstartLeft ;
430
+ leftPaddleY = previousPositionLeft + distLeft ;
431
+ leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
432
+ }
433
+ } else {
434
+ if ( touchYstartRight !== null ) {
435
+ distRight = touch . pageY - touchYstartRight ;
436
+ rightPaddleY = previousPositionRight + distRight ;
437
+ rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
438
+ }
440
439
}
441
- } else {
442
- if ( touchYstartRight !== null ) {
443
- distRight = e . pageY - touchYstartRight ;
444
- rightPaddleY = previousPositionRight + distRight ;
445
- rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
440
+ } else { // 鼠标移动
441
+ if ( e . pageX - canvas . offsetLeft < canvas . width / 2 ) {
442
+ if ( touchYstartLeft !== null ) {
443
+ distLeft = e . pageY - touchYstartLeft ;
444
+ leftPaddleY = previousPositionLeft + distLeft ;
445
+ leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
446
+ }
447
+ } else {
448
+ if ( touchYstartRight !== null ) {
449
+ distRight = e . pageY - touchYstartRight ;
450
+ rightPaddleY = previousPositionRight + distRight ;
451
+ rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
452
+ }
446
453
}
447
454
}
448
455
}
0 commit comments