akhaliq HF staff commited on
Commit
e7e7de8
·
verified ·
1 Parent(s): 7ca58f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -8,7 +8,7 @@ with demo:
8
  <head>
9
  <meta charset="UTF-8">
10
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
11
- <title>Hugging Face Temple Run Mobile - Tap to Jump</title>
12
  <style>
13
  body {
14
  margin: 0;
@@ -148,11 +148,21 @@ with demo:
148
  const gameOverMessage = document.getElementById('game-over-message');
149
  let gameOver = false;
150
 
151
- // Touch controls
 
152
  let touchStartX = 0;
153
  let touchMoved = false;
154
  const swipeThreshold = 30;
155
 
 
 
 
 
 
 
 
 
 
156
  document.addEventListener('touchstart', (e) => {
157
  e.preventDefault();
158
  touchStartX = e.touches[0].clientX;
@@ -217,6 +227,22 @@ with demo:
217
  }
218
  }
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  // Animation loop
221
  function animate() {
222
  requestAnimationFrame(animate);
@@ -241,6 +267,9 @@ with demo:
241
  targetPosition.y = face.position.y; // Match height to avoid tilting
242
  face.lookAt(targetPosition);
243
 
 
 
 
244
  // Spawn items
245
  spawnItems();
246
 
 
8
  <head>
9
  <meta charset="UTF-8">
10
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
11
+ <title>Hugging Face Temple Run Mobile/Desktop - Tap/Space to Jump</title>
12
  <style>
13
  body {
14
  margin: 0;
 
148
  const gameOverMessage = document.getElementById('game-over-message');
149
  let gameOver = false;
150
 
151
+ // Keyboard and touch controls
152
+ const keys = {};
153
  let touchStartX = 0;
154
  let touchMoved = false;
155
  const swipeThreshold = 30;
156
 
157
+ // Keyboard event listeners (for desktop)
158
+ window.addEventListener('keydown', (e) => {
159
+ if (!gameOver) keys[e.key] = true;
160
+ });
161
+ window.addEventListener('keyup', (e) => {
162
+ keys[e.key] = false;
163
+ });
164
+
165
+ // Touch event listeners (for mobile)
166
  document.addEventListener('touchstart', (e) => {
167
  e.preventDefault();
168
  touchStartX = e.touches[0].clientX;
 
227
  }
228
  }
229
 
230
+ // Handle controls in animation loop
231
+ function handleControls(deltaTime) {
232
+ if (!gameOver) {
233
+ // Keyboard controls (desktop)
234
+ if (keys['ArrowLeft'] && face.lane > -1) face.lane--;
235
+ if (keys['ArrowRight'] && face.lane < 1) face.lane++;
236
+ if ((keys[' '] || keys['ArrowUp']) && !face.jumping) {
237
+ face.jumping = true;
238
+ face.velocity.y = 15; // Spacebar or Up Arrow to jump
239
+ }
240
+
241
+ // Ensure lane position is applied
242
+ face.position.x = face.lane * 3;
243
+ }
244
+ }
245
+
246
  // Animation loop
247
  function animate() {
248
  requestAnimationFrame(animate);
 
267
  targetPosition.y = face.position.y; // Match height to avoid tilting
268
  face.lookAt(targetPosition);
269
 
270
+ // Handle controls
271
+ handleControls(deltaTime);
272
+
273
  // Spawn items
274
  spawnItems();
275