Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Hangman Hero</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script> | |
| <style> | |
| @keyframes bounce { | |
| 0%, 100% { transform: translateY(0); } | |
| 50% { transform: translateY(-10px); } | |
| } | |
| .animate-bounce-slow { | |
| animation: bounce 1.5s infinite; | |
| } | |
| .keyboard-key { | |
| transition: all 0.2s ease; | |
| } | |
| .keyboard-key:hover { | |
| transform: scale(1.1); | |
| } | |
| .keyboard-key:active { | |
| transform: scale(0.95); | |
| } | |
| .hangman-part { | |
| opacity: 0; | |
| transition: opacity 0.3s ease; | |
| } | |
| .hangman-part.visible { | |
| opacity: 1; | |
| } | |
| </style> | |
| </head> | |
| <body class="bg-gray-100 min-h-screen flex flex-col"> | |
| <header class="bg-white shadow-sm py-4"> | |
| <div class="container mx-auto px-4"> | |
| <h1 class="text-3xl font-bold text-center text-gray-800"> | |
| <i data-feather="help-circle" class="inline mr-2 text-indigo-500"></i> | |
| Hangman Hero | |
| </h1> | |
| </div> | |
| </header> | |
| <main class="flex-grow container mx-auto px-4 py-8"> | |
| <div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden p-6"> | |
| <!-- Game Stats --> | |
| <div class="flex justify-between mb-6"> | |
| <div class="text-center"> | |
| <p class="text-sm text-gray-500">Score</p> | |
| <p class="text-2xl font-bold" id="score">0</p> | |
| </div> | |
| <div class="text-center"> | |
| <p class="text-sm text-gray-500">Lives</p> | |
| <p class="text-2xl font-bold text-red-500" id="lives">6</p> | |
| </div> | |
| <div class="text-center"> | |
| <p class="text-sm text-gray-500">Level</p> | |
| <p class="text-2xl font-bold" id="level">1</p> | |
| </div> | |
| </div> | |
| <!-- Hangman Drawing --> | |
| <div class="relative h-48 mb-8 flex justify-center"> | |
| <svg width="200" height="180" viewBox="0 0 200 180" class="mx-auto"> | |
| <!-- Gallows --> | |
| <line x1="20" y1="170" x2="120" y2="170" stroke="black" stroke-width="4" /> | |
| <line x1="40" y1="20" x2="40" y2="170" stroke="black" stroke-width="4" /> | |
| <line x1="40" y1="20" x2="100" y2="20" stroke="black" stroke-width="4" /> | |
| <line x1="100" y1="20" x2="100" y2="40" stroke="black" stroke-width="4" /> | |
| <!-- Hangman Parts (initially hidden) --> | |
| <circle cx="100" cy="60" r="20" stroke="black" stroke-width="3" fill="none" class="hangman-part" id="head"/> | |
| <line x1="100" y1="80" x2="100" y2="120" stroke="black" stroke-width="3" class="hangman-part" id="body"/> | |
| <line x1="100" y1="100" x2="80" y2="80" stroke="black" stroke-width="3" class="hangman-part" id="left-arm"/> | |
| <line x1="100" y1="100" x2="120" y2="80" stroke="black" stroke-width="3" class="hangman-part" id="right-arm"/> | |
| <line x1="100" y1="120" x2="80" y2="150" stroke="black" stroke-width="3" class="hangman-part" id="left-leg"/> | |
| <line x1="100" y1="120" x2="120" y2="150" stroke="black" stroke-width="3" class="hangman-part" id="right-leg"/> | |
| </svg> | |
| </div> | |
| <!-- Word Display --> | |
| <div class="mb-8 text-center"> | |
| <div class="flex justify-center space-x-2 mb-4" id="word-display"></div> | |
| <p class="text-sm text-gray-500" id="category">Category: Loading...</p> | |
| </div> | |
| <!-- Keyboard --> | |
| <div class="grid grid-cols-7 gap-2 mb-6" id="keyboard"> | |
| <!-- Letters will be added by JavaScript --> | |
| </div> | |
| <!-- Game Controls --> | |
| <div class="flex justify-center space-x-4"> | |
| <button id="hint-btn" class="px-4 py-2 bg-yellow-500 text-white rounded-lg hover:bg-yellow-600 transition"> | |
| <i data-feather="lightbulb" class="inline mr-1"></i> Hint | |
| </button> | |
| <button id="new-game-btn" class="px-4 py-2 bg-indigo-500 text-white rounded-lg hover:bg-indigo-600 transition"> | |
| <i data-feather="refresh-cw" class="inline mr-1"></i> New Game | |
| </button> | |
| </div> | |
| </div> | |
| </main> | |
| <!-- Game Over Modal --> | |
| <div id="game-over-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden"> | |
| <div class="bg-white rounded-xl p-6 max-w-sm w-full mx-4"> | |
| <h2 class="text-2xl font-bold mb-4 text-center" id="game-over-title">Game Over</h2> | |
| <p class="text-center mb-4" id="game-over-message">The word was: <span class="font-bold" id="correct-word"></span></p> | |
| <div class="flex justify-center"> | |
| <button id="play-again-btn" class="px-4 py-2 bg-indigo-500 text-white rounded-lg hover:bg-indigo-600 transition"> | |
| Play Again | |
| </button> | |
| </div> | |
| </div> | |
| </div> | |
| <footer class="bg-white py-4 text-center text-gray-600 text-sm"> | |
| <p>Made with <i data-feather="heart" class="inline text-red-500"></i> for word lovers</p> | |
| </footer> | |
| <script> | |
| document.addEventListener('DOMContentLoaded', function() { | |
| feather.replace(); | |
| // Game variables | |
| let score = 0; | |
| let lives = 6; | |
| let level = 1; | |
| let word = ''; | |
| let guessedLetters = []; | |
| let category = ''; | |
| let hintUsed = false; | |
| // DOM elements | |
| const wordDisplay = document.getElementById('word-display'); | |
| const keyboard = document.getElementById('keyboard'); | |
| const scoreElement = document.getElementById('score'); | |
| const livesElement = document.getElementById('lives'); | |
| const levelElement = document.getElementById('level'); | |
| const categoryElement = document.getElementById('category'); | |
| const hintBtn = document.getElementById('hint-btn'); | |
| const newGameBtn = document.getElementById('new-game-btn'); | |
| const gameOverModal = document.getElementById('game-over-modal'); | |
| const gameOverTitle = document.getElementById('game-over-title'); | |
| const gameOverMessage = document.getElementById('game-over-message'); | |
| const correctWord = document.getElementById('correct-word'); | |
| const playAgainBtn = document.getElementById('play-again-btn'); | |
| // Word categories and words | |
| const wordCategories = { | |
| animals: ['elephant', 'giraffe', 'kangaroo', 'octopus', 'rhinoceros'], | |
| fruits: ['watermelon', 'strawberry', 'pineapple', 'blueberry', 'raspberry'], | |
| countries: ['australia', 'brazil', 'canada', 'denmark', 'ethiopia'], | |
| sports: ['basketball', 'football', 'swimming', 'volleyball', 'wrestling'] | |
| }; | |
| // Initialize game | |
| function initGame() { | |
| // Reset game state | |
| lives = 6; | |
| guessedLetters = []; | |
| hintUsed = false; | |
| // Update UI | |
| livesElement.textContent = lives; | |
| updateHangmanDrawing(); | |
| // Select random category and word | |
| const categories = Object.keys(wordCategories); | |
| category = categories[Math.floor(Math.random() * categories.length)]; | |
| const words = wordCategories[category]; | |
| word = words[Math.floor(Math.random() * words.length)]; | |
| // Display word | |
| displayWord(); | |
| categoryElement.textContent = `Category: ${category.charAt(0).toUpperCase() + category.slice(1)}`; | |
| // Create keyboard | |
| createKeyboard(); | |
| } | |
| // Display the word with blanks for unguessed letters | |
| function displayWord() { | |
| wordDisplay.innerHTML = ''; | |
| let allLettersGuessed = true; | |
| for (let letter of word) { | |
| const letterElement = document.createElement('div'); | |
| letterElement.className = 'w-8 h-8 flex items-center justify-center text-xl font-bold border-b-2 border-gray-800'; | |
| if (guessedLetters.includes(letter)) { | |
| letterElement.textContent = letter; | |
| } else { | |
| letterElement.textContent = '_'; | |
| allLettersGuessed = false; | |
| } | |
| wordDisplay.appendChild(letterElement); | |
| } | |
| // Check if player won | |
| if (allLettersGuessed) { | |
| winGame(); | |
| } | |
| } | |
| // Create keyboard buttons | |
| function createKeyboard() { | |
| keyboard.innerHTML = ''; | |
| const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
| for (let letter of alphabet) { | |
| const key = document.createElement('button'); | |
| key.className = 'keyboard-key px-2 py-2 bg-gray-200 rounded hover:bg-gray-300 font-bold'; | |
| key.textContent = letter; | |
| key.dataset.letter = letter; | |
| if (guessedLetters.includes(letter)) { | |
| key.disabled = true; | |
| key.classList.add('bg-gray-400', 'text-gray-600'); | |
| } | |
| key.addEventListener('click', () => guessLetter(letter)); | |
| keyboard.appendChild(key); | |
| } | |
| } | |
| // Handle letter guess | |
| function guessLetter(letter) { | |
| if (guessedLetters.includes(letter)) return; | |
| guessedLetters.push(letter); | |
| // Update keyboard | |
| const keyButtons = document.querySelectorAll('.keyboard-key'); | |
| keyButtons.forEach(btn => { | |
| if (btn.dataset.letter === letter) { | |
| btn.disabled = true; | |
| if (word.includes(letter)) { | |
| btn.classList.add('bg-green-500', 'text-white'); | |
| } else { | |
| btn.classList.add('bg-red-500', 'text-white'); | |
| lives--; | |
| livesElement.textContent = lives; | |
| updateHangmanDrawing(); | |
| } | |
| } | |
| }); | |
| // Update word display | |
| displayWord(); | |
| // Check if game over | |
| if (lives <= 0) { | |
| loseGame(); | |
| } | |
| } | |
| // Update hangman drawing based on lives left | |
| function updateHangmanDrawing() { | |
| const parts = ['head', 'body', 'left-arm', 'right-arm', 'left-leg', 'right-leg']; | |
| const visibleParts = 6 - lives; | |
| parts.forEach((part, index) => { | |
| const element = document.getElementById(part); | |
| if (index < visibleParts) { | |
| element.classList.add('visible'); | |
| } else { | |
| element.classList.remove('visible'); | |
| } | |
| }); | |
| } | |
| // Player wins | |
| function winGame() { | |
| score += level * 100; | |
| if (!hintUsed) score += 50; // Bonus for not using hint | |
| level++; | |
| scoreElement.textContent = score; | |
| levelElement.textContent = level; | |
| // Show win message | |
| gameOverTitle.textContent = 'You Win!'; | |
| gameOverMessage.textContent = `Great job! The word was:`; | |
| correctWord.textContent = word; | |
| gameOverModal.classList.remove('hidden'); | |
| } | |
| // Player loses | |
| function loseGame() { | |
| // Show lose message | |
| gameOverTitle.textContent = 'Game Over'; | |
| gameOverMessage.textContent = 'The word was:'; | |
| correctWord.textContent = word; | |
| gameOverModal.classList.remove('hidden'); | |
| } | |
| // Provide hint | |
| function provideHint() { | |
| if (hintUsed) return; | |
| // Find first unguessed letter | |
| for (let letter of word) { | |
| if (!guessedLetters.includes(letter)) { | |
| hintUsed = true; | |
| hintBtn.disabled = true; | |
| hintBtn.classList.add('bg-yellow-400', 'cursor-not-allowed'); | |
| // Highlight the letter in the word display | |
| const letterElements = wordDisplay.querySelectorAll('div'); | |
| word.split('').forEach((char, index) => { | |
| if (char === letter) { | |
| letterElements[index].classList.add('animate-bounce-slow', 'text-yellow-500'); | |
| } | |
| }); | |
| // Flash the corresponding keyboard key | |
| const keyButtons = document.querySelectorAll('.keyboard-key'); | |
| keyButtons.forEach(btn => { | |
| if (btn.dataset.letter === letter) { | |
| btn.classList.add('animate-bounce-slow', 'bg-yellow-300'); | |
| } | |
| }); | |
| break; | |
| } | |
| } | |
| } | |
| // Event listeners | |
| hintBtn.addEventListener('click', provideHint); | |
| newGameBtn.addEventListener('click', initGame); | |
| playAgainBtn.addEventListener('click', () => { | |
| gameOverModal.classList.add('hidden'); | |
| initGame(); | |
| }); | |
| // Start the game | |
| initGame(); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |