File tree 2 files changed +44
-2
lines changed
2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -127,11 +127,53 @@ document.addEventListener('keydown', (e) => {
127
127
handleMove ( e . keyCode ) ;
128
128
} ) ;
129
129
130
+ // Add touch event listeners for mobile support
131
+ let touchStartX = 0 ;
132
+ let touchStartY = 0 ;
133
+ let touchEndX = 0 ;
134
+ let touchEndY = 0 ;
135
+
136
+ function handleTouchStart ( e ) {
137
+ touchStartX = e . touches [ 0 ] . clientX ;
138
+ touchStartY = e . touches [ 0 ] . clientY ;
139
+ }
140
+
141
+ function handleTouchMove ( e ) {
142
+ touchEndX = e . touches [ 0 ] . clientX ;
143
+ touchEndY = e . touches [ 0 ] . clientY ;
144
+ }
145
+
146
+ function handleTouchEnd ( ) {
147
+ const dx = touchEndX - touchStartX ;
148
+ const dy = touchEndY - touchStartY ;
149
+ const absDx = Math . abs ( dx ) ;
150
+ const absDy = Math . abs ( dy ) ;
151
+
152
+ if ( Math . max ( absDx , absDy ) > 20 ) { // Swipe threshold
153
+ if ( absDx > absDy ) {
154
+ if ( dx > 0 ) {
155
+ handleMove ( 39 ) ; // Swipe right
156
+ } else {
157
+ handleMove ( 37 ) ; // Swipe left
158
+ }
159
+ } else {
160
+ if ( dy > 0 ) {
161
+ handleMove ( 40 ) ; // Swipe down
162
+ } else {
163
+ handleMove ( 38 ) ; // Swipe up
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ document . addEventListener ( 'touchstart' , handleTouchStart , false ) ;
170
+ document . addEventListener ( 'touchmove' , handleTouchMove , false ) ;
171
+ document . addEventListener ( 'touchend' , handleTouchEnd , false ) ;
172
+
130
173
function initGame ( ) {
131
174
generateRandom ( ) ;
132
175
generateRandom ( ) ;
133
176
createGrid ( ) ;
134
177
}
135
178
136
-
137
179
initGame ( ) ;
Original file line number Diff line number Diff line change 60
60
margin-top : 20px ;
61
61
font-size : 24px ;
62
62
font-weight : bold;
63
- }
63
+ }
You can’t perform that action at this time.
0 commit comments