Could someone assist me with creating an invisible background color that appears after 10 seconds of losing a game? Also, I need to add a "Restart" button that resets the game.
I have tried researching online for ways to implement the restart function, but it seems complicated. Can someone explain a simple method as I am just a student and prefer straightforward solutions?
const width = $(document).width() - 200 // window html width 1366
const height = $(document).height() - 200 // window html height 600
let timer = 10
let points = 0
let isClick = false
let intervalID
$('.item-1').click(function () {
setRandomPosition()
$('.points').text(points)
points += 10
if(points == 100) {
endGame('You Win')
clearInterval(intervalID)
}
})
let intervalID = setInterval(function() {
if(timer > 0) {
timer--
$('.timer').text(timer)
} else {
endGame('You Lose')
}
}, 1000)
function setRandomPosition() {
$('.item-1').css({
'top' : Math.floor(Math.random() * height),
'left' : Math.floor(Math.random() * width)
})
}
function endGame(endText) {
$('.end').css('display', 'flex')
$('h1').text(endText)
}
* {
margin: 0;
padding: 0;
}
.item {
width: 200px;
aspect-ratio: 1/1;
background-color: lightpink;
position: absolute;
}
.timer {
font-size: 65px;
text-align: center;
position: absolute;
left: 0;
right: 0;
z-index: 1;
}
.points {
font-size: 65px;
position: absolute;
right: 20px;
z-index: 1;
}
.end {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.497);
align-items: center;
justify-content: center;
}
.restart {
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="item item-1"></div>
<p class="timer">10</p>
<p class="points">0</p>
<div class="end">
<h1></h1>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="./script.js"></script>
</body>
</html>