CommanderLazarus commited on
Commit
ed45bd4
·
verified ·
1 Parent(s): 4b59f08

Allow the screen to rotate and tilt to up to a 45 degree angle.

Browse files
Files changed (3) hide show
  1. index.html +8 -2
  2. script.js +37 -8
  3. style.css +11 -2
index.html CHANGED
@@ -57,12 +57,18 @@
57
  <option value="hard">Hard</option>
58
  </select>
59
  </div>
60
-
61
  <button id="start-btn" class="w-full bg-green-600 hover:bg-green-700 text-white py-3 rounded-lg font-bold">
62
  Start Game
63
  </button>
64
 
65
- <div class="mt-6 pt-4 border-t border-gray-700">
 
 
 
 
 
 
 
66
  <h3 class="text-lg font-semibold mb-2">Stats</h3>
67
  <div class="flex justify-between">
68
  <span>Time:</span>
 
57
  <option value="hard">Hard</option>
58
  </select>
59
  </div>
 
60
  <button id="start-btn" class="w-full bg-green-600 hover:bg-green-700 text-white py-3 rounded-lg font-bold">
61
  Start Game
62
  </button>
63
 
64
+ <div class="mt-4">
65
+ <label class="inline-flex items-center cursor-pointer">
66
+ <input type="checkbox" id="tilt-control" class="sr-only peer">
67
+ <div class="relative w-11 h-6 bg-gray-700 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-indigo-600"></div>
68
+ <span class="ms-3 text-sm font-medium">Tilt Controls</span>
69
+ </label>
70
+ </div>
71
+ <div class="mt-6 pt-4 border-t border-gray-700">
72
  <h3 class="text-lg font-semibold mb-2">Stats</h3>
73
  <div class="flex justify-between">
74
  <span>Time:</span>
script.js CHANGED
@@ -17,24 +17,48 @@ document.addEventListener('DOMContentLoaded', () => {
17
  canvas.width = size;
18
  canvas.height = size;
19
  }
20
-
21
  // Game variables
22
  let maze = [];
23
  let cellSize = 0;
24
- let ufo = { x: 0, y: 0 };
 
25
  let exit = { x: 0, y: 0 };
26
  let moves = 0;
27
  let time = 0;
28
  let timer = null;
29
  let gameRunning = false;
30
  let mazeSize = 10;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  // Initialize game
33
  function initGame() {
34
  resizeCanvas();
35
  window.addEventListener('resize', resizeCanvas);
36
 
37
- // Set maze size based on difficulty
 
 
 
 
38
  switch(difficultySelect.value) {
39
  case 'easy': mazeSize = 8; break;
40
  case 'medium': mazeSize = 10; break;
@@ -170,9 +194,8 @@ document.addEventListener('DOMContentLoaded', () => {
170
 
171
  // Move UFO
172
  function moveUfo(dx, dy) {
173
- if (!gameRunning) return;
174
-
175
- const newX = ufo.x + dx;
176
  const newY = ufo.y + dy;
177
 
178
  // Check if move is valid
@@ -189,12 +212,18 @@ document.addEventListener('DOMContentLoaded', () => {
189
  }
190
  }
191
  }
 
 
 
 
 
 
 
192
 
193
  // Start game
194
  function startGame() {
195
  if (gameRunning) return;
196
-
197
- // Reset game state
198
  moves = 0;
199
  time = 0;
200
  movesDisplay.textContent = moves;
 
17
  canvas.width = size;
18
  canvas.height = size;
19
  }
 
20
  // Game variables
21
  let maze = [];
22
  let cellSize = 0;
23
+ let orientationEnabled = false;
24
+ let ufo = { x: 0, y: 0 };
25
  let exit = { x: 0, y: 0 };
26
  let moves = 0;
27
  let time = 0;
28
  let timer = null;
29
  let gameRunning = false;
30
  let mazeSize = 10;
31
+ // Handle device orientation
32
+ function handleOrientation(event) {
33
+ if (!gameRunning) return;
34
+
35
+ const tiltThreshold = 15; // degrees
36
+ const beta = event.beta; // front-to-back tilt
37
+ const gamma = event.gamma; // left-to-right tilt
38
+
39
+ if (beta > tiltThreshold) {
40
+ moveUfo(0, 1); // tilt forward = move down
41
+ } else if (beta < -tiltThreshold) {
42
+ moveUfo(0, -1); // tilt backward = move up
43
+ }
44
+
45
+ if (gamma > tiltThreshold) {
46
+ moveUfo(1, 0); // tilt right = move right
47
+ } else if (gamma < -tiltThreshold) {
48
+ moveUfo(-1, 0); // tilt left = move left
49
+ }
50
+ }
51
 
52
  // Initialize game
53
  function initGame() {
54
  resizeCanvas();
55
  window.addEventListener('resize', resizeCanvas);
56
 
57
+ // Add device orientation listener
58
+ if (window.DeviceOrientationEvent) {
59
+ window.addEventListener('deviceorientation', handleOrientation);
60
+ }
61
+ // Set maze size based on difficulty
62
  switch(difficultySelect.value) {
63
  case 'easy': mazeSize = 8; break;
64
  case 'medium': mazeSize = 10; break;
 
194
 
195
  // Move UFO
196
  function moveUfo(dx, dy) {
197
+ if (!gameRunning || (orientationEnabled && dx === 0 && dy === 0)) return;
198
+ const newX = ufo.x + dx;
 
199
  const newY = ufo.y + dy;
200
 
201
  // Check if move is valid
 
212
  }
213
  }
214
  }
215
+ // Toggle tilt controls
216
+ document.getElementById('tilt-control').addEventListener('change', function() {
217
+ orientationEnabled = this.checked;
218
+ if (orientationEnabled && !gameRunning) {
219
+ alert('Tilt controls enabled! Tilt your device to move the UFO.');
220
+ }
221
+ });
222
 
223
  // Start game
224
  function startGame() {
225
  if (gameRunning) return;
226
+ // Reset game state
 
227
  moves = 0;
228
  time = 0;
229
  movesDisplay.textContent = moves;
style.css CHANGED
@@ -23,10 +23,19 @@
23
  .ufo-float {
24
  animation: ufoFloat 2s ease-in-out infinite;
25
  }
26
-
27
  /* Responsive adjustments */
28
  @media (max-width: 768px) {
29
  #maze-container {
30
  max-width: 100%;
31
  }
32
- }
 
 
 
 
 
 
 
 
 
 
 
23
  .ufo-float {
24
  animation: ufoFloat 2s ease-in-out infinite;
25
  }
 
26
  /* Responsive adjustments */
27
  @media (max-width: 768px) {
28
  #maze-container {
29
  max-width: 100%;
30
  }
31
+ }
32
+
33
+ /* Toggle switch styling */
34
+ .toggle-checkbox:checked {
35
+ right: 0;
36
+ border-color: #7C3AED;
37
+ }
38
+
39
+ .toggle-checkbox:checked + .toggle-label {
40
+ background-color: #7C3AED;
41
+ }