本文实例为大家分享了jQuery实现飞机大战游戏的具体代码,供大家参考,具体内容如下

一、效果图

二、核心代码

1.创建地图  

this.createMap = function () {
   var that = this;
                that._bg = $("<div class='bgmap'></div>");
                that._bg.css({
                    width: sw,
                    height: sh,
                    backgroundPosition: '0px '   (-sh)   'px'
                });
                mapEle.append(that._bg);
                this.startAnimate();
                //创建分数
                that.score=$("<span class='score'>0</span>");
                mapEle.append(that.score);
}; 

2.用户选择飞机界面

this.createPage=function(){
    var that=this;
                that._userPage=$("<div class='user_check'></div>");
                that._userplane[0]=$("<div class='plane1'><img src='./img/my_1.png'></div>");
                that._userplane[1]=$("<div class='plane2'><img src='./img/my_2.png'></div>");
                that._userplane.map(function (item,index){
                    !index?item.addClass("check"):'';//默认第一个选中
                    that._userPage.append(item);
                    item.on("click",function(){
                        //设置二选一
                        $(this).addClass("check").siblings().removeClass("check");
                    });
                });
                that._start = $("<button class='start'>开始</button>");
                that._start.on("click",function(){
                    that._userplane.map(function(item,index){
                        if(item.hasClass("check")){
                            that._userPage.hide();
                            //开启背景动画
                            that.startAnimate();
                            //获取用户选择的飞机的图片路径
                            that._userFeisrc=item.find("img").attr("src");
                            that._plane.createUser(that._userFeisrc);
                        }
                    })
                });
                that._close = $("<button class='start'>关闭</button>");
                that._close.on("click",function(){
                    //浏览器关闭
                    window.close();
                })
                that._userPage.append(that._start);
                that._userPage.append(that._close);
                mapEle.append(that._userPage);
            } 

3.设置背景循环

this.startAnimate=function(){
   var that=this;
                that._bg.animate({
                    backgroundPositionY:0
                },5000,'linear').queue(function(){
                    $(this).css({
                        backgroundPosition:'0px ' (-sh) 'px'
                    }).dequeue();
                    that.startAnimate();
                });
            }; 

4.创建飞机

this.createUser=function(src){
     var that=this;
                that._user=$("<img class='user' src=" src "/>");
                mapEle.append(that._user);
                that._user.css({
                    top:sh,
                    left: sw / 2 - 30
                }).animate({
                    top:that.pos.top
                },1000,function(){
                    //动画执行完成之后用户开始操作
                    console.log("开始触屏");
                    //给当前飞机添加触屏事件
                    that.addTouch();
                    //设置子弹几发,并且开始发射
                    that.gun();
                    //敌机开始动
                    that.randomEnemy();
                });
            }; 

5.创建敌机

this.createEnemy = function () {
   var that = this;
      that.index = Math.floor(Math.random() * that.enemylist.length);
      var wrandom = Math.random() * sw;
      var ele = that.enemylist[that.index];//获取当前的敌机
      that.enemy = $("<img class='enemy'/>");
      mapEle.append(that.enemy);
      that.top = ele.direct == 'todown' ? -ele.h : sh   ele.h;
      that.left = wrandom - ele.w < 0 ? 0 : wrandom   ele.w > sw ? sw - ele.w : wrandom;
      that.w = ele.w;
      that.h = ele.h;
      that.enemy.css({
                    width: that.w,
                    height: that.h,
                    left: that.left,
                    top: that.top
                }).attr("src", ele.src);
                that.blood = ele.blood;
                that.score = ele.score;
                that.way = ele.direct;
                that.speed = ele.speed;
                //敌机移动
                that.move();
            }; 

6.敌机移动

this.move=function(){
      var that=this;
                if(that.way=="todown"){
                    that.top =that.speed;
                    if(that.top>=sh){
                        that.enemy.remove();
                        return;
                    }
                }
                else{
                    that.top-=that.speed;
                    if(that.top<=0){
                        that.enemy.remove();
                        return;
                    }
                }
                that.enemy.css({
                    top: that.top
                });
                that.timer=setTimeout(function(args){
                    args.move();
                },that.tspeed,that)
            } 

7.设置敌机爆炸

this.blow = function (index) {
    var that = this;
   //开始爆炸
    that.blowool = true;
    that.enemy.remove();
   //加分
       allsc =that.score;
                $(".score").text(allsc);
                //在原位置创建爆炸
                var b = $("<img class='blow' src='./img/blow.gif'/>");
                b.css({
                    left: that.left,
                    top: that.top,
                    width: that.w,
                    height: that.h
                });
                b.timer = setTimeout(function (arg) {
                    arg.remove();
                }, 300, b)
                mapEle.append(b);
                allEnemy.splice(index, 1);
            }; 

8.创建子弹

this.createBullet=function(pos,left){
this._bullet = $("<img class='bullet' src='"   this.img   "'/>");
        mapEle.append(this._bullet);
                //设置当前子弹的坐标
                this.top = pos.top - 20;
                this.left = left - this.w / 2;
                this._bullet.css({
                    width: this.w,
                    height: this.h,
                    left: this.left,
                    top: this.top
                });
                this.move();
            } 

9.检测子弹的移动(是否打到敌机)

this.move=function(){
 var that=this;
                that.top-=that.dus;
                if(that.top<=0){
                    that._bullet.remove();
                    return;
                }
                //在子弹里面去检测  是否打到敌机
                that = that.checkEnemy();
                //检测子弹如果为null  直接出
                if (that == null)
                    return;
                that._bullet.css({
                    top: that.top
                });
                that.timer=setTimeout(function(args){
                    args.move();
                },that.speed,that);
            }; 

10.设置敌机被消灭的情况

this.checkEnemy = function () {
var that = this;
 //left  top
                for (var i = 0; i < allEnemy.length; i  ) {
                    var item = allEnemy[i];
                    //检测条件
                    if (item.blowool == false && that.left   that.w >= item.left && that.left <= item.left   item.w && that.top <= item.top  
item.h && that.top   that.h >= item.top) {
                        //开始血量减少
                        item.blood -= 1;
                        if (item.blood <= 0) {
                            //敌机移除
                            item.blow(i);
                        }
                        //子弹移除
                        that._bullet.remove();
                        //移除子弹对象
                        return null;
                    }

                }
                return that;
            } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

jQuery实现飞机大战游戏的更多相关文章

  1. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. 在IOS9中的Cordova应用程序使用JQuery / Javascript的window.history问题

    在两个测试用例中唯一改变的是Cordova.js.解决方法我看到这是几个星期前,但我会发布这个,以防其他人遇到它.听起来它可能与iOS9中的哈希更改生成的导航事件有关.如果是这样,可以将其添加到index.html以禁用哈希侦听:

  3. iOS 5上的jQuery事件

    解决方法在Apple开发论坛上由一个人回答:我需要在将元素添加到DOM之后才绑定(),如下所示:

  4. Swift coreAnimation 加计时器写的游戏《飞机大战》

    最近在学习swift的动画和图层,就用现学的东西写了个游戏,基本思想基本功能都实现了,图片都是在网上找得。希望能帮助大家更好的理解动画和图层、声明下,我是初学者,代码写的不好。大家应该都能看懂。游戏所有代码都是swift加动画加计时器,没有用任何游戏引擎框架。效果图源代码地址:点击打开链接地址失效了的话我可以补上

  5. android – Phonegap本地构建 – jquery ajax错误:readystate 0 responsetext status 0 statustext error

    解决方法您是否在索引文件中包含了内容安全元标记?

  6. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  7. 设置焦点到输入框和显示Android键盘使用jquery手机在pageshow

    我正在设置焦点到输入框,并显示Android键盘使用jquery手机网页显示.我从Web上尝试过很多选项.但是没有一个在模拟器和移动设备中都能按预期工作.这是代码:查找屏幕截图以供参考请咨询…解决方法对我有用的解决方案

  8. android – 如何在焦点()上以编程方式隐藏jquery mobile中的键盘

    我想在Focus()上隐藏键盘,但是当$(“.ui-input-text”).focus();它会自动打开键盘.我只是想隐藏在特定的屏幕上,我用document.activeElement.blur()测试;但它也没有关注()输入.解决方法提交表单时,iOS键盘可能不会自动关闭.这是一个非常实用的问题,因为不应要求用户手动关闭键盘,否则他们不会期望需要这样做.可以通过调用document.acti

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. jQuery实现简单的抽奖游戏

    这篇文章主要为大家详细介绍了jQuery实现简单的抽奖游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部