Skip to content

Commit 732e9e7

Browse files
committed
- add mobile touch support for paddle movement and ball launch
- adjust game speeds on mobile devices and small screens - fix combo system (start at 0 instead of 1) - improve responsive layout: - add `overflow-y: auto` to screen containers - set specific width constraints for game elements - add desktop-specific media query - enhance UI styling with better margins and max-width
1 parent f89c14e commit 732e9e7

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

games/breakout.html

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
transition: opacity 0.5s ease;
268268
text-align: center;
269269
padding: 20px;
270+
overflow-y: auto;
270271
}
271272

272273
.screen.active {
@@ -318,8 +319,9 @@
318319
background: rgba(25, 27, 58, 0.7);
319320
border-radius: 10px;
320321
padding: 15px;
321-
margin-top: 20px;
322-
max-width: 80%;
322+
margin: 20px auto;
323+
width: 80%;
324+
max-width: 500px;
323325
gap: 20px;
324326
}
325327

@@ -463,6 +465,19 @@
463465
flex-direction: column;
464466
}
465467
}
468+
469+
470+
@media (min-width: 769px) {
471+
.game-container {
472+
width: var(--game-width);
473+
margin: 20px auto;
474+
max-height: 90vh;
475+
}
476+
477+
#game-board {
478+
height: min(var(--game-height), 70vh);
479+
}
480+
}
466481
</style>
467482
</head>
468483
<body>
@@ -830,6 +845,13 @@ <h2>You Win!</h2>
830845
config.boardWidth = elements.gameBoard.offsetWidth;
831846
config.boardHeight = elements.gameBoard.offsetHeight;
832847

848+
if (window.innerWidth <= 768 || /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
849+
config.initialBallSpeed *= 0.7;
850+
config.maxBallSpeed *= 0.7;
851+
paddle.speed *= 0.7;
852+
config.powerupSpeed *= 0.7;
853+
}
854+
833855
paddle.x = config.boardWidth / 2;
834856
elements.paddle.style.width = paddle.width + 'px';
835857
elements.paddle.style.height = paddle.height + 'px';
@@ -843,7 +865,7 @@ <h2>You Win!</h2>
843865
state.score = 0;
844866
state.lives = 3;
845867
state.level = 1;
846-
state.combo = 1;
868+
state.combo = 0;
847869
state.powerups = [];
848870
state.particles = [];
849871
state.effects = [];
@@ -883,6 +905,27 @@ <h2>You Win!</h2>
883905
}
884906
});
885907

908+
elements.gameBoard.addEventListener('touchmove', (e) => {
909+
if (state.running && !state.paused) {
910+
e.preventDefault(); // Prevent scrolling
911+
const touch = e.touches[0];
912+
const gameBoardRect = elements.gameBoard.getBoundingClientRect();
913+
const relativeX = touch.clientX - gameBoardRect.left;
914+
915+
if (relativeX > 0 && relativeX < config.boardWidth) {
916+
paddle.x = relativeX;
917+
updatePaddlePosition();
918+
}
919+
}
920+
});
921+
922+
elements.gameBoard.addEventListener('touchend', (e) => {
923+
if (state.running && !state.paused && !state.transitionActive && state.launchReady &&
924+
(!ball.launched || (state.balls.length > 0 && state.balls.some(b => !b.launched)))) {
925+
launchBall();
926+
}
927+
});
928+
886929
document.addEventListener('keydown', (e) => {
887930
if (e.key === 'ArrowLeft') {
888931
state.keyStates.left = true;
@@ -1334,7 +1377,7 @@ <h2>You Win!</h2>
13341377
const brick = state.bricks[index];
13351378

13361379
const brickType = brickTypes[brick.type];
1337-
const points = brickType.points * state.combo;
1380+
const points = brickType.points * (state.combo + 1);
13381381
state.score += points;
13391382
updateScoreDisplay();
13401383

@@ -1848,7 +1891,7 @@ <h2>You Win!</h2>
18481891
state.lives--;
18491892
updateLivesDisplay();
18501893

1851-
state.combo = 1;
1894+
state.combo = 0;
18521895
updateComboDisplay();
18531896

18541897
clearPowerups();
@@ -1873,7 +1916,7 @@ <h2>You Win!</h2>
18731916
if (now - state.lastBrickHit < config.comboTimeout) {
18741917
state.combo++;
18751918
} else {
1876-
state.combo = 1;
1919+
state.combo = 0;
18771920
}
18781921

18791922
state.lastBrickHit = now;
@@ -1882,8 +1925,8 @@ <h2>You Win!</h2>
18821925

18831926
function checkComboTimeout() {
18841927
const now = Date.now();
1885-
if (state.combo > 1 && now - state.lastBrickHit > config.comboTimeout) {
1886-
state.combo = 1;
1928+
if (state.combo > 0 && now - state.lastBrickHit > config.comboTimeout) { // Changed from > 1 to > 0
1929+
state.combo = 0;
18871930
updateComboDisplay();
18881931
}
18891932
}

0 commit comments

Comments
 (0)