Skip to content

Commit c961594

Browse files
committed
fix bug
1 parent 32573f7 commit c961594

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

07-Ping-Pong-Game/script.js

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var pauseBtn = document.getElementById("pause-btn");
77
var restartBtn = document.getElementById("restart-btn");
88
var animationId;
99
var gameRunning = false;
10+
var ballColor = "#FFF";
1011

1112
startBtn.addEventListener("click", function () {
1213
if (!gameRunning) {
@@ -172,13 +173,13 @@ function draw() {
172173
// Clear canvas
173174
ctx.clearRect(0, 0, canvas.width, canvas.height);
174175

175-
ctx.fillStyle = "#FFF";
176+
ctx.fillStyle = ballColor;
176177
ctx.font = "15px Arial";
177178

178179
ctx.beginPath();
179180
ctx.moveTo(canvas.width / 2, 0);
180181
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
182183
ctx.stroke();
183184
ctx.closePath();
184185

@@ -344,11 +345,12 @@ var previousPositionRight = canvas.height / 2 - paddleHeight;
344345
function actionStart(e) {
345346
//e.preventDefault();
346347
//判断是鼠标左键点击还是触摸
348+
if(!gameRunning ) { return; }
347349
var leftPt = { pageX: canvas.width, pageY: 0 }, rightPt = { pageX: 0, pageY: 0 };
348350

349-
if (e.touches && e.touches.length >1) {//如果是触摸
351+
if (e.touches && e.touches.length > 1) {//如果是触摸
350352
e.touches.forEach(element => {//记录左半边,最左边的点
351-
if (element.pageX - canvas.offsetLeft < canvas.width / 2) {
353+
if (element.pageX - canvas.offsetLeft < canvas.width / 2) {
352354
leftPt = element.pageX < leftPt.pageX ? element : leftPt;
353355
} else {//记录右半边,最右边的点
354356
rightPt = element.pageX > rightPt.pageX ? element : rightPt;
@@ -357,14 +359,14 @@ function actionStart(e) {
357359
if (touchYstartLeft == null) { touchYstartLeft = leftPt.pageY; }
358360
if (touchYstartRight == null) { touchYstartRight = rightPt.pageY; }
359361
return;
360-
} else if(e.touches && e.touches.length == 1) {//如果是单点触摸
362+
} else if (e.touches && e.touches.length == 1) {//如果是单点触摸
361363
if (e.touches[0].pageX - canvas.offsetLeft < canvas.width / 2) {
362364
if (touchYstartLeft == null) { touchYstartLeft = e.touches[0].pageY; }
363365
} else {
364366
if (touchYstartRight == null) { touchYstartRight = e.touches[0].pageY; }
365367
}
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) {
368370
if (touchYstartLeft == null) { touchYstartLeft = e.pageY; }
369371
} else {
370372
if (touchYstartRight == null) { touchYstartRight = e.pageY; }
@@ -388,61 +390,66 @@ function actionEnd(e) {
388390
}
389391
function actionMove(e) {
390392
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+
}
404409
}
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-
}
418410

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;
424413
leftPaddleY = previousPositionLeft + distLeft;
425414
leftPaddleY = Math.max(0, Math.min(canvas.height - paddleHeight, leftPaddleY));
415+
ballColor = 'blue';
426416
}
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;
430420
rightPaddleY = previousPositionRight + distRight;
431421
rightPaddleY = Math.max(0, Math.min(canvas.height - paddleHeight, rightPaddleY));
422+
ballColor = 'green';
432423
}
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+
}
440439
}
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+
}
446453
}
447454
}
448455
}

0 commit comments

Comments
 (0)