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.
 
 
 
Code2C/math game.html

223 lines
7.0 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Math Battle Game</title>
<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
text-align: center;
}
.container {
margin-top: 50px;
}
.score, .timer {
font-size: 24px;
margin-bottom: 20px;
}
.question {
font-size: 32px;
margin: 20px 0;
}
.input-box {
margin: 20px 0;
}
.input-box input {
padding: 10px;
font-size: 18px;
width: 200px;
}
.btn {
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #0779e4;
color: white;
border: none;
border-radius: 5px;
}
.btn:hover {
background-color: #005bb5;
}
.result {
font-size: 24px;
margin: 20px 0;
}
.final-score {
font-size: 36px;
color: green;
margin-top: 20px;
}
#try-again-btn, #go-back-btn {
display: none;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #ff6347;
color: white;
border: none;
border-radius: 5px;
margin-top: 20px;
}
#try-again-btn:hover, #go-back-btn:hover {
background-color: #e55347;
}
</style>
</head>
<body>
<div class="container">
<h1>Math Battle Game</h1>
<div class="score">
Score: <span id="score">0</span>
</div>
<div class="timer">
Time Left: <span id="timer">10</span> seconds
</div>
<div class="question" id="question">What is 5 + 3?</div>
<div class="input-box">
<input type="number" id="answer" placeholder="Enter your answer">
</div>
<button class="btn" onclick="checkAnswer()">Submit</button>
<div class="result" id="result"></div>
<div id="final-score" class="final-score"></div>
<button id="try-again-btn" class="btn" onclick="tryAgain()">Try Again</button>
<button id="go-back-btn" class="btn" onclick="goBack()">Go Back</button>
</div>
<script>
let score = 0;
let timeLeft = 10;
let questionCount = 0;
const totalQuestions = 10;
let timerInterval;
let correctAnswer;
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateQuestion() {
const num1 = getRandomNumber(1, 20);
const num2 = getRandomNumber(1, 20);
const operators = ['+', '-', '*'];
const operator = operators[Math.floor(Math.random() * operators.length)];
return {
question: `${num1} ${operator} ${num2}`,
answer: eval(`${num1} ${operator} ${num2}`)
};
}
function displayNewQuestion() {
const questionData = generateQuestion();
document.getElementById('question').textContent = `What is ${questionData.question}?`;
return questionData.answer;
}
function startTimer() {
timeLeft = 10;
document.getElementById('timer').textContent = timeLeft;
timerInterval = setInterval(function () {
timeLeft--;
document.getElementById('timer').textContent = timeLeft;
if (timeLeft <= 0) {
clearInterval(timerInterval);
handleTimeout();
}
}, 1000);
}
function handleTimeout() {
document.getElementById('result').textContent = 'Time is up! You lose a point.';
document.getElementById('result').style.color = 'red';
score = Math.max(0, score - 1);
document.getElementById('score').textContent = score;
correctAnswer = displayNewQuestion();
startTimer();
questionCount++;
if (questionCount >= totalQuestions) {
endGame();
}
}
function checkAnswer() {
const userAnswer = Number(document.getElementById('answer').value);
clearInterval(timerInterval);
const resultElement = document.getElementById('result');
if (userAnswer === correctAnswer) {
resultElement.textContent = 'Correct!';
resultElement.style.color = 'green';
score++;
} else {
resultElement.textContent = 'Wrong! Try again.';
resultElement.style.color = 'red';
}
document.getElementById('score').textContent = score;
document.getElementById('answer').value = '';
correctAnswer = displayNewQuestion();
startTimer();
questionCount++;
if (questionCount >= totalQuestions) {
endGame();
}
}
function endGame() {
document.getElementById('question').textContent = 'Game Over!';
document.getElementById('result').textContent = '';
document.getElementById('timer').style.display = 'none';
document.getElementById('final-score').textContent = `Your Final Score is: ${score}`;
document.getElementById('try-again-btn').style.display = 'inline-block'; // Show "Try Again" button
document.getElementById('go-back-btn').style.display = 'inline-block'; // Show "Go Back" button
}
function tryAgain() {
// Reset the game
score = 0;
timeLeft = 10;
questionCount = 0;
document.getElementById('score').textContent = score;
document.getElementById('timer').style.display = 'block';
document.getElementById('final-score').textContent = '';
document.getElementById('try-again-btn').style.display = 'none'; // Hide "Try Again" button
document.getElementById('go-back-btn').style.display = 'none'; // Hide "Go Back" button
correctAnswer = displayNewQuestion();
startTimer();
}
function goBack() {
// Go back to the previous page or homepage
window.history.back(); // This will take the user to the previous page
// Or you can redirect to a homepage like:
// window.location.href = 'index.html'; // Uncomment and replace with your homepage
}
// Initialize the first question and start the timer
correctAnswer = displayNewQuestion();
startTimer();
</script>
</body>
</html>