Javascript网页点击计数小游戏
作者:庄泽峰 / 2023-12-27 15:26
Hello,GPT,请你用html5+CSS3来实现: 一个网页小游戏。然后GPT给出下面这个点击计数的小游戏,但显然该网页游戏Javascript来调动的。大家可以运行查看下面代码:
<!DOCTYPE html>
<html>
<head>
<title>点击游戏</title>
<style>
body {
text-align: center;
}
#game-container {
margin-top: 100px;
}
.circle {
width: 100px;
height: 100px;
background-color: red;
border-radius: 50%;
display: inline-block;
margin: 10px;
cursor: pointer;
transition: background-color 0.3s;
}
.circle:hover {
background-color: green;
}
#score {
font-size: 24px;
margin-top: 20px;
}
#restart-button {
margin-top: 20px;
padding: 10px 20px;
font-size: 18px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
#restart-button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>点击游戏</h1>
<div id="game-container">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
<div id="score">分数: <span id="score-value">0</span></div>
<button id="restart-button" style="display: none;">再来一次</button>
<script>
var circles = document.getElementsByClassName('circle');
var score = 0;
var scoreValue = document.getElementById('score-value');
var restartButton = document.getElementById('restart-button');
function increaseScore() {
score++;
scoreValue.innerText = score;
}
function restartGame() {
score = 0;
scoreValue.innerText = score;
restartButton.style.display = 'none';
for (var i = 0; i < circles.length; i++) {
circles[i].style.display = 'inline-block';
}
}
for (var i = 0; i < circles.length; i++) {
circles[i].addEventListener('click', function() {
this.style.display = 'none';
increaseScore();
var remainingCircles = document.querySelectorAll('.circle:not([style*=\"display: none\"])');
if (remainingCircles.length === 0) {
restartButton.style.display = 'block';
}
});
}
restartButton.addEventListener('click', restartGame);
</script>
</body>
</html>
在这个修复后的示例中,我在点击事件监听器中添加了一段代码,用于检查剩余的圆形元素数量。如果剩余的圆形元素数量为0,即所有圆形元素都被点击消失,那么"再来一次"按钮将会显示出来。
标签: JavaScript 分类: 网页语言