web前端 打地鼠小游戏

 <style>
    html,body{
   
        height: 100%;
    }
    body{
   
        margin: 0;
        overflow: hidden;
        background-image: url(image/bg.jpg);
        /* 用户不能选择内容 */
        user-select: none;
        /* 火狐浏览器的私有属性 */
        -moz-user-select: none;
        /* 谷歌和safaif浏览器的私有属性 */
        -webkit-user-select: none;
        /* ie 浏览器 */
        -ms-user-select: none;
        /* 设置鼠标样式 如果自定义图片必须加auto */
        cursor: url(image/cursor.png),auto;
    }
    p{
   
        background-color: red;
        position: absolute;
        top: 30px;
        border-radius: 0px 5px 5px 0px;
        font-size: 20px;
        font-weight: bold;
        font-family: 楷体;
        color: white;
        left: 30px;
        padding: 10px;
    }
    table{
   
        border: 1px solid black;
        width: 600px;
        height: 600px;
        margin: 40px auto;
        border-collapse: collapse;
    }
    td{
   
        width: 150px;
        height: 150px;
        border: 1px solid red;
        /* 合并边框 */
        background-image: url(image/hole.png);
        background-repeat: no-repeat;
        background-size: 120px 60px;
        background-position: center bottom;
        text-align: center;
        /* 数值对齐方式 */
        vertical-align: bottom;
    }
    td>img{
   
        width: 80px;
        height: 0px;
        position: relative;
        top: -10px;
    }
    /* 使用老师上去的动画效果 以后再js对一个元素对象添加这个up类,这个元素对象就是这个类的效果 */
    .up{
   
        animation: mouseUp 0.3s linear forwards;
    }
    .down{
   
        animation: mouseDown 0.3s linear forwards;
    }
    /* 地鼠上升和地鼠下去  添加两个动画效果 上去的动画效果:height 从0到70 
    下去的动画效果:height 从70到0
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的web前端地鼠小游戏实现,使用HTML、CSS和JavaScript编写。 HTML部分: ```html <!DOCTYPE html> <html> <head> <title>打地鼠小游戏</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>打地鼠小游戏</h1> <div class="game-container"> <div class="hole hole1"></div> <div class="hole hole2"></div> <div class="hole hole3"></div> <div class="hole hole4"></div> <div class="hole hole5"></div> <div class="hole hole6"></div> <div class="score">得分:<span id="score">0</span></div> </div> <script src="script.js"></script> </body> </html> ``` CSS部分: ```css .game-container { margin: 0 auto; width: 500px; height: 400px; position: relative; } .hole { width: 100px; height: 100px; background-image: url("hole.png"); background-size: 100% 100%; position: absolute; top: 50px; left: 50px; cursor: pointer; display: none; } .hole1 { top: 50px; left: 50px; } .hole2 { top: 50px; left: 175px; } .hole3 { top: 50px; left: 300px; } .hole4 { top: 175px; left: 50px; } .hole5 { top: 175px; left: 175px; } .hole6 { top: 175px; left: 300px; } .score { font-size: 24px; font-weight: bold; text-align: center; margin-top: 20px; } .mole { width: 100px; height: 100px; background-image: url("mole.png"); background-size: 100% 100%; position: absolute; top: 50px; left: 50px; display: none; cursor: pointer; animation: mole-up 0.5s ease-in-out; } @keyframes mole-up { 0% { transform: translateY(0); } 50% { transform: translateY(-50px); } 100% { transform: translateY(0); } } ``` JavaScript部分: ```javascript var score = 0; var timeLeft = 30; var timerId = null; var moleTimerId = null; function startGame() { score = 0; timeLeft = 30; document.getElementById("score").innerHTML = score; document.getElementById("time-left").innerHTML = timeLeft; document.getElementById("start-button").disabled = true; timerId = setInterval(timerTick, 1000); moleTimerId = setInterval(showMole, 1000); } function timerTick() { timeLeft--; document.getElementById("time-left").innerHTML = timeLeft; if (timeLeft === 0) { clearInterval(timerId); clearInterval(moleTimerId); alert("游戏结束,你的得分是:" + score); document.getElementById("start-button").disabled = false; } } function showMole() { var holes = document.getElementsByClassName("hole"); var randomIndex = Math.floor(Math.random() * holes.length); var hole = holes[randomIndex]; var mole = hole.nextElementSibling; hole.style.display = "block"; mole.style.display = "block"; setTimeout(hideMole, 1000, hole, mole); } function hideMole(hole, mole) { hole.style.display = "none"; mole.style.display = "none"; } function hitMole(event) { if (event.target.classList.contains("mole")) { score++; document.getElementById("score").innerHTML = score; event.target.style.display = "none"; } } var holes = document.getElementsByClassName("hole"); for (var i = 0; i < holes.length; i++) { holes[i].addEventListener("click", hitMole); } ``` 这个小游戏实现方式是:在页面上创建6个“洞”,然后每秒随机在其中一个洞里“钻出”一个地鼠,玩家需要在1秒内点击地鼠,击中后得分加1。游戏时长为30秒,时间到后弹出得分提示框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值