Skip to content

Commit 31c23ba

Browse files
committed
change pingPong to mouse/touch ctrl
1 parent 5c54c38 commit 31c23ba

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

04-Breakout-Game/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ body {
2929
justify-content: center;
3030
min-height: 100vh;
3131
margin: 0;
32-
overscroll-behavior: contain;
32+
overscroll-behavior-y: contain;
3333
}
3434

3535
h1 {

07-Ping-Pong-Game/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h5 id="message"></h5>
5656
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
5757
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
5858
<script src="script.js"></script>
59-
<script src="../ctrl.js"></script>
59+
<!-- <script src="../ctrl.js"></script>
6060
<script>
6161
var upbt = document.getElementById("up_arrow");
6262
var downbt = document.getElementById("down_arrow");
@@ -92,7 +92,7 @@ <h5 id="message"></h5>
9292
vkbd.style.width = "100vw";
9393
vkbd.style.height = "100vh";
9494
95-
</script>
95+
</script> -->
9696
</body>
9797

9898
</html>

07-Ping-Pong-Game/script.js

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ function update() {
148148
} else if (rightPlayerScore === maxScore) {
149149
playerWin("Right player");
150150
}
151+
151152
}
152153

153154
function playerWin(player) {
155+
gameRunning = false;
154156
var message = "Congratulations! " + player + " win!";
155157
$("#message").text(message); // Set the message text
156158
$("#message-modal").modal("show"); // Display the message modal
@@ -215,7 +217,7 @@ $("#message-modal-close").on("click", function () {
215217

216218
var downLock = false;
217219
function keyDown() {// pressed down arrow key
218-
if(upLock) { keyUp(); }
220+
if (upLock) { keyUp(); }
219221
if (downLock) {
220222
var event = new KeyboardEvent('keyup', {
221223
bubbles: true,
@@ -279,7 +281,7 @@ function keyRight() {// pressed right arrow key
279281

280282
var wLock = false;
281283
function keyW() {// pressed w key
282-
if (sLock) {keyS();}
284+
if (sLock) { keyS(); }
283285
if (wLock) {
284286
var event = new KeyboardEvent('keyup', {
285287
bubbles: true,
@@ -308,7 +310,7 @@ function keyW() {// pressed w key
308310

309311
var sLock = false;
310312
function keyS() {// pressed s key
311-
if (wLock) {keyW();}
313+
if (wLock) { keyW(); }
312314
if (sLock) {
313315
var event = new KeyboardEvent('keyup', {
314316
bubbles: true,
@@ -333,3 +335,105 @@ function keyS() {// pressed s key
333335
document.getElementById("right_arrow").style.transform = "scale(0.9)rotate(90deg)";
334336
}
335337
}
338+
339+
//增加一个触摸屏幕事件,让Paddle水平移动距离和触摸点水平移动距离一致
340+
var touchYstartLeft = null;
341+
var previousPositionLeft = canvas.height / 2 - paddleHeight;
342+
var touchYstartRight = null;
343+
var previousPositionRight = canvas.height / 2 - paddleHeight;
344+
function actionStart(e) {
345+
//e.preventDefault();
346+
//判断是鼠标左键点击还是触摸
347+
var leftPt = { pageX: canvas.width, pageY: 0 }, rightPt = { pageX: 0, pageY: 0 };
348+
349+
if (e.touches) {//如果是触摸
350+
e.touches.forEach(element => {//记录左半边,最左边的点
351+
if (element.pageX - canvas.offsetLeft < canvas.width / 2) {
352+
leftPt = element.pageX < leftPt.pageX ? element : leftPt;
353+
} else {//记录右半边,最右边的点
354+
rightPt = element.pageX > rightPt.pageX ? element : rightPt;
355+
}
356+
});
357+
if (touchYstartLeft == null) { touchYstartLeft = leftPt.pageY; }
358+
if (touchYstartRight == null) { touchYstartRight = rightPt.pageY; }
359+
return;
360+
} else {//如果是鼠标 touchXstart等于鼠标的x坐标
361+
if (e.pageX - canvas.offsetLeft < canvas.width / 2) {
362+
if (touchYstartLeft == null) { touchYstartLeft = e.pageY; }
363+
} else {
364+
if (touchYstartRight == null) { touchYstartRight = e.pageY; }
365+
}
366+
}
367+
368+
369+
370+
371+
}
372+
function actionEnd(e) {
373+
//e.preventDefault();
374+
if (touchYstartRight != null) {
375+
touchYstartRight = null;
376+
previousPositionRight = rightPaddleY;
377+
}
378+
if (touchYstartLeft != null) {
379+
touchYstartLeft = null;
380+
previousPositionLeft = leftPaddleY;
381+
}
382+
}
383+
function actionMove(e) {
384+
//e.preventDefault();
385+
var distLeft, distRight, leftPt, rightPt;
386+
//判断是鼠标左键点击还是触摸
387+
if (e.touches) {
388+
e.touches.forEach(element => {
389+
if (element.pageX - canvas.offsetLeft < canvas.width / 2) {
390+
leftPt = element.pageX < leftPt.pageX ? element : leftPt;
391+
} else {//记录右半边,最右边的点
392+
rightPt = element.pageX > rightPt.pageX ? element : rightPt;
393+
}
394+
});
395+
if (touchYstartLeft != null) {
396+
distLeft = leftPt.pageY - touchYstartLeft;
397+
leftPaddleY = previousPositionLeft + distLeft;
398+
if (leftPaddleY + paddleHeight > canvas.height) leftPaddleY = canvas.height - paddleHeight;
399+
if (leftPaddleY < 0) leftPaddleY = 0;
400+
}
401+
if (touchYstartRight != null) {
402+
distRight = rightPt.pageY - touchYstartRight;
403+
rightPaddleY = previousPositionRight + distRight;
404+
if (rightPaddleY + paddleHeight > canvas.height) rightPaddleY = canvas.height - paddleHeight;
405+
if (rightPaddleY < 0) rightPaddleY = 0;
406+
}
407+
408+
} else {//如果是鼠标 dist等于鼠标的x坐标x相对touchXstart的移动距离
409+
if (e.pageX - canvas.offsetLeft < canvas.width / 2) {
410+
if (touchYstartLeft != null) {
411+
distLeft = e.pageY - touchYstartLeft;
412+
leftPaddleY = previousPositionLeft + distLeft;
413+
if (leftPaddleY + paddleHeight > canvas.height) leftPaddleY = canvas.height - paddleHeight;
414+
if (leftPaddleY < 0) leftPaddleY = 0;
415+
}
416+
} else {
417+
if (touchYstartRight != null) {
418+
distRight = e.pageY - touchYstartRight;
419+
rightPaddleY = previousPositionRight + distRight;
420+
if (rightPaddleY + paddleHeight > canvas.height) rightPaddleY = canvas.height - paddleHeight;
421+
if (rightPaddleY < 0) rightPaddleY = 0;
422+
}
423+
}
424+
}
425+
426+
427+
428+
429+
430+
}
431+
432+
433+
document.addEventListener('touchstart', actionStart);
434+
document.addEventListener('touchend', actionEnd);
435+
document.addEventListener('touchmove', actionMove);
436+
//相应鼠标点击后移动的事件
437+
document.addEventListener('mousedown', actionStart);
438+
document.addEventListener('mouseup', actionEnd);
439+
document.addEventListener('mousemove', actionMove);

0 commit comments

Comments
 (0)