Skip to content

Commit a0c0ace

Browse files
committed
Add mobile support for touch
1 parent 0dd36d8 commit a0c0ace

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

Day #12 - 2048 Game/script.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,16 @@ document.addEventListener('keydown', (e) => {
130130
// Add touch event listeners for mobile support
131131
let touchStartX = 0;
132132
let touchStartY = 0;
133-
let touchEndX = 0;
134-
let touchEndY = 0;
135133

136134
function handleTouchStart(e) {
137135
touchStartX = e.touches[0].clientX;
138136
touchStartY = e.touches[0].clientY;
139137
}
140138

141-
function handleTouchMove(e) {
142-
touchEndX = e.touches[0].clientX;
143-
touchEndY = e.touches[0].clientY;
144-
}
139+
function handleTouchEnd(e) {
140+
const touchEndX = e.changedTouches[0].clientX;
141+
const touchEndY = e.changedTouches[0].clientY;
145142

146-
function handleTouchEnd() {
147143
const dx = touchEndX - touchStartX;
148144
const dy = touchEndY - touchStartY;
149145
const absDx = Math.abs(dx);
@@ -167,7 +163,6 @@ function handleTouchEnd() {
167163
}
168164

169165
document.addEventListener('touchstart', handleTouchStart, false);
170-
document.addEventListener('touchmove', handleTouchMove, false);
171166
document.addEventListener('touchend', handleTouchEnd, false);
172167

173168
function initGame() {

Day #12 - 2048 Game/style.css

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ body {
1313
padding: 30px;
1414
border-radius: 15px;
1515
text-align: center;
16-
16+
width: 100%;
17+
max-width: 500px;
1718
}
1819

1920
.game-container {
@@ -26,24 +27,28 @@ body {
2627

2728
#grid-container {
2829
display: grid;
29-
grid-template-columns: repeat(4, 100px);
30-
grid-template-rows: repeat(4, 100px);
30+
grid-template-columns: repeat(4, 1fr);
31+
grid-template-rows: repeat(4, 1fr);
3132
gap: 10px;
33+
width: 100%;
34+
max-width: 400px;
3235
}
3336

37+
/* Ensure grid cells maintain a square aspect ratio */
3438
.grid-cell {
35-
width: 100px;
36-
height: 100px;
39+
width: 100%;
40+
padding-bottom: 100%; /* Creates a square aspect ratio */
3741
background-color: #cdc1b4;
3842
border-radius: 6px;
3943
display: flex;
4044
justify-content: center;
4145
align-items: center;
42-
font-size: 30px;
46+
font-size: 1.5rem; /* Adjust font size for better visibility */
4347
font-weight: bold;
4448
color: #776e65;
4549
}
4650

51+
/* Color specific rules for different cell values */
4752
.grid-cell[data-value="2"] { background-color: #eee4da; color: #776e65; }
4853
.grid-cell[data-value="4"] { background-color: #ede0c8; color: #776e65; }
4954
.grid-cell[data-value="8"] { background-color: #f2b179; color: #f9f6f2; }
@@ -58,6 +63,6 @@ body {
5863

5964
.score-container {
6065
margin-top: 20px;
61-
font-size: 24px;
66+
font-size: 1.5rem; /* Adjusted for better readability on mobile */
6267
font-weight: bold;
63-
}
68+
}

0 commit comments

Comments
 (0)