o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
write simple html game
- User
write simple html game
- Assistant
write simple html game
Sure! Let's create a simple "Guess the Number" game using HTML, CSS, and JavaScript. In this game, the computer will randomly choose a number between 1 and 100, and the player has to guess the number. The game will provide feedback whether the guess is too high, too low, or correct.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Guess the Number Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f8ff;
text-align: center;
margin-top: 50px;
}
h1 {
color: #333;
}
#game {
display: inline-block;
padding: 20px;
border: 2px solid #333;
border-radius: 10px;
background-color: #fff;
}
input[type="number"] {
padding: 10px;
width: 60px;
font-size: 16px;
}
button {
padding: 10px 20px;
font-size: 16px;
margin-left: 10px;
cursor: pointer;
}
#message {
margin-top: 20px;
font-size: 18px;
height: 24px;
}
#reset {
margin-top: 20px;
display: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #ff4d4d;
color: #fff;
border: none;
border-radius: 5px;
}
#reset:hover {
background-color: #ff1a1a;
}
</style>
</head>
<body>
<h1>Guess the Number!</h1>
<div id="game">
<p>I have selected a number between 1 and 100.</p>
<p>Can you guess it?</p>
<input type="number" id="guessInput" min="1" max="100" />
<button onclick="checkGuess()">Guess</button>
<div id="message"></div>
<button id="reset" onclick="resetGame()">Play Again</button>
</div>
<script>
let secretNumber;
let attempts;
function initializeGame() {
secretNumber = Math.floor(Math.random() * 100) + 1;
attempts = 0;
document.getElementById('message').textContent = '';
document.getElementById('guessInput').value = '';
document.getElementById('reset').style.display = 'none';
}
function checkGuess() {
const guess = Number(document.getElementById('guessInput').value);
const message = document.getElementById('message');
if (!guess || guess < 1 || guess > 100) {
message.textContent = 'Please enter a valid number between 1 and 100.';
return;
}
attempts++;
if (guess === secretNumber) {
message.textContent = `Congratulations! You guessed the number in ${attempts} attempts.`;
document.getElementById('reset').style.display = 'inline-block';
} else if (guess < secretNumber) {
message.textContent = 'Too low! Try again.';
} else {
message.textContent = 'Too high! Try again.';
}
}
function resetGame() {
initializeGame();
}
// Initialize the game when the page loads
window.onload = initializeGame;
</script>
</body>
</html>
.html extension, for example, guess_the_number.html.HTML Structure:
<h1> tag displays the game title.<div id="game"> contains all game elements:
<input> field for the player to enter their guess.<div id="message"> to display feedback messages.CSS Styling:
JavaScript Functionality:
initializeGame() to start a new game.Feel free to customize the game further by adding more features, such as keeping track of the highest score or limiting the number of attempts!
Not yet included in a published release.