|
| 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>Pokémon Guessing Game</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + display: flex; |
| 10 | + flex-direction: column; |
| 11 | + align-items: center; |
| 12 | + justify-content: center; |
| 13 | + height: 100vh; |
| 14 | + background-color: #f5f5f5; |
| 15 | + font-family: Arial, sans-serif; |
| 16 | + } |
| 17 | + h1 { |
| 18 | + margin-bottom: 20px; |
| 19 | + } |
| 20 | + img { |
| 21 | + width: 300px; |
| 22 | + height: 300px; |
| 23 | + border-radius: 10px; |
| 24 | + filter: blur(10px); |
| 25 | + transition: filter 0.5s; |
| 26 | + } |
| 27 | + input { |
| 28 | + padding: 10px; |
| 29 | + margin: 10px 0; |
| 30 | + width: 200px; |
| 31 | + } |
| 32 | + button { |
| 33 | + padding: 10px 15px; |
| 34 | + cursor: pointer; |
| 35 | + background-color: #28a745; |
| 36 | + color: white; |
| 37 | + border: none; |
| 38 | + border-radius: 5px; |
| 39 | + } |
| 40 | + button:hover { |
| 41 | + background-color: #218838; |
| 42 | + } |
| 43 | + .message { |
| 44 | + margin-top: 10px; |
| 45 | + font-size: 1.2em; |
| 46 | + color: #ff5733; |
| 47 | + } |
| 48 | + </style> |
| 49 | +</head> |
| 50 | +<body> |
| 51 | + <h1>Pokémon Guessing Game</h1> |
| 52 | + <img id="pokemon-image" src="" alt="Pokemon" /> |
| 53 | + <input type="text" id="guess" placeholder="Guess the Pokémon's name" /> |
| 54 | + <button onclick="checkGuess()">Submit Guess</button> |
| 55 | + <div class="message" id="message"></div> |
| 56 | + |
| 57 | + <script> |
| 58 | + let pokemonName = ''; |
| 59 | + |
| 60 | + async function fetchPokemon() { |
| 61 | + const randomId = Math.floor(Math.random() * 898) + 1; // Random ID between 1 and 898 |
| 62 | + const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${randomId}`); |
| 63 | + const data = await response.json(); |
| 64 | + pokemonName = data.name; |
| 65 | + const imageUrl = data.sprites.front_default; |
| 66 | + document.getElementById('pokemon-image').src = imageUrl; |
| 67 | + document.getElementById('pokemon-image').style.filter = 'blur(10px)'; |
| 68 | + document.getElementById('message').innerText = ''; |
| 69 | + document.getElementById('guess').value = ''; |
| 70 | + } |
| 71 | + |
| 72 | + function checkGuess() { |
| 73 | + const userGuess = document.getElementById('guess').value.toLowerCase(); |
| 74 | + const messageElement = document.getElementById('message'); |
| 75 | + |
| 76 | + if (userGuess === pokemonName) { |
| 77 | + messageElement.innerText = 'Correct! The Pokémon is ' + pokemonName.charAt(0).toUpperCase() + pokemonName.slice(1) + '!'; |
| 78 | + document.getElementById('pokemon-image').style.filter = 'blur(0px)'; |
| 79 | + setTimeout(() => { |
| 80 | + fetchPokemon(); |
| 81 | + }, 3000); // Fetch a new Pokémon after 3 seconds |
| 82 | + } else { |
| 83 | + messageElement.innerText = 'Incorrect! Try again.'; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + window.onload = fetchPokemon; // Fetch a Pokémon when the page loads |
| 88 | + </script> |
| 89 | +</body> |
| 90 | +</html> |
0 commit comments