js 飞机大战

说明:  本案例所需要的文件以及源代码我已上传?文件

网盘链接:https://pan.baidu.com/s/1hs7sBUs 密码: d83x

飞机大战css定义:

<style>
#container{  
width:320px;
height:568px;
background: url(images/start.png);
margin:20px auto;
position: relative;
overflow: hidden;  
}
#container input{
width:120px;
height: 38px;
background: #ff6600;
border:0;
color:#fff;
font-size:14px;
font-family: 微软雅黑;
position: absolute;
left: 100px;
bottom:50px;
}
#enddiv{
position: absolute;
top: 210px;
left: 75px;
border: 1px solid gray;
border-radius: 5px;
text-align: center;
background-color:#d7ddde;
display: none;
z-index: 2;
}
.plantext{
width: 160px;
height: 30px;
line-height: 30px;
font-size: 16px;
font-weight: bold;
}
#enddiv div{
width: 160px;
height: 50px;
}
#enddiv div button{
margin-top:10px ;
width: 90px;
height: 30px;
border: 1px solid gray;
border-radius: 30px;
}
#scoretext{
margin: 0;
font-family: arial;
font-size:12px;
font-weight: bold;
color:#ff6600;
position: absolute;
left: 4px;
top: 4px;
z-index: 100;
}
</style>

飞机大战: HTML代码如下:

  <div id="container">
        <p id="scoretext"></p>
        <div id="enddiv">
            <p class="plantext">游戏结束</p>
            <div><button onclick="restartGame()" id="restart">继续</button></div>
        </div>
        <input type="button" value="开始游戏" id="startBtn">
    </div>

飞机大战 : 调用js

//中大型飞机射击次数未实现,gameover未实现
        function startGame(container){

            var startBtn = document.getElementById("startBtn");
            startBtn.onclick = function(){
                container.style.background = "url(images/background_1.png)";
                this.style.display = "none";
                bgMove(container);
                var score = 0;
                var myplan = new MyPlan(120,480,container);
                var middleEnemy = new MiddleEnemy(container,myplan.bullets,myplan,score); //中型飞机对象实例
                var maxEnemy = new MaxEnemy(container,myplan.bullets,myplan,score);//大型飞机对象实例
                var enemy = new Enemy(container,myplan.bullets,myplan,middleEnemy,maxEnemy,score);
                enemy.init();
            }
        }
        var speed = 0;
        function bgMove(container){
            setInterval(function(){
                speed++;
                container.style.backgroundPosition = "0 " + speed + "px";
                if(speed > 568){
                    speed = 0;
                }
            },15);

        }
        function gameOver(){
            var enddiv = document.getElementById("enddiv");
            var restart = document.getElementById("restart");

            enddiv.style.display = "block";
            restart.onclick = function(){
                location.reload();
            }

        }
        var score = 0;
        function getScore(num){
            var scoretext = document.getElementById("scoretext");
            score += num;
            scoretext.innerHTML = score + "分";

        }
        onload = function(){
            var container = document.getElementById("container");

            startGame(container);

        }

飞机大战: 我放飞机创建:

  

Array.prototype.remove = function(value){
    for(var i = 0; i < this.length; i++){
        if(this[i] == value){
            this.splice(i,1);
        }
    }
}
function MyPlan(x , y , container){
    this.x = x;
    this.y = y;
    this.img = "images/my.gif";
    this.container = container;
    this.bullets = [];
    this.createTimer;
    this.init();
}
MyPlan.prototype = {
    init:function(){
        this.create();
        this.planMove();
        this.bullets.push(this.plan);
        var that = this;
        this.createTimer = setInterval(function(){
            that.createBullets();
        },200);
        this.createBullets();
    },
    planMove:function(){
        var that = this;
        this.container.onmousemove = function(e){
            e = e || event;
            var maxLeft = that.container.offsetWidth - that.plan.offsetWidth;
            var maxTop = that.container.offsetHeight - that.plan.offsetHeight;
            var planX = Math.max(Math.min(e.clientX - that.container.offsetLeft - that.plan.offsetWidth / 2,maxLeft),0);
            var planY = Math.max(Math.min(e.clientY - that.container.offsetTop - that.plan.offsetHeight / 2,maxTop),0);
            that.plan.style.left = planX + "px";
            that.plan.style.top = planY + "px";
        }
    },
    create:function(){
        this.plan = document.createElement("img");
        this.plan.src = this.img;
        this.plan.style.cssText = "position:absolute; top:" + this.y + "px; left:" + this.x + "px;";
        this.container.appendChild(this.plan);
    },
    createBullets:function(){
        this.bull = document.createElement("img");
        this.bull.src = "images/bullet1.png";
        var bullX = this.plan.offsetLeft + this.plan.offsetWidth / 2 - 6 / 2;
        var bullY = this.plan.offsetTop - 14;

        this.bull.style.cssText = "position:absolute; top:" + bullY + "px; left:" + bullX + "px;";
        this.container.appendChild(this.bull);
        this.bullets.push(this.bull);
        var bull = this.bull; //不能用that = this 对象冒充 闭包问题
        var container = this.container;
        var bullets = this.bullets;
        this.bull.timer = setInterval(function(){
            bull.style.top = bull.offsetTop - 3 + "px";
            if(bull.offsetTop <= -14){
                bullets.remove(bull);
                container.removeChild(bull);
                clearInterval(bull.timer);
            }
        },8);
    }
}

飞机大战: 敌方基本飞机创建:

function Enemy(container,bullets,myplan,middleEnemy,maxEnemy,score){
    this.container = container;
    this.img = "images/enemy1_fly_1.png";
    this.createTime = 600; //创建敌机的间隔时间
    this.bullets = bullets;
    this.dieImg = "images/enemy1_fly_3.gif";
    this.width = 34; //敌机的宽度
    this.height = 24;    //敌机的高度
    this.myplan = myplan;
    this.count = 1; //小型敌机创建个数
    this.dieCount = 1; //敌机消灭需子弹打击次数
    this.middleEnemy = middleEnemy;
    this.maxEnemy = maxEnemy;
    this.score = score;

}
Enemy.prototype = {
    init:function(){
        var that = this;
        var middleEnemy = this.middleEnemy;
        var maxEnemy = this.maxEnemy;
        var count = 0;
        setInterval(function(){
            that.create();
            count++;
            if(count % 5 == 0){
                middleEnemy.create();
            }
            if(count % 30 == 0){
                maxEnemy.create();
                count = 1;
            }
        },this.createTime);

    },
    create:function(){
        this.enemy = document.createElement("img");
        this.enemy.src = this.img;
        var enemyX = Math.random() * (this.container.offsetWidth - this.width);
        var enemyY = -1 * this.height;
        this.enemy.style.cssText = "position:absolute; left:" + enemyX + "px; top:" + enemyY + "px;";
        this.container.appendChild(this.enemy);
        var enemy = this.enemy;
        this.data_hitCount = 0;
        var container = this.container;
        var bullets = this.bullets;
        var dieImg = this.dieImg;
        var myplan = this.myplan;
        var plan = this.myplan.plan;
        var createBullets = this.myplan.createBullets;
        var dieCount = this.dieCount;
        var isremove = true; //是否可以移除敌机
        var score = this.score;
        var that = this;
        this.enemy.timer = setInterval(function(){
            enemy.style.top = enemy.offsetTop + 3 + "px";

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

                if(bullets[i].offsetLeft + bullets[i].offsetWidth > enemy.offsetLeft && bullets[i].offsetLeft < enemy.offsetLeft + enemy.offsetWidth){
                    if(bullets[i].offsetTop + bullets[i].offsetHeight > enemy.offsetTop && bullets[i].offsetTop < enemy.offsetTop + enemy.offsetHeight && isremove){
                        if(i == 0){
                            plan.src = "images/myover.gif";
                            container.onmousemove = null;
                            clearInterval(myplan.createTimer);
                            gameOver();
                        }
                        else{
                            container.removeChild(bullets[i]);
                            bullets.remove(bullets[i]);
                            that.data_hitCount++;
                            if(that.data_hitCount == dieCount){
                                isremove = false;
                                enemy.src = dieImg;
                                getScore(dieCount);
                                setTimeout(function(){
                                    container.removeChild(enemy);
                                },300);
                            }
                        }
                    }
                }
            }
            if(enemy.offsetTop >= container.offsetHeight){
                container.removeChild(enemy);
                clearInterval(enemy.timer);
            }
        },30);
    }
}

飞机大战: 其他敌机创建:

function MiddleEnemy(container,bullets,myplan,score){

    Enemy.call(this,container,bullets,myplan,score);
    this.img = "images/enemy2_fly_1.png";
    this.dieImg = "images/enemy2_fly_3.gif";
    this.width = 46;  //敌机的宽度
    this.height = 60; //敌机的高度
    this.dieCount = 5;
}

MiddleEnemy.prototype = Enemy.prototype;

function MaxEnemy(container,bullets,myplan,score){

    Enemy.call(this,container,bullets,myplan,score);
    this.img = "images/enemy3_fly_1.png";
    this.dieImg = "images/enemy3_fly_3.gif";
    this.width = 110; //敌机的宽度
    this.height = 164; //敌机的高度
    this.dieCount = 10;
}
MaxEnemy.prototype = Enemy.prototype;

效果图如图所示:

  

时间: 2024-07-30 03:17:58

js 飞机大战的相关文章

js实例--飞机大战

<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>飞机大战</title> <style> #did{ width:500px;height:500px; background:url("./images/bg2.png") no-repeat 0px -1036px; position:relative;ove

[知了堂学习笔记]_纯JS制作《飞机大战》游戏_第1讲(实现思路与游戏界面的实现)

整体效果展示: 一.实现思路 如图,这是我完成该项目的一个逻辑图,也是一个功能模块完成的顺序图. 游戏界面的完成 英雄飞机对象实现,在实现发射子弹方法过程中,又引出了子弹对象并实现.在此时,英雄飞机能进行基本操作了. 敌机对象的实现,并且初步完成了boos出现(30s自动出现).然后又引出了许多方法的处理,如英雄子弹击中敌机和boos,英雄与敌机相撞等等.并一一解决. 随后又设置了一些游戏的参数,如血量,关卡数,等级,积分,必杀,道具对象等等. 最后又完成了一些辅助功能,暂停游戏,继续游戏,退出

web版canvas做飞机大战游戏 总结

唠唠:两天的时间跟着做了个飞机大战的游戏,感觉做游戏挺好的.说是用html5做,发现全都是js.说js里一切皆为对象,写的最多的还是函数,都是函数调用.对这两天的代码做个总结,希望路过的大神指点一下,我对这个游戏的思路,可改进优化的代码. 先说一下游戏的基本内容: 打飞机(不要想歪了),有鼠标控制移动英雄机,子弹自动射击:敌机从上而下,有三种敌机: 先说下HTML代码(主要就是这一行): <canvas id="canFly" width="480" heig

无聊吗?写个【飞机大战】来玩吧(上篇)

01前言介绍 微信小游戏是基于微信客户端的游戏,它即点即玩,无需下载安装,体验轻便,可以和微信内的好友一起玩,比如PK.围观等,享受小游戏带来的乐趣.那如何开发一款属于自己的小游戏呢? 源码地址: https://github.com/A123asdo11/aircraft_war (新版ccc已无法正常使用,需要修复,文章作者花费了大量的时间和精力,在ccc2.0以上版本进行了修复,并在微信小游戏正常运行) 02微信小游戏飞机大战简介 1.大事记 经典飞机大战是腾讯交流软件微信5.0版本在20

一文教你实现「飞机大战」里战机的控制逻辑

? 纵版射击游戏是一种比较经典的游戏类型,从早期的红白机平台到如今的手机平台,一直都有非常经典的游戏作品.纵版射击游戏只需要控制飞行器躲避敌机和子弹并攻击敌机,玩法和操作都非常简单,因此很适合移动平台上的操作.曾经微信平台红极一时的「飞机大战」相信每个人都玩过,那么今天就来教大家如何实现游戏里战机的控制逻辑. 1.首先创建一个游戏场景 GameScene,在场景中添加游戏背景和今天的主角——战斗机: 2.接下来创建战斗机的控制脚本 GamePlane.js: 3.创建成功后就可以进行编辑了,战斗

基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)

在飞机大战游戏开发中遇到的问题和解决方法: 1.在添加菜单时,我要添加一个有背景的菜单,需要在菜单pMenu中添加一个图片精灵,结果编译过了但是运行出错,如下图: 查了很多资料,调试了很长时间,整个人都要崩溃了. 最后发现引擎中CCMenu::itemForTouch函数中有遍历子节点的行为,但是循环中没有判断子节点类型是否为CCMenuItem.如图:码,这样一来,加入到pMenu中的图片精灵被当作菜单项取了出来使用,导致报错.老版本的果然又不完善的地方,整个人都不好了...果断修改引擎里的源

安卓飞机大战(二) SurfaceView实现自制背景

用SurfaceView写一个自制的背景图,并且可以移动,加上安卓飞机大战(一)中的BackgroundManager类,可以直接使用 GameView代码: public class GameView extends SurfaceView implements SurfaceHolder.Callback,Runnable{    private SurfaceHolder hd=null;    private Canvas canvas=null;    private Backgrou

android:“新版飞机大战”源代码开源啦!

今天10.24,为了纪念程序员的节日,把之前写过的一个"飞机大战"的一个源代码开源了. 源代码地址:https://github.com/nuptboyzhb/newplanegame 豌豆荚Demo:http://www.wandoujia.com/apps/edu.njupt.zhb.planegame 截图: ------------------------------------------------------------------- 更多交流,Android开发联盟QQ

android:如何用一天时间,写出“飞机大战”这样的游戏!(无框架-SurfaceView绘制)

序言作为一个android开发者,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的.体验地址:http://www.wandoujia.com/apps/edu.njupt.zhb.planegame游戏分析玩过"飞机大战"游戏的都知道,飞机大战中的主要"角色"有:1.玩家飞机2.敌方飞机3.玩家飞机发送的子弹4.敌方Boss飞机发送的子弹我们需要控制的有:1.绘制屏幕内的角色2.控制角色的逻辑,比如:敌方