arad1367 commited on
Commit
5d2ed11
β€’
1 Parent(s): 8c0bdf9

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +42 -2
index.html CHANGED
@@ -146,6 +146,19 @@
146
  let invaderInterval;
147
  let username = '';
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  buttons.forEach(button => {
150
  button.addEventListener('click', (e) => {
151
  username = usernameInput.value.trim();
@@ -167,7 +180,6 @@
167
  invaderSpeed = 14; // Fast level (3 times faster)
168
  }
169
  createInvaders();
170
- document.addEventListener('keydown', movePlayer);
171
  invaderInterval = setInterval(moveInvaders, 500);
172
  setInterval(createInvaders, 5000); // Create new invaders every 5 seconds
173
 
@@ -190,7 +202,35 @@
190
  }
191
  }
192
 
193
- // Move player
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  function movePlayer(e) {
195
  switch (e.key) {
196
  case 'ArrowLeft':
 
146
  let invaderInterval;
147
  let username = '';
148
 
149
+ // Determine if touch events are supported
150
+ const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
151
+
152
+ if (isTouchDevice) {
153
+ // Touch events for mobile devices
154
+ game.addEventListener('touchstart', handleTouchStart);
155
+ game.addEventListener('touchmove', handleTouchMove);
156
+ game.addEventListener('touchend', handleTouchEnd);
157
+ } else {
158
+ // Keyboard events for desktop
159
+ document.addEventListener('keydown', movePlayer);
160
+ }
161
+
162
  buttons.forEach(button => {
163
  button.addEventListener('click', (e) => {
164
  username = usernameInput.value.trim();
 
180
  invaderSpeed = 14; // Fast level (3 times faster)
181
  }
182
  createInvaders();
 
183
  invaderInterval = setInterval(moveInvaders, 500);
184
  setInterval(createInvaders, 5000); // Create new invaders every 5 seconds
185
 
 
202
  }
203
  }
204
 
205
+ // Handle touch events for player movement and shooting
206
+ let touchStartX = null;
207
+ function handleTouchStart(event) {
208
+ const touch = event.touches[0];
209
+ touchStartX = touch.clientX;
210
+ }
211
+
212
+ function handleTouchMove(event) {
213
+ event.preventDefault();
214
+ }
215
+
216
+ function handleTouchEnd(event) {
217
+ const touch = event.changedTouches[0];
218
+ const touchEndX = touch.clientX;
219
+
220
+ // Determine swipe direction or tap for shooting
221
+ if (touchEndX < touchStartX) {
222
+ // Swipe left
223
+ movePlayer({ key: 'ArrowLeft' });
224
+ } else if (touchEndX > touchStartX) {
225
+ // Swipe right
226
+ movePlayer({ key: 'ArrowRight' });
227
+ } else {
228
+ // Tap for shooting
229
+ shoot();
230
+ }
231
+ }
232
+
233
+ // Move player based on keyboard events
234
  function movePlayer(e) {
235
  switch (e.key) {
236
  case 'ArrowLeft':