【JavaScript】简易打地鼠游戏

步骤分析:

  1. 获取相关元素
    1.1 开始按钮
    1.2 分数框
    1.3 所有的地鼠洞
  2. 给开始按钮定义函数startGame
    2.1 隐藏开始按钮
    2.2 清除分数为0: 允许多次玩
    2.3 开启定时器: 间隔时间自定义
    2.3.1 获取随机整数: 0-地鼠洞长度之间
    2.3.2 给随机数对应的地鼠洞添加up类(显示地鼠)
    2.3.3 给当前地鼠洞增加一个延时器: 到时自动清除up类(隐藏地鼠)
  3. 给所有的地鼠(mole)增加点击事件
    3.1 获取所有的地鼠
    3.2 给所有的地鼠绑定点击事件
    3.3 干掉地鼠对应的洞的类up
    3.4 增加分数的数量
    3.5 清理地鼠所在洞本身的延时器

最终的效果:

在这里插入图片描述

html部分

<h1>Whack-a-mole! <span class="score">0</span></h1>
    <p><button class="start">Start</button></p>
 
    <div class="game">
        <div class="hole hole1">
            <div class="mole"></div>
        </div>
        <div class="hole hole2">
            <div class="mole"></div>
        </div>
        <div class="hole hole3">
            <div class="mole"></div>
        </div>
        <div class="hole hole4">
            <div class="mole"></div>
        </div>
        <div class="hole hole5">
            <div class="mole"></div>
        </div>
        <div class="hole hole6">
            <div class="mole"></div>
        </div>
    </div>
 
    <h2 class="game-status"></h2>
	 //引入js文件
    <script src="./index.js"></script>

css部分

			html {
		    box-sizing: border-box;
		    font-size: 10px;
		    background: #ffc600;
		}
		 
		*,
		*:before,
		*:after {
		    box-sizing: inherit;
		}
		 
		body {
		    padding: 0;
		    margin: 0;
		    font-family: 'Amatic SC', cursive;
		}
		 
		h1,
		h2 {
		    text-align: center;
		    font-size: 10rem;
		    line-height: 1;
		    margin-bottom: 0;
		}
		 
		h2 {
		    font-size: 8rem;
		}
		 
		.score {
		    background: rgba(255, 255, 255, 0.2);
		    padding: 0 3rem;
		    line-height: 1;
		    border-radius: 1rem;
		}
		 
		p {
		    height: 5rem;
		    text-align: center;
		}
		 
		button {
		    margin: 0 auto;
		    width: 10rem;
		    font-size: 3rem;
		    font-family: 'Amatic SC', cursive;
		    -webkit-appearance: none;
		    appearance: none;
		    font-weight: bold;
		    background: rgba(255, 255, 255, 0.2);
		    border: 1px solid black;
		    border-radius: 1rem;
		}
		 
		.game {
		    width: 600px;
		    height: 400px;
		    display: flex;
		    flex-wrap: wrap;
		    margin: 0 auto;
		}
		 
		.hole {
		    flex: 1 0 33.33%;
		    overflow: hidden;
		    position: relative;
		}
		 
		.hole:after {
		    display: block;
		    background: url(dirt.svg) bottom center no-repeat;
		    background-size: contain;
		    content: '';
		    width: 100%;
		    height: 70px;
		    position: absolute;
		    z-index: 2;
		    bottom: -30px;
		}
		 
		.mole {
		    background: url('mole.svg') bottom center no-repeat;
		    background-size: 60%;
		    position: absolute;
		    top: 70%;
		    width: 100%;
		    height: 100%;
		    transition: all 0.4s;
		}
		 
		.hole.up .mole {
		    top: 0;
		}

js部分

	window.addEventListener('load', () => {
    // 获取元素
    let start = document.querySelector('.start')    // 开始按钮
    let mole = document.querySelectorAll('.mole')    // 打鼹鼠
    let score = document.querySelector('.score')    // 数字文本
    let timerId     // 定时句柄
    let index       // 随机数索引
    let num = 0     // 击打次数
 
 
    // 开始添加点击事件
    start.addEventListener('click', () => {
 
        // 重新点击开始,击打次数清零
        num = 0
        score.innerHTML = num
 
        start.style.display = 'none'    // 隐藏开始按钮,以防多次点击出现定时器bug
   
        timerId = setInterval(() => {       // 添加打地鼠定时器
            // 生成随机数
            index = parseInt(Math.random() * mole.length)
            mole[index].style.top = '0%'        // 设置地鼠样式从下往上出来
            time = setTimeout(() => {           // 添加定时器嵌套延时器,延时器只执行一遍
                mole[index].style.top = '70%'      // 设置地鼠样式钻回地底
            }, 500)
        }, 1000)
        
        // 添加倒计时10秒后停止打地鼠定时器
        setInterval(() => {
            start.style.display = 'block'
            clearInterval(timerId)
        }, 10000)
    })
 
    // 点击敲打鼹鼠
    mole.forEach((value, index) => {
        value.addEventListener('click', () => {
            mole[index].style.top = '70%'
            num++
            score.innerHTML = num
        })
    });

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
地鼠是一款经典的游戏,在JavaScript实现一个简单的打地鼠游戏,可以通过以下代码实现: HTML部分: ```html <!DOCTYPE html> <html> <head> <title>打地鼠游戏</title> <style> .game-board { display: grid; width: 300px; height: 300px; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 10px; } .hole { background-color: #e8e8e8; display: flex; align-items: center; justify-content: center; } .mole { background-color: #e91e63; color: #ffffff; font-size: 24px; font-weight: bold; display: flex; align-items: center; justify-content: center; } </style> </head> <body> <h1>打地鼠游戏</h1> <div class="game-board"></div> <script src="script.js"></script> </body> </html> ``` JavaScript部分: ```javascript document.addEventListener("DOMContentLoaded", function(event) { const gameBoard = document.querySelector(".game-board"); const holes = Array.from(gameBoard.querySelectorAll(".hole")); let score = 0; function randomTime(min, max) { return Math.round(Math.random() * (max - min) + min); } function randomHole(holes) { const index = Math.floor(Math.random() * holes.length); const hole = holes[index]; if (hole.classList.contains("mole")) { return randomHole(holes); } return hole; } function showMole() { const time = randomTime(200, 1000); const hole = randomHole(holes); hole.classList.add("mole"); setTimeout(function() { hole.classList.remove("mole"); if (!timeUp) { showMole(); } }, time); } function startGame() { score = 0; showMole(); setTimeout(function() { timeUp = true; }, 10000); } function bonkMole(e) { if (!e.isTrusted) return; // 防止欺骗点击 score++; this.classList.remove("mole"); } holes.forEach(function(hole) { hole.addEventListener("click", bonkMole); }); document.querySelector("#start-button").addEventListener("click", startGame); }); ``` 这段代码通过监听页面加载完成事件,在页面中创建一个网格,其中包含9个格子,通过在格子中随机显示一个地鼠,然后每点击一个地鼠,分数增加一分,游戏时间为10秒。点击开始按钮可以开始游戏。 以上就是一个简单的JavaScript实现打地鼠游戏的代码,可以通过浏览器运行来体验游戏的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值