web前端开发打地鼠小游戏HTML+CSS+JS
时间: 2023-09-06 07:09:13 浏览: 117
好的,下面我来给你讲解一下如何用 HTML、CSS 和 JS 开发一个打地鼠小游戏。
首先,在 HTML 中创建游戏界面,可以使用以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打地鼠小游戏</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
width: 400px;
height: 400px;
margin: auto;
}
.hole {
width: 100px;
height: 100px;
background-color: #ccc;
border: 1px solid #000;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
}
.hole:hover {
background-color: #999;
}
</style>
</head>
<body>
<div class="container">
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
</div>
<script src="script.js"></script>
</body>
</html>
```
在上面的代码中,我们创建了一个包含 9 个洞口的容器,每个洞口都是一个 div 元素,样式为灰色背景、黑色边框、30px 的字号、光标为手型,并且当鼠标悬浮在洞口上时会变为深灰色背景。其中容器的样式使用了 flex 布局,可以使洞口自动排列。
接下来,我们使用 JS 来实现地鼠的随机出现和消失,代码如下:
```js
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const moles = document.querySelectorAll('.mole');
let lastHole;
let timeUp = false;
let score = 0;
function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function randomHole(holes) {
const idx = Math.floor(Math.random() * holes.length);
const hole = holes[idx];
if (hole === lastHole) {
return randomHole(holes);
}
lastHole = hole;
return hole;
}
function peep() {
const time = randomTime(200, 1000);
const hole = randomHole(holes);
hole.classList.add('up');
setTimeout(() => {
hole.classList.remove('up');
if (!timeUp) peep();
}, time);
}
function startGame() {
scoreBoard.textContent = 0;
timeUp = false;
score = 0;
peep();
setTimeout(() => {
timeUp = true;
}, 10000);
}
function bonk(e) {
if (!e.isTrusted) return;
score++;
this.classList.remove('up');
scoreBoard.textContent = score;
}
moles.forEach(mole => mole.addEventListener('click', bonk));
```
在上面的代码中,我们首先获取了 HTML 中所有的洞口和地鼠元素,并定义了一些变量用来存储上一个出现的洞口、游戏是否结束和得分等信息。接着,我们实现了三个函数:
1. `randomTime`:用于生成一个随机时间,表示地鼠出现和消失的时间间隔。
2. `randomHole`:用于随机选择一个洞口,保证地鼠每次出现的位置不同。
3. `peep`:用于控制地鼠的随机出现和消失,每次随机生成一个时间和一个洞口,将该洞口的样式修改为出现状态并在一定时间后恢复为原来的样式,然后在一定时间后再次调用该函数。
最后,我们实现了开始游戏和打地鼠的函数,并将打地鼠的函数绑定到每个地鼠元素的点击事件上。在打地鼠函数中,我们判断是否由用户点击触发,如果不是则直接返回,否则将得分加一并将该地鼠的样式修改为消失状态。
完成以上代码后,你就可以在浏览器中打开该 HTML 文件,开始你的打地鼠小游戏了!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)