用Javascript模拟微信飞机大战游戏

时间:2023-03-08 15:38:28

最近微信的飞机大战非常流行,下载量非常高。

利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化]

[此游戏中没有使用HTML5 任何浏览器都可以运行]。

效果图:

用Javascript模拟微信飞机大战游戏

原理:利用javascript setInterval函数不停的进行元素位置的切换和添加飞机子弹,在飞机和子弹的运动中进行位置

检测,进行子弹和飞机的消失。

1、添加飞机

setInterval(function () {
var flyDiv = $('<div class="flyDiv"></div>');
flyDiv.css({left: Math.random() * 578, top: -22});
flyDiv.appendTo(main);
}, 3000);

2、添加子弹

    setInterval(function () {
var flyZ = $('<span class="z"></span>');
var l = m.offset().left + 23, t = m.offset().top;
flyZ.css({left: l, top: t});
flyZ.appendTo(main);
}, 600);

3、飞机的运动

setInterval(function () {
var allDiv = main.find('div'), allZ = main.find('span');
for (var i = 0; i < allDiv.length; i++) {
var f = $(allDiv[i]);
var nowTop = f.offset().top;
nowTop += 10;
f.css({top: nowTop});
for (var j = 0; j < allZ.length; j++) {
var z = $(allZ[j]);
if (testColl(
z.offset().left, z.offset().top, z.width(), z.height(),
f.offset().left, f.offset().top, f.width(), f.height()
)) {
z.remove();
f.remove();
sum.text(parseInt(sum.text()) + 1);
}
}
if (nowTop + 22 >= 400) {
f.remove();
}
}
}, 60);
4、子弹的运动
    setInterval(function () {
var allZ = main.find('span'),
allDiv = main.find('div');
for (var i = 0; i < allZ.length; i++) {
var z = $(allZ[i]);
var nowTop = z.offset().top;
nowTop -= 10;
z.css({top: nowTop}); for (var j = 0; j < allDiv.length; j++) {
var f = $(allDiv[j]);
if (testColl(
z.offset().left, z.offset().top, z.width(), z.height(),
f.offset().left, f.offset().top, f.width(), f.height()
)) {
z.remove();
f.remove();
sum.text(parseInt(sum.text()) + 1);
}
}
if (nowTop + 22 <= 0) {
z.remove();
}
} }, 60);

5、碰撞检测

function testColl(x1, y1, w1, h1, x2, y2, w2, h2) {
var l1 = x1;
var r1 = x1 + w1;
var t1 = y1;
var b1 = y1 + h1; var l2 = x2;
var r2 = x2 + w2;
var t2 = y2;
var b2 = y2 + h2; if (r1 < l2 || l1 > r2 || b1 < t2 || t1 > b2) {
return false;
}
else {
return true;
}
}

【完整源码】

<!DOCTYPE html>

<html lang="zh">

<head>

    <meta charset="utf-8"/>

    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>

    <title> new document </title>

    <style type="text/css">

        * {

            margin: 0;

            padding: 0;

        }

 

        #main {

            width: 600px;

            height: 400px;

            border: solid 1px #000;

            background: #cccccc;

        }

 

        .flyDiv {

            width: 20px;

            height: 20px;

            background: red;

            position: absolute;

        }

 

        .z {

            display: block;

            width: 4px;

            background: #000000;

            height: 10px;

            position: absolute;

            top: 390px;

        }

 

        .m {

            width: 50px;

            height: 50px;

            background: #000000;

            position: absolute;

            top: 345px;

            left: 275px;

        }

    </style>

</head>

 

<body>

<!-- html :begin -->

<div id="main"></div>

<div class="m" id="m"></div>

<div>

    打击飞机数:<label id="sum">0</label>

</div>

<!-- html :end -->

</body>

<script src="js/jquery-1.10.2.min.js"></script>

<script>

    $(document).ready(function () {

        var main = $('#main'),

                m = $('#m'),

                sum = $('#sum');

        $(document).on('keydown', function (ev) {

            var oEvent = ev || window.event;

            var l = m.offset().left,

                    t = m.offset().top;

            switch (oEvent.keyCode) {

                case 37:

                    if (l >= 0) {

                        l -= 12;

                    }

                    break;

                /*    case 38:

                 if (t >= 0) {

                 t -= 2;

                 }

                 break;*/

                case 39:

                    if (!(l + 50 > 600)) {

                        l += 12;

                    }

                    break;

                case 40:

                    if (!(t + 50 > 400)) {

                        t += 12;

                    }

                    break;

            }

            m.css({left: l, top: t});

        });

        setInterval(function () {

            var flyDiv = $('<div class="flyDiv"></div>');

            flyDiv.css({left: Math.random() * 578, top: -22});

            flyDiv.appendTo(main);

        }, 3000);

 

        setInterval(function () {

            var allDiv = main.find('div'), allZ = main.find('span');

            for (var i = 0; i < allDiv.length; i++) {

                var f = $(allDiv[i]);

                var nowTop = f.offset().top;

                nowTop += 10;

                f.css({top: nowTop});

                for (var j = 0; j < allZ.length; j++) {

                    var z = $(allZ[j]);

                    if (testColl(

                            z.offset().left, z.offset().top, z.width(), z.height(),

                            f.offset().left, f.offset().top, f.width(), f.height()

                    )) {

                        z.remove();

                        f.remove();

                        sum.text(parseInt(sum.text()) + 1);

                    }

                }

                if (nowTop + 22 >= 400) {

                    f.remove();

                }

            }

        }, 60);

 

        setInterval(function () {

            var flyZ = $('<span class="z"></span>');

            var l = m.offset().left + 23, t = m.offset().top;

            flyZ.css({left: l, top: t});

            flyZ.appendTo(main);

        }, 600);

 

        setInterval(function () {

            var allZ = main.find('span'),

                    allDiv = main.find('div');

            for (var i = 0; i < allZ.length; i++) {

                var z = $(allZ[i]);

                var nowTop = z.offset().top;

                nowTop -= 10;

                z.css({top: nowTop});

 

                for (var j = 0; j < allDiv.length; j++) {

                    var f = $(allDiv[j]);

                    if (testColl(

                            z.offset().left, z.offset().top, z.width(), z.height(),

                            f.offset().left, f.offset().top, f.width(), f.height()

                    )) {

                        z.remove();

                        f.remove();

                        sum.text(parseInt(sum.text()) + 1);

                    }

                }

                if (nowTop + 22 <= 0) {

                    z.remove();

                }

            }

 

        }, 60);

 

        function testColl(x1, y1, w1, h1, x2, y2, w2, h2) {

            var l1 = x1;

            var r1 = x1 + w1;

            var t1 = y1;

            var b1 = y1 + h1;

 

            var l2 = x2;

            var r2 = x2 + w2;

            var t2 = y2;

            var b2 = y2 + h2;

 

            if (r1 < l2 || l1 > r2 || b1 < t2 || t1 > b2) {

                return false;

            }

            else {

                return true;

            }

        }

    });

</script>

 

</html>