@@ -135,8 +135,8 @@ <h5 id="message"></h5>
135
135
// Define paddle properties
136
136
var paddleHeight = 80 ;
137
137
var paddleWidth = paddleHeight / 2 ;
138
- var leftPaddleY = canvas . height / 2 - paddleHeight / 2 ;
139
- var rightPaddleY = canvas . height / 2 - paddleHeight / 2 ;
138
+ var leftPaddleY = canvas . height / 2 ;
139
+ var rightPaddleY = canvas . height / 2 ;
140
140
var leftPaddleX = 0 ;
141
141
var rightPaddleX = canvas . width ;
142
142
var paddleSpeed = 10 ;
@@ -147,9 +147,13 @@ <h5 id="message"></h5>
147
147
var rightPlayerScore = 0 ;
148
148
var maxScore = 100 ;
149
149
var touchYstartLeft = null ;
150
- var previousPositionLeft = canvas . height / 2 - paddleHeight ;
150
+ var touchXstartLeft = null ;
151
+ var previousYPositionLeft = canvas . height / 2 ;
152
+ var previousXPositionLeft = 0 + edgeWidth ;
151
153
var touchYstartRight = null ;
152
- var previousPositionRight = canvas . height / 2 - paddleHeight ;
154
+ var touchXstartRight = null ;
155
+ var previousYPositionRight = canvas . height / 2 ;
156
+ var previousXPositionRight = canvas . width - edgeWidth ;
153
157
const canvasBounds = canvas . getBoundingClientRect ( ) ;
154
158
const canvasMiddle = canvasBounds . width / 2 + canvasBounds . left ;
155
159
var effectiveBounds = {
@@ -397,14 +401,14 @@ <h5 id="message"></h5>
397
401
rightPt = e . touches [ i ] . pageX > rightPt . pageX ? e . touches [ i ] : rightPt ;
398
402
}
399
403
}
400
- if ( touchYstartLeft == null && leftPt . pageY != - 1 ) { touchYstartLeft = leftPt . pageY ; }
401
- if ( touchYstartRight == null && rightPt . pageY != - 1 ) { touchYstartRight = rightPt . pageY ; }
404
+ if ( touchYstartLeft == null && leftPt . pageY != - 1 ) { touchYstartLeft = leftPt . pageY ; touchXstartLeft = leftPt . pageX ; }
405
+ if ( touchYstartRight == null && rightPt . pageY != - 1 ) { touchYstartRight = rightPt . pageY ; touchXstartRight = rightPt . pageX ; }
402
406
return ;
403
407
} else { //如果是鼠标 touchXstart等于鼠标的x坐标
404
408
if ( e . pageX < canvasMiddle ) {
405
- if ( touchYstartLeft == null ) { touchYstartLeft = e . pageY ; }
409
+ if ( touchYstartLeft == null ) { touchYstartLeft = e . pageY ; touchXstartLeft = e . pageX ; }
406
410
} else {
407
- if ( touchYstartRight == null ) { touchYstartRight = e . pageY ; }
411
+ if ( touchYstartRight == null ) { touchYstartRight = e . pageY ; touchXstartRight = e . pageX ; }
408
412
}
409
413
}
410
414
@@ -414,11 +418,15 @@ <h5 id="message"></h5>
414
418
//e.preventDefault();
415
419
if ( touchYstartRight != null ) {
416
420
touchYstartRight = null ;
417
- previousPositionRight = rightPaddleY ;
421
+ touchXstartRight = null ;
422
+ previousYPositionRight = rightPaddleY ;
423
+ previousXPositionRight = rightPaddleX ;
418
424
}
419
425
if ( touchYstartLeft != null ) {
420
426
touchYstartLeft = null ;
421
- previousPositionLeft = leftPaddleY ;
427
+ touchXstartLeft = null ;
428
+ previousYPositionLeft = leftPaddleY ;
429
+ previousXPositionLeft = leftPaddleX ;
422
430
}
423
431
}
424
432
@@ -459,18 +467,24 @@ <h5 id="message"></h5>
459
467
// 更新左侧滑板位置
460
468
try {
461
469
if ( leftTouch && touchYstartLeft !== null ) {
462
- let distLeft = leftTouch . pageY - touchYstartLeft ;
463
- leftPaddleY = previousPositionLeft + distLeft ;
470
+ let distYLeft = leftTouch . pageY - touchYstartLeft ;
471
+ leftPaddleY = previousYPositionLeft + distYLeft ;
464
472
leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
473
+ let distXLeft = leftTouch . pageX - touchXstartLeft ;
474
+ leftPaddleX = previousXPositionLeft + distXLeft ;
475
+ leftPaddleX = Math . max ( 0 , Math . min ( canvas . width / 2 , leftPaddleX ) ) ;
465
476
}
466
477
} catch ( e ) { }
467
478
468
479
// 更新右侧滑板位置
469
480
try {
470
481
if ( rightTouch && touchYstartRight !== null ) {
471
- let distRight = rightTouch . pageY - touchYstartRight ;
472
- rightPaddleY = previousPositionRight + distRight ;
482
+ let distYRight = rightTouch . pageY - touchYstartRight ;
483
+ rightPaddleY = previousYPositionRight + distYRight ;
473
484
rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
485
+ let distXRight = rightTouch . pageX - touchXstartRight ;
486
+ rightPaddleX = previousXPositionRight + distXRight ;
487
+ rightPaddleX = Math . min ( canvas . width , Math . max ( canvas . width / 2 , rightPaddleX ) ) ;
474
488
}
475
489
} catch ( e ) { }
476
490
}
@@ -482,15 +496,21 @@ <h5 id="message"></h5>
482
496
483
497
if ( mouseX < canvasMiddle ) {
484
498
if ( touchYstartLeft !== null ) {
485
- let distLeft = mouseY - touchYstartLeft ;
486
- leftPaddleY = previousPositionLeft + distLeft ;
499
+ let distYLeft = mouseY - touchYstartLeft ;
500
+ leftPaddleY = previousYPositionLeft + distYLeft ;
487
501
leftPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , leftPaddleY ) ) ;
502
+ let distXLeft = mouseX - touchXstartLeft ;
503
+ leftPaddleX = previousXPositionLeft + distXLeft ;
504
+ leftPaddleX = Math . max ( 0 , Math . min ( canvas . width - paddleWidth , leftPaddleX ) ) ;
488
505
}
489
506
} else {
490
507
if ( touchYstartRight !== null ) {
491
- let distRight = mouseY - touchYstartRight ;
492
- rightPaddleY = previousPositionRight + distRight ;
508
+ let distYRight = mouseY - touchYstartRight ;
509
+ rightPaddleY = previousYPositionRight + distYRight ;
493
510
rightPaddleY = Math . max ( 0 , Math . min ( canvas . height - paddleHeight , rightPaddleY ) ) ;
511
+ let distXRight = mouseX - touchXstartRight ;
512
+ rightPaddleX = previousXPositionRight + distXRight ;
513
+ rightPaddleX = Math . max ( 0 , Math . min ( canvas . width - paddleWidth , rightPaddleX ) ) ;
494
514
}
495
515
}
496
516
}
0 commit comments