JavaScript实战——打气球游戏

源码:

<!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>气球游戏</title>
</head>
<body>
    <div id="app"></div>

    <script>
    //获取变量
    var app = document.getElementById('app');
    //构造函数创造类
    function Balloon(id, dom, top) {
        this.id = id;
        this.dom = dom;
        this.x = 125;
        this.y = parseInt(500 / 3);
        this.ball = document.createElement('div');
        this.dom.appendChild(this.ball);
        this.left = -125;
        this.top = top;
        this.remove = true; //可以移动
        this.text = 'abcdefghijklmnopqrstuvwxyz';
        this.letter = this.text[parseInt(Math.random() * 26)].toUpperCase();
        var me = this;
        //气球移动
        this.move = function() {
            this.left++;
            if (this.left > document.documentElement.clientWidth) {
                this.remove = false;
            }
            this.ball.style.left = this.left + 'px';
        }
        //移除气球
        this.die = function() {
            this.dom.removeChild(this.ball);
        }
        //绘制
        this.init = function() {
            this.ball.style.width = '125px';
            this.ball.style.height = '166px';
            this.ball.style.top = this.top + 'px';
            this.ball.style.lineHeight = '130px';
            this.ball.style.fontSize = '60px';
            this.ball.style.fontWeight = 'blod';
            this.ball.style.textAlign = 'center';
            this.ball.style.position = 'absolute';
            this.ball.style.backgroundImage = 'url(./image/balloon.jpg)';
            this.ball.style.backgroundPositionX = -this.x * (this.id % 4 - 1) + 'px';
            this.ball.style.backgroundPositionY = -this.y * parseInt(this.id / 4) + 'px';
            this.ball.innerHTML = this.letter;
        }
    } 
    //创建数组容器存储气球
    var arr = [];
    var time = 0;
    var score = 0;
    var name = prompt('亲爱的玩家,输入您的名字即可开启游戏!');
    var a = setInterval(function() {
        if (time % 200 === 0) {
            var demo = new Balloon(Math.ceil(Math.random()*12), app, Math.random() * 250 + 50);
            demo.move();
            demo.init();
            arr.push(demo);
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            if (arr[i].remove) {
                arr[i].move();
            } else {
                arr[i].die();
                arr.splice(i, 1);
                console.log(arr);
            }
        }
        time++;
        if (time > 10000) {
            clearInterval(a);
            confirm('游戏结束,亲爱的' + name + '您的分数为' + score + '分');
        }
        console.log(time);
    }, 1);
    //键盘判断
    document.onkeydown = function(e) {
        for (var i = arr.length - 1; i >= 0; i--) {
            if (arr[i].letter === e.key.toUpperCase()) {
                arr[i].remove = false;
                score++;
                return;
            }
        }
    }
    </script>
</body>
</html>

balloon.jpg(附上demo中使用到的一张图片,复制代码就可以运行啦)

 效果展示:

https://www.bilibili.com/video/BV1EY41177dA?t=3.1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

邻家的肥猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值