JS实现飞机大战(1)

本文详细介绍了使用JavaScript和HTML5实现的一款简易飞机大战游戏,包括敌方飞机生成、子弹控制、碰撞检测以及提供一套针对Android开发的学习资料,旨在帮助初级工程师提升技能。
摘要由CSDN通过智能技术生成
</div>

<div id="end">又菜又爱玩</div>

<script>

    var mainEle=document.querySelector('#main')

    var startEle=document.querySelector('#start')

    var scoreEle=document.querySelector('.score')

    var endEle=document.querySelector('#end')

//敌方飞机数组

    var enemyarry=[]

//子弹数组

    var bulletarry=[]

    let score=0

    //封装随机数函数

    function getRandom(m,n){

        return Math.floor(Math.random()*(n-m)+m)

    }

    //开始

    startEle.onclick=function(){

        startEle.style.display='none'

    //创建敌方飞机

    function Enemy(imgsrc,x,y,speed){

        this.imgNode=document.createElement('img')

        this.imgsrc=imgsrc

        this.x=x

        this.y=y

        this.speed=speed

        this.init=function(){

            this.imgNode.src=this.imgsrc

            this.imgNode.style.position='absolute'

            this.imgNode.style.left=this.x+"px"

            this.imgNode.style.top=this.y+"px"

//加入敌方飞机到节点下

            mainEle.appendChild(this.imgNode)

        }

        this.init()

//移动函数

        this.move=function(){

            this.imgNode.style.top=parseInt(this.imgNode.style.top) +this.speed+'px' 

            

        }

    }

    function getEnemy(){

        let newEnemy=new Enemy('images/neemy2.png',getRandom(0,560),-getRandom(0,30),getRandom(0,10)+10)

//创建的敌方飞机加入到敌方数组

        enemyarry.push(newEnemy)

    }

    setInterval(function(){

        getEnemy()

    },1000)

    function enemyMove(){

       for(let i=0;i<enemyarry.length;i++){

//执行移动函数

           enemyarry[i].move()

//判定如果超出画面,移出敌方飞机

           if(parseInt(enemyarry[i].imgNode.style.top)>650){

            mainEle.removeChild(enemyarry[i].imgNode)

            enemyarry.splice(i,1)

            }

       }

    }

    setInterval(enemyMove,50)

    //背景图动画

    function background(){

        

        let a=0;

        setInterval(function(){

            a=a+1

//背景图移动

            mainEle.style.background=`url(images/background.webp) 0px ${a}px`

            if(a==840){

                a=0

            }

        },10)

    }background()

    //创建我方飞机

    function myplane(imgsrc){

        this.myplaneNode=document.createElement('img')

        // myplaneNode.src='images/myplane2png.png'

        this.imgsrc=imgsrc

       

        this.init=function(){

            this.myplaneNode.src=this.imgsrc

            this.myplaneNode.style.position='absolute'

            mainEle.appendChild(this.myplaneNode)

        }            

        this.init()

        this.move=function(){

            setInterval(function(){

//移动时执行X,Y赋值给我方飞机

            document.onmousemove=function(e){

            

            let l1=mainEle.offsetLeft

            let t1=mainEle.offsetTop

            

            let l=myplaneNode.clientWidth/2

            let t=myplaneNode.clientHeight/2

            let x=e.clientX

            let y=e.clientY

            x1=e.clientX-l-l1

            y1=e.clientY-t-t1

            if(x1<-l){

                x1=-l

            }

            if(x1>600-t){

                x1=600-t

            }

        

           myplaneNode.style.top=y1+'px'

           myplaneNode.style.left=x1+'px'

        getBullet(x1,y1)

    //判断游戏结束

        for(let i=0;i<enemyarry.length;i++){

//循环敌方飞机坐标

            let enTop=parseInt(enemyarry[i].imgNode.style.top)

            let enLeft=parseInt(enemyarry[i].imgNode.style.left)

            if(enLeft>=x1 && enLeft<=x1+50 &&enTop>=y1 &&enTop<=y1+60){

//弹出结束界面

                endEle.style.display='block'

//移出画面

                mainEle.remove()

            }

        }

        }

    },50)   

        }

        this.move()

    }

    myplane('images/myplane2png.png')

    function getBullet(x1,y1){

//创建子弹

            let newbullet=new bullet('images/20181227204057448.png',x1+21,y1-20,10)

//创建的子弹加入到子弹数组里

            bulletarry.push(newbullet) 

        }   

    //创建子弹

    function bullet(imgsrc,x,y,speed){

        this.imgNode=document.createElement('img')

        this.imgsrc=imgsrc

        this.x=x

        this.y=y

        this.speed=speed

        this.init=function(){

            this.imgNode.src=this.imgsrc

            this.imgNode.style.position='absolute'

            this.imgNode.style.left=this.x+"px"

            this.imgNode.style.top=this.y+"px"

//子弹加入到页面中

            mainEle.appendChild(this.imgNode)

            

        }

        this.init()

//子弹移动函数

        this.move=function(){

            this.imgNode.style.top=parseInt(this.imgNode.style.top)-this.speed+"px"

            

        }



    }

   function bulletMove(){

//遍历所有子弹判定如果超出界面,移出子弹

       for(let i=0;i<bulletarry.length;i++){

           bulletarry[i].move()

           if(parseInt(bulletarry[i].imgNode.style.top)<-650){

            mainEle.removeChild(bulletarry[i].imgNode)

            bulletarry.splice(i,1)

          

           }

       }

   }

   setInterval(bulletMove,10)

    //碰撞函数

   function crash(){

        for(let i=0;i<bulletarry.length;i++){

            for(let j=0;j<enemyarry.length;j++){

//拿到子弹和敌方飞机X和Y坐标

                let blTop=parseInt(bulletarry[i].imgNode.style.top)

                let blLeft=parseInt(bulletarry[i].imgNode.style.left)

                let enTop=parseInt(enemyarry[j].imgNode.style.top)

                let enLeft=parseInt(enemyarry[j].imgNode.style.left)

                if(blTop>=enTop && blTop<=enTop+30 && blLeft>=enLeft && blLeft<=enLeft+40){

//判定生效移出敌方飞机和子弹

                    mainEle.removeChild(bulletarry[i].imgNode)

                    bulletarry.splice(i,1)

                    mainEle.removeChild(enemyarry[j].imgNode)

                    enemyarry.splice(j,1)

//分数加一

                    score++

                }

            }

        }

        scoreEle.innerHTML=score

   }

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
…(img-pwWZXBKs-1714932134517)]

[外链图片转存中…(img-zR0RzeT1-1714932134517)]

[外链图片转存中…(img-oKDIppJI-1714932134518)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值