Cosmic keystrokes
Generate an interactive speed typing game in a single HTML file, featuring side-scrolling gameplay and Tailwind CSS styling.
You should make the website very aesthetic and use Tailwind.
Here's a complete web app as a single HTML file that implements the side-scrolling game with word typing mechanics and a point counter, using Tailwind CSS for styling:
html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Side-Scrolling Word Typing Game</title> <script src="https://cdn.tailwindcss.com"></script> <style> #game-container { position: relative; width: 100%; height: 400px; overflow: hidden; } #character { position: absolute; width: 50px; height: 50px; background-color: #3b82f6; border-radius: 50%; } .word { position: absolute; background-color: #10b981; color: white; padding: 5px 10px; border-radius: 5px; } </style> </head> <body class="bg-gray-100"> <div class="container mx-auto p-4"> <h1 class="text-3xl font-bold mb-4">Side-Scrolling Word Typing Game</h1> <div id="game-container" class="bg-white border border-gray-300 rounded-lg mb-4"> <div id="character"></div> </div> <input type="text" id="word-input" class="border border-gray-300 rounded px-2 py-1 mb-4" placeholder="Type the word"> <div class="flex justify-between"> <p class="text-xl">Points: <span id="points">0</span></p> <button id="start-btn" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded">Start Game</button> </div> </div> <script> const gameContainer = document.getElementById('game-container'); const character = document.getElementById('character'); const wordInput = document.getElementById('word-input'); const pointsElement = document.getElementById('points'); const startButton = document.getElementById('start-btn'); let words = ['apple', 'banana', 'orange', 'grape', 'watermelon', 'pineapple', 'strawberry', 'mango', 'kiwi', 'peach']; let points = 0; let gameLoop; function startGame() { points = 0; pointsElement.textContent = points; wordInput.value = ''; character.style.left = '0'; gameContainer.innerHTML = '<div id="character"></div>'; gameLoop = setInterval(moveCharacter, 20); spawnWords(); } function moveCharacter() { const characterLeft = parseInt(character.style.left) || 0; if (characterLeft < gameContainer.offsetWidth - character.offsetWidth) { character.style.left = `${characterLeft + 1}px`; } else { clearInterval(gameLoop); } } function spawnWords() { const word = words[Math.floor(Math.random() * words.length)]; const wordElement = document.createElement('div'); wordElement.className = 'word'; wordElement.textContent = word; wordElement.style.left = `${gameContainer.offsetWidth}px`; wordElement.style.top = `${Math.random() * (gameContainer.offsetHeight - 30)}px`; gameContainer.appendChild(wordElement); const wordLoop = setInterval(() => { const wordLeft = parseInt(wordElement.style.left); if (wordLeft > 0) { wordElement.style.left = `${wordLeft - 1}px`; if (isColliding(character, wordElement)) { clearInterval(wordLoop); wordInput.focus(); wordInput.onkeyup = function(event) { if (event.key === 'Enter' && wordInput.value.trim().toLowerCase() === word.toLowerCase()) { const elapsedTime = Date.now() - wordElement.dataset.startTime; const pointsEarned = Math.max(1000 - elapsedTime, 0); points += pointsEarned; pointsElement.textContent = points; wordInput.value = ''; wordElement.remove(); spawnWords(); } }; } } else { clearInterval(wordLoop); wordElement.remove(); spawnWords(); } }, 20); wordElement.dataset.startTime = Date.now(); } function isColliding(el1, el2) { const rect1 = el1.getBoundingClientRect(); const rect2 = el2.getBoundingClientRect(); return !( rect1.bottom < rect2.top || rect1.top > rect2.bottom || rect1.right < rect2.left || rect1.left > rect2.right ); } startButton.addEventListener('click', startGame); document.addEventListener('keydown', function(event) { const characterLeft = parseInt(character.style.left) || 0; if (event.key === 'a' && characterLeft > 0) { character.style.left = `${characterLeft - 10}px`; } else if (event.key === 'd' && characterLeft < gameContainer.offsetWidth - character.offsetWidth) { character.style.left = `${characterLeft + 10}px`; } else if (event.key === 'w' && parseInt(character.style.top) > 0) { character.style.top = `${parseInt(character.style.top) - 10}px`; } else if (event.key === 's' && parseInt(character.style.top) < gameContainer.offsetHeight - character.offsetHeight) { character.style.top = `${parseInt(character.style.top) + 10}px`; } }); </script> </body> </html>
This web app uses HTML, CSS, and JavaScript to create a side-scrolling game where the player controls a character using the WASD keys. The character moves from left to right, and words randomly appear on the screen. When the character collides with a word, the player must type the word correctly in the input field and press Enter. The faster the player types the word, the more points they earn. The points are displayed in the top-right corner of the screen.
The game uses Tailwind CSS classes for styling, giving it an aesthetic appearance. The character is represented by a blue circle, and the words are displayed as green pills.
To start the game, click the "Start Game" button. The character will begin moving from left to right, and words will spawn randomly. Use the WASD keys to move the character and try to collide with the words. Type the word correctly in the input field and press Enter to earn points based on how quickly you typed the word.
Note: Make sure you have an internet connection to load the Tailwind CSS library from the CDN.
API request
pythonimport anthropic client = anthropic.Anthropic( # defaults to os.environ.get("ANTHROPIC_API_KEY") api_key="my_api_key", ) message = client.messages.create( model="claude-3-opus-20240229", max_tokens=2000, temperature=0, messages=[{"role":"user","content":"Write me a fully complete web app as a single HTML file. The app should contain a simple side-scrolling game where I use WASD to move around. When moving around the world, occasionally the character/sprite will encounter words. When a word is encountered, the player must correctly type the word as fast as possible.The faster the word is successfully typed, the more point the player gets. We should have a counter in the top-right to keep track of points. Words should be random and highly variable to keep the game interesting. \n\nYou should make the website very aesthetic and use Tailwind."}] ) print(message.content)