jQuery实现打飞机游戏

玩法介绍:不同样式的飞机出来其它飞机会暂停飞行且处于无敌状态,子弹对它无效,你操纵的飞机不能碰到任何飞机,发出的子弹可以攻击正在飞行的飞机,每击落一架飞机会记录分数,你操纵的飞机碰到其它飞机即为游戏结束

效果图演示(gif图看着可能会有些卡顿,可以复制下面代码在浏览器运行查看):

完整代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }

            html,
            body {
                width: 100%;
                height: 100%;
                overflow: hidden;
                background: url(./img/bj.jpg) repeat 0 0px;
            }

            .mine {
                width: 110px;
                height: 90px;
                background: url(img/P.png) no-repeat 0 -102px;
                position: absolute;
                cursor: pointer;
            }

            .zi {
                width: 10px;
                height: 10px;
                background-color: red;
                position: absolute;
                border-radius: 50%;
            }

            .lan {
                width: 118px;
                height: 100px;
                position: absolute;
                background: url(img/P.png) no-repeat -115px -1px;
            }
            .hong{
                width: 107px;
                height: 80px;
                position: absolute;
                background: url(img/P.png) no-repeat -1px -1px;
            }
            .cheng{
                width: 122px;
                height: 87px;
                position: absolute;
                background: url(img/P.png) no-repeat -242px -1px;
            }
            .chen{
                width: 122px;
                height: 80px;
                position: absolute;
                background: url(img/P.png) no-repeat -387px -1px;
            }
            p{
                position: absolute;
                color: orange;
                font-size: 30px;
                z-index: 999;
                left:20;
                top:40;
            }
        </style>
    </head>
    <body>
        <ul></ul>
        <p>击落飞机:0</p>
    </body>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        let a = null, //创建mine定时器
            b = null, //创建琪它飞机定时器
            qi = null, //其它飞机移动定时器
            tim = null, //子弹移动定时器
            n = 'lan', //存取随机飞机名称
            op = 0 //记录分数
        const feiji = ['lan','hong','cheng','chen']
        $('<div>').addClass('mine').css({
                left: ($(document).innerWidth() - 110) / 2,
                top: ($(document).innerHeight() - 90)
            }).appendTo('ul') //添加mine飞机
            .on('mousedown', function(e) { //实现拖拽
                $(document).on('mousemove', e => { //鼠标移动
                    n = feiji[Math.floor(Math.random()*feiji.length)]
                    let left = e.clientX - $(this).width() / 2 + 2
                    let top = e.clientY - $(this).height() / 2 - 10
                    left = loob(left, 0, $(document).innerWidth() - $(this).width())
                    top = loob(top, 0, $(document).innerHeight() - $(this).height())
                    $(this).css({
                        left,
                        top
                    })
                    e.preventDefault() //清楚默认事件
                })
                $(document).on('mouseup', () => { //鼠标抬起
                    $(document).off('mousemove')
                })
            })

        function loob(dir, min, max) { //限制出界
            dir < min ? dir = min : dir > max ? dir = max : ''
            return dir
        }
        const $mine = $('.mine')
        
        let num = 0
        let kkk = 0
        a = setInterval(() => { //创建子弹
            clearInterval(tim)
            $('<div>').addClass('zi').css({
                left: $mine.offset().left + $mine.width() / 2 - 5,
                top: $mine.offset().top - 5
            }).appendTo('ul')
            tim = setInterval(() => { //子弹移动
                num += 10 //背景移动速度
                $('.zi').css({
                    top: '-=10'
                }).each(function() {
                    let k = $(this)
                    $(`.${n}`).each(function(){
                        if(k.offset().left+10>$(this).offset().left&&k.offset().left<$(this).offset().left+10+$(this).width()&&k.offset().top+10>$(this).offset().top&&k.offset().top<$(this).offset().top+$(this).height()){//判断子弹是否击中其它飞机
                            kkk++
                            if(kkk==10){
                                $(this).remove()
                                kkk=0
                                op++
                                $('p').html(`击落飞机:${op}`)
                            }
                            k.remove()
                        }
                    })
                    if ($(this).offset().top <= -10) { //超出屏幕删除子弹
                        $(this).remove()
                    }
                })
                $('body').css({
                    background: `url(./img/bj.jpg) repeat 0 ${num}px`
                })
            }, 1000 / 60)
        }, 100)
        b = setInterval(() => { //随机创建飞机
            clearInterval(qi)
        xuan(n)//随机出现飞机
        }, 600)
        function xuan(obj){
            $('<div>').addClass(obj).css({
                left: Math.floor(Math.random() * ($(document).innerWidth() - 300))
            }).appendTo('ul')
            qi = setInterval(() => {//其它飞机移动
                $(`.${n}`).each(function(){
                    if($('.mine').offset().left+$('.mine').width()>$(this).offset().left&&$('.mine').offset().left<$(this).offset().left+$(this).width()&&$('.mine').offset().top+$('.mine').height()>$(this).offset().top&&$('.mine').offset().top<$(this).offset().top+$(this).height()){//判断mine飞机是否触碰其它飞机
                        clearInterval(a)
                        clearInterval(b)
                        clearInterval(tim)
                        clearInterval(qi)
                        $('body').css({
                            background:'white'
                        }).html(`游戏结束!你一共击落了${op}架飞机`)
                        return
                    }
                })
                $(`.${obj}`).css({
                    top: '+=4'
                }).each(function() {
                    $(this)[0].num = 0
                    if($(this).offset().top>$(window).innerHeight()){
                        $(this).remove()
                    }
                })
            }, 1000 / 60)
        }
    </script>
</html>

如果要复制代码运行的话,可以私聊我,我把用到的图片分享给你们

相关推荐:

Python 反向猜数字游戏:修复重复打印问题

本文教你修正反向猜数字游戏中的重复输出缺陷,通过调整print(c)的执行时机,确保每次猜测反馈只显示一次当前猜测值。 本文教你修正反向猜数字游戏中的重复输出缺陷,通过调整`print(c)`的执行时机,确保每次猜测反馈只显示一次当前猜测值。 在反向猜数字游戏中,程序需根据用户输入(b表示“太大”,...

如何正确重置Python井字棋(Tic Tac Toe)游戏的全局棋盘

本文详解python中因变量作用域导致的棋盘重置失效问题,通过`global`声明或返回新对象两种方式,彻底解决全局`board`列表无法被函数修改的核心难点。 在开发井字棋(TicTacToe)这类状态驱动的游戏时,一个常见却易被新手忽略的问题是:明明在resetGame()函数中重新赋值了Boa...

jquery怎么给类添加事件

这篇文章主要介绍了jquery怎么给类添加事件的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇jquery怎么给类添加事件文章都会有所收获,下面我们一起来看看吧。   jquery可以通过选择器选择一类的标签集合。然后通过$(this)指向当前对象。   $("...

20个Jquery实用工具

1.xLazyLoader2.SpeedingupGoogleAnalyticsloadtimeswithjQuery3.AjaxFancyCaptcha–jQueryPlugin4.PriceFormat–jQueryPlugin5.jTruncate–TextTruncationforjQuer...

jquery查找元素是否存在的方法

小编给大家分享一下jquery查找元素是否存在的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!jquery查找元素是否存在的方法:首先创建一个前端示例文件;然后通过“if($("#someID")....

jquery如何查找指定html元素是否存在

本篇内容介绍了“jquery如何查找指定html元素是否存在”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! 在jquery中,可以利用“ifelse”语句配合length属性来查找指定html元素是否...