Spaces:
Running
Running
File size: 15,160 Bytes
673d355 a100ff8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | <!DOCTYPE html>
<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>
|