You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
7.6 KiB
234 lines
7.6 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Guess the Number Game</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
background: linear-gradient(to right, #ff7e5f, #feb47b);
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
margin: 0;
|
|
text-align: center;
|
|
flex-direction: column;
|
|
color: white;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 36px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.game-container {
|
|
width: 300px;
|
|
padding: 20px;
|
|
background-color: rgba(0, 0, 0, 0.7);
|
|
border-radius: 8px;
|
|
box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
|
|
}
|
|
|
|
input, .btn {
|
|
padding: 10px;
|
|
font-size: 16px;
|
|
margin: 10px 0;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
input {
|
|
width: 60%;
|
|
border: 1px solid #ccc;
|
|
background-color: #fff;
|
|
color: #333;
|
|
}
|
|
|
|
input:focus {
|
|
outline: none;
|
|
border-color: #0779e4;
|
|
}
|
|
|
|
.btn {
|
|
width: 100%;
|
|
background-color: #0779e4;
|
|
color: white;
|
|
border: none;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.btn:hover {
|
|
background-color: #005bb5;
|
|
}
|
|
|
|
.feedback {
|
|
margin: 10px 0;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.attempts, .score, .timer {
|
|
font-size: 20px;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.game-over {
|
|
font-size: 24px;
|
|
color: #ff6347;
|
|
margin-top: 30px;
|
|
}
|
|
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
|
|
.timer {
|
|
font-size: 24px;
|
|
color: yellow;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
@media (max-width: 500px) {
|
|
.game-container {
|
|
width: 250px;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 28px;
|
|
}
|
|
|
|
input, .btn {
|
|
font-size: 14px;
|
|
}
|
|
|
|
.attempts, .score, .timer {
|
|
font-size: 16px;
|
|
}
|
|
|
|
.game-over {
|
|
font-size: 20px;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Guess the Number Game</h1>
|
|
<div class="game-container" id="game-container">
|
|
<p>Guess a number between 0 and 100:</p>
|
|
<input type="number" id="guessInput" min="0" max="100" placeholder="Enter a number" />
|
|
<button class="btn" id="submitGuessButton">Submit Guess</button>
|
|
<p id="feedback" class="feedback"></p>
|
|
<p id="attempts" class="attempts">Attempts: 0</p>
|
|
<p id="score" class="attempts">Score: 0</p>
|
|
<p id="timer" class="timer">Time Left: 30s</p>
|
|
<button class="btn hidden" id="nextLevelButton" onclick="nextLevel()">Next Level</button>
|
|
<button class="btn hidden" id="restartButton" onclick="restartGame()">Play Again</button>
|
|
<p id="gameOverMessage" class="game-over hidden">Game Over! Your Final Score: 0</p>
|
|
</div>
|
|
|
|
<script>
|
|
let secretNumber;
|
|
let attempts;
|
|
let score;
|
|
let gameOver;
|
|
let timer;
|
|
let timeLeft;
|
|
let maxNumber = 100; // Max number is fixed at 100
|
|
|
|
function startGame() {
|
|
secretNumber = Math.floor(Math.random() * (maxNumber + 1)); // Generate number between 0 and 100
|
|
attempts = 0;
|
|
score = 0;
|
|
gameOver = false;
|
|
timeLeft = 30;
|
|
|
|
document.getElementById('feedback').textContent = '';
|
|
document.getElementById('attempts').textContent = `Attempts: ${attempts}`;
|
|
document.getElementById('score').textContent = `Score: ${score}`;
|
|
document.getElementById('timer').textContent = `Time Left: ${timeLeft}s`;
|
|
document.getElementById('guessInput').disabled = false;
|
|
document.getElementById('submitGuessButton').disabled = false;
|
|
document.getElementById('gameOverMessage').classList.add('hidden');
|
|
document.getElementById('restartButton').classList.add('hidden');
|
|
document.getElementById('nextLevelButton').classList.add('hidden');
|
|
|
|
// Start the timer
|
|
timer = setInterval(function () {
|
|
if (timeLeft > 0) {
|
|
timeLeft--;
|
|
document.getElementById('timer').textContent = `Time Left: ${timeLeft}s`;
|
|
} else {
|
|
gameOver = true;
|
|
clearInterval(timer);
|
|
endGame();
|
|
}
|
|
}, 1000);
|
|
}
|
|
|
|
function checkGuess() {
|
|
const guess = parseInt(document.getElementById('guessInput').value);
|
|
if (gameOver || isNaN(guess)) return;
|
|
|
|
attempts++;
|
|
|
|
if (guess === secretNumber) {
|
|
score += 10; // Add points for correct guess
|
|
document.getElementById('feedback').textContent = `Congratulations! You guessed it right! The number was ${secretNumber}.`;
|
|
document.getElementById('feedback').style.color = 'green';
|
|
document.getElementById('feedback').style.fontWeight = 'bold';
|
|
gameOver = true;
|
|
document.getElementById('nextLevelButton').classList.remove('hidden'); // Show next level button
|
|
clearInterval(timer); // Stop the timer
|
|
} else if (Math.abs(guess - secretNumber) <= 10) {
|
|
document.getElementById('feedback').textContent = `Close to the answer! Your guess is within 10 of the number.`;
|
|
document.getElementById('feedback').style.color = 'orange';
|
|
} else if (guess < secretNumber) {
|
|
document.getElementById('feedback').textContent = 'Too low! Try again.';
|
|
document.getElementById('feedback').style.color = 'red';
|
|
} else if (guess > secretNumber) {
|
|
document.getElementById('feedback').textContent = 'Too high! Try again.';
|
|
document.getElementById('feedback').style.color = 'red';
|
|
}
|
|
|
|
document.getElementById('attempts').textContent = `Attempts: ${attempts}`;
|
|
document.getElementById('score').textContent = `Score: ${score}`;
|
|
|
|
if (gameOver) {
|
|
document.getElementById('guessInput').disabled = true;
|
|
document.getElementById('submitGuessButton').disabled = true;
|
|
}
|
|
}
|
|
|
|
function nextLevel() {
|
|
// No change to maxNumber because range stays between 0 and 100
|
|
document.getElementById('nextLevelButton').classList.add('hidden');
|
|
startGame(); // Restart game with same range
|
|
}
|
|
|
|
function endGame() {
|
|
document.getElementById('guessInput').disabled = true;
|
|
document.getElementById('submitGuessButton').disabled = true;
|
|
document.getElementById('gameOverMessage').textContent = `Game Over! The correct number was ${secretNumber}. Your Final Score: ${score}`;
|
|
document.getElementById('gameOverMessage').classList.remove('hidden');
|
|
document.getElementById('restartButton').classList.remove('hidden');
|
|
}
|
|
|
|
function restartGame() {
|
|
startGame();
|
|
}
|
|
|
|
// Start a new game when the page loads
|
|
startGame();
|
|
|
|
// Event listener for the submit guess button
|
|
document.getElementById('submitGuessButton').addEventListener('click', checkGuess);
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|