|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Left or Right Game</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + background-color: black; |
| 10 | + display: flex; |
| 11 | + justify-content: center; |
| 12 | + align-items: center; |
| 13 | + height: 100vh; |
| 14 | + margin: 0; |
| 15 | + font-family: Arial, sans-serif; |
| 16 | + } |
| 17 | + #game-text { |
| 18 | + font-size: 48px; |
| 19 | + color: white; |
| 20 | + text-transform: uppercase; |
| 21 | + } |
| 22 | + #score { |
| 23 | + position: absolute; |
| 24 | + top: 10px; |
| 25 | + right: 10px; |
| 26 | + color: white; |
| 27 | + } |
| 28 | + #start-screen { |
| 29 | + display: flex; |
| 30 | + flex-direction: column; |
| 31 | + align-items: center; |
| 32 | + justify-content: center; |
| 33 | + text-align: center; |
| 34 | + color: white; |
| 35 | + } |
| 36 | + #instructions { |
| 37 | + max-width: 80%; |
| 38 | + } |
| 39 | + </style> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + <div id="start-screen"> |
| 43 | + <h1>Left or Right Game</h1> |
| 44 | + <p id="instructions"> |
| 45 | + Click the left side of the screen or press "Z" when you see "left".<br> |
| 46 | + Click the right side of the screen or press "/" when you see "right".<br> |
| 47 | + Be quick! The game speeds up as you score points. |
| 48 | + </p> |
| 49 | + <button id="start-button">Start Game</button> |
| 50 | + </div> |
| 51 | + <div id="game" style="display: none;"> |
| 52 | + <div id="game-text"></div> |
| 53 | + <div id="score">Score: 0</div> |
| 54 | + </div> |
| 55 | + |
| 56 | + <script> |
| 57 | + const game = document.getElementById('game'); |
| 58 | + const gameText = document.getElementById('game-text'); |
| 59 | + const scoreElement = document.getElementById('score'); |
| 60 | + const startScreen = document.getElementById('start-screen'); |
| 61 | + const startButton = document.getElementById('start-button'); |
| 62 | + let score = 0; |
| 63 | + let currentDirection; |
| 64 | + let interval; |
| 65 | + let timeout; |
| 66 | + const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); |
| 67 | + |
| 68 | + function getRandomDirection() { |
| 69 | + return Math.random() < 0.5 ? 'left' : 'right'; |
| 70 | + } |
| 71 | + |
| 72 | + function updateGameText() { |
| 73 | + clearTimeout(timeout); |
| 74 | + gameText.style.color = 'white'; |
| 75 | + currentDirection = getRandomDirection(); |
| 76 | + gameText.textContent = currentDirection; |
| 77 | + timeout = setTimeout(() => { |
| 78 | + gameText.style.color = 'red'; |
| 79 | + resetGame(); |
| 80 | + }, intervalTime()); |
| 81 | + } |
| 82 | + |
| 83 | + function updateScore() { |
| 84 | + score++; |
| 85 | + scoreElement.textContent = `Score: ${score}`; |
| 86 | + } |
| 87 | + |
| 88 | + function resetGame() { |
| 89 | + score = 0; |
| 90 | + scoreElement.textContent = `Score: ${score}`; |
| 91 | + clearInterval(interval); |
| 92 | + interval = setInterval(updateGameText, intervalTime()); |
| 93 | + } |
| 94 | + |
| 95 | + function intervalTime() { |
| 96 | + return 1000 - (10 * score); |
| 97 | + } |
| 98 | + |
| 99 | + function handleKeyPress(event) { |
| 100 | + if (event.key === 'z' || event.key === '/') { |
| 101 | + if ((event.key === 'z' && currentDirection === 'left') || (event.key === '/' && currentDirection === 'right')) { |
| 102 | + clearTimeout(timeout); |
| 103 | + gameText.style.color = 'green'; |
| 104 | + updateScore(); |
| 105 | + clearInterval(interval); |
| 106 | + interval = setInterval(updateGameText, intervalTime()); |
| 107 | + } else { |
| 108 | + gameText.style.color = 'red'; |
| 109 | + resetGame(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + function handleScreenTap(event) { |
| 115 | + const screenWidth = window.innerWidth; |
| 116 | + const tappedLeft = event.clientX < screenWidth / 2; |
| 117 | + |
| 118 | + if ((tappedLeft && currentDirection === 'left') || (!tappedLeft && currentDirection === 'right')) { |
| 119 | + clearTimeout(timeout); |
| 120 | + gameText.style.color = 'green'; |
| 121 | + updateScore(); |
| 122 | + clearInterval(interval); |
| 123 | + interval = setInterval(updateGameText, intervalTime()); |
| 124 | + } else { |
| 125 | + gameText.style.color = 'red'; |
| 126 | + resetGame(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (isMobile) { |
| 131 | + document.addEventListener('click', handleScreenTap); |
| 132 | + } else { |
| 133 | + document.addEventListener('keydown', handleKeyPress); |
| 134 | + } |
| 135 | + |
| 136 | + function startGame() { |
| 137 | + startScreen.style.display = 'none'; |
| 138 | + game.style.display = 'block'; |
| 139 | + resetGame(); |
| 140 | + } |
| 141 | + |
| 142 | + startButton.addEventListener('click', startGame); |
| 143 | + |
| 144 | + resetGame(); |
| 145 | + </script> |
| 146 | +</body> |
| 147 | +</html> |
0 commit comments