【HTML+CSS+JavaScript】实现简单网页版的飞机大战
创始人
2024-05-01 08:09:33
0

文章目录

  • 【HTML+CSS+JavaScript】实现简单网页版的飞机大战
    • 一. HTML部分代码
    • 二. CSS部分代码
    • 三. JavaScript部分代码
    • 四. 完整的代码和图片获取

【HTML+CSS+JavaScript】实现简单网页版的飞机大战

本文分享的是键盘版飞机大战的代码,且文章末尾有惊喜

效果图

请添加图片描述

一. HTML部分代码

分数:0

二. CSS部分代码


三. JavaScript部分代码

;
(function() {var gamebox = document.querySelector('.gamebox')var oEm = document.querySelector('em')var zscore = 0//1.让背景运动起来var bgposition = 0var bgtimer = setInterval(function() {bgposition += 2gamebox.style.backgroundPosition = '0 ' + bgposition + 'px'}, 30)//2.我方飞机的构造函数function Myplane(w, h, x, y, imgurl, boomurl) {//w,h宽高 x,y位置  imgurl和boomurl我方飞机的图片路径this.w = wthis.h = hthis.x = xthis.y = ythis.imgurl = imgurlthis.boomurl = boomurlthis.createmyplane()}//2.1创建我方飞机Myplane.prototype.createmyplane = function() {this.myplaneimg = document.createElement('img')this.myplaneimg.src = this.imgurlthis.myplaneimg.style.cssText = `width:${this.w}px;height:${this.h}px;position:absolute;left:${this.x}px;top:${this.y}px;`gamebox.appendChild(this.myplaneimg)//飞机创建完成,执行运动和发射子弹this.myplanemove()this.myplaneshoot()}//2.2键盘控制我方飞机移动Myplane.prototype.myplanemove = function() {var that = this//方向定时器var uptimer = null,downtimer = null,lefttimer = null,righttimer = nullvar uplock = true,downlock = true,leftlock = true,rightlock = truedocument.addEventListener('keydown', movekey, false) //movekey:事件处理函数function movekey(ev) {//W:87 A:65 S:83 D:68  K:75var ev = ev || window.eventswitch (ev.keyCode) {case 87:moveup() // 上breakcase 83:movedown() // 下breakcase 65:moveleft() // 左breakcase 68:moveright() // 右break}// 飞机向上移动function moveup() {if (uplock) {uplock = falseclearInterval(downtimer)uptimer = setInterval(function() {that.y -= 4if (that.y <= 0) {that.y = 0}that.myplaneimg.style.top = that.y + 'px'}, 30)}}// 飞机向下移动function movedown() {if (downlock) {downlock = falseclearInterval(uptimer)downtimer = setInterval(function() {that.y += 4if (that.y >= gamebox.offsetHeight - that.h) {that.y = gamebox.offsetHeight - that.h}that.myplaneimg.style.top = that.y + 'px'}, 30)}}// 飞机向左移动function moveleft() {if (leftlock) {leftlock = falseclearInterval(righttimer)lefttimer = setInterval(function() {that.x -= 4if (that.x <= 0) {that.x = 0}that.myplaneimg.style.left = that.x + 'px'}, 30)}}// 飞机向右移动function moveright() {if (rightlock) {rightlock = falseclearInterval(lefttimer)righttimer = setInterval(function() {that.x += 4if (that.x >= gamebox.offsetWidth - that.w) {that.x = gamebox.offsetWidth - that.w}that.myplaneimg.style.left = that.x + 'px'}, 30)}}}document.addEventListener('keyup',function(ev) {var ev = ev || window.eventif (ev.keyCode == 87) {clearInterval(uptimer)uplock = true}if (ev.keyCode == 83) {clearInterval(downtimer)downlock = true}if (ev.keyCode == 65) {clearInterval(lefttimer)leftlock = true}if (ev.keyCode == 68) {clearInterval(righttimer)rightlock = true}},false)}//2.3我方飞机发射子弹Myplane.prototype.myplaneshoot = function() {var that = thisvar shoottimer = nullvar shootlock = truedocument.addEventListener('keydown', shootbullet, false)function shootbullet(ev) {var ev = ev || window.eventif (ev.keyCode == 75) {if (shootlock) {shootlock = falsefunction shoot() {new Bullet(6,14,that.x + that.w / 2 - 3,that.y - 14,'img/bullet.png')}shoot()shoottimer = setInterval(shoot, 200)}}}document.addEventListener('keyup',function(ev) {var ev = ev || window.eventif (ev.keyCode == 75) {clearInterval(shoottimer)shootlock = true}},false)}//3.子弹的构造函数function Bullet(w, h, x, y, imgurl) {//w,h宽高 x,y位置  imgurl图片路径this.w = wthis.h = hthis.x = xthis.y = ythis.imgurl = imgurl//创建子弹this.createbullet()}//3.1创建子弹Bullet.prototype.createbullet = function() {this.bulletimg = document.createElement('img')this.bulletimg.src = this.imgurlthis.bulletimg.style.cssText = `width:${this.w}px;height:${this.h}px;position:absolute;left:${this.x}px;top:${this.y}px;`gamebox.appendChild(this.bulletimg)//子弹创建完成,执行运动。this.bulletmove()}//3.2子弹运动Bullet.prototype.bulletmove = function() {var that = thisthis.timer = setInterval(function() {that.y -= 4if (that.y <= -that.h) {//让子弹消失clearInterval(that.timer)gamebox.removeChild(that.bulletimg)}that.bulletimg.style.top = that.y + 'px'that.bullethit()}, 30)}Bullet.prototype.bullethit = function() {var enemys = document.querySelectorAll('.enemy')for (var i = 0; i < enemys.length; i++) {if (this.x + this.w >= enemys[i].offsetLeft &&this.x <= enemys[i].offsetLeft + enemys[i].offsetWidth &&this.y + this.h >= enemys[i].offsetTop &&this.y <= enemys[i].offsetTop + enemys[i].offsetHeight) {clearInterval(this.timer)try {gamebox.removeChild(this.bulletimg)} catch (e) {return}//血量减1enemys[i].blood--//监听敌机的血量(给敌机添加方法)enemys[i].checkblood()}}}//4.敌机的构造函数function Enemy(w, h, x, y, imgurl, boomurl, blood, score, speed) {this.w = wthis.h = hthis.x = xthis.y = ythis.imgurl = imgurlthis.boomurl = boomurlthis.blood = bloodthis.score = scorethis.speed = speedthis.createenemy()}//4.1创建敌机图片Enemy.prototype.createenemy = function() {var that = thisthis.enemyimg = document.createElement('img')this.enemyimg.src = this.imgurlthis.enemyimg.style.cssText = `width:${this.w}px;height:${this.h}px;position:absolute;left:${this.x}px;top:${this.y}px;`gamebox.appendChild(this.enemyimg)this.enemyimg.className = 'enemy' //给每一架创建的敌机添加类this.enemyimg.score = this.score //给每一架创建的敌机添加分数this.enemyimg.blood = this.blood //给每一架创建的敌机添加自定义的属性--血量this.enemyimg.checkblood = function() {//this==>this.enemyimgif (this.blood <= 0) {//敌机消失爆炸。this.className = '' //去掉类名。this.src = that.boomurlclearInterval(that.enemyimg.timer)setTimeout(function() {gamebox.removeChild(that.enemyimg)}, 300)zscore += this.scoreoEm.innerHTML = zscore}}//子弹创建完成,执行运动。this.enemymove()}//4.2敌机运动Enemy.prototype.enemymove = function() {var that = thisthis.enemyimg.timer = setInterval(function() {that.y += that.speedif (that.y >= gamebox.offsetHeight) {clearInterval(that.enemyimg.timer)gamebox.removeChild(that.enemyimg)}that.enemyimg.style.top = that.y + 'px'that.enemyhit()}, 30)}//4.3敌机碰撞我方飞机Enemy.prototype.enemyhit = function() {if (!(this.x + this.w < ourplane.x ||this.x > ourplane.x + ourplane.w ||this.y + this.h < ourplane.y ||this.y > ourplane.y + ourplane.h)) {var enemys = document.querySelectorAll('.enemy')for (var i = 0; i < enemys.length; i++) {clearInterval(enemys[i].timer)}clearInterval(enemytimer)clearInterval(bgtimer)ourplane.myplaneimg.src = ourplane.boomurlsetTimeout(function() {gamebox.removeChild(ourplane.myplaneimg)alert('game over!!')location.reload()}, 300)}}var enemytimer = setInterval(function() {for (var i = 0; i < ranNum(1, 3); i++) {var num = ranNum(1, 20) //1-20if (num < 15) {//小飞机new Enemy(34,24,ranNum(0, gamebox.offsetWidth - 34), -24,'img/smallplane.png','img/smallplaneboom.gif',1,1,ranNum(2, 4))} else if (num >= 15 && num < 20) {new Enemy(46,60,ranNum(0, gamebox.offsetWidth - 46), -60,'img/midplane.png','img/midplaneboom.gif',3,5,ranNum(1, 3))} else if (num == 20) {new Enemy(110,164,ranNum(0, gamebox.offsetWidth - 110), -164,'img/bigplane.png','img/bigplaneboom.gif',10,10,1)}}}, 3000)function ranNum(min, max) {return Math.round(Math.random() * (max - min)) + min}//实例化我方飞机var ourplane = new Myplane(66,80,(gamebox.offsetWidth - 66) / 2,gamebox.offsetHeight - 80,'img/myplane.gif','img/myplaneBoom.gif')})()

键盘的WSAD分别控制飞机的上下左右,K键发射子弹。

四. 完整的代码和图片获取

  1. 鼠标版飞机大战资源获取:

    链接:https://pan.baidu.com/s/1NXGjetosHJ4K_pcj4RLUNQ?pwd=yyds 
    提取码:yyds
    
  2. 键盘版飞机大战资源获取:

    链接:https://pan.baidu.com/s/1YkH7dCseGJw8ssRl2lHysg?pwd=yyds 
    提取码:yyds
    

相关内容

热门资讯

安卓双系统添加应用,轻松实现多... 你有没有想过,你的安卓手机里可以同时运行两个系统呢?听起来是不是很酷?想象一边是熟悉的安卓系统,一边...
pipo安卓进系统慢,探究pi... 最近是不是发现你的Pipo安卓系统更新或者运行起来特别慢?别急,今天就来给你好好分析分析这个问题,让...
怎样使用安卓手机系统,安卓手机... 你有没有发现,安卓手机已经成为我们生活中不可或缺的一部分呢?从早晨闹钟响起,到晚上睡前刷剧,安卓手机...
双系统安卓安装caj,轻松实现... 你有没有想过,你的安卓手机里装上双系统,是不是就能同时享受安卓和Windows系统的乐趣呢?没错,这...
安卓使用ios系统教程,安卓用... 你是不是也和我一样,对安卓手机上的iOS系统充满了好奇?想要体验一下苹果的优雅和流畅?别急,今天我就...
安卓系统gps快速定位,畅享便... 你有没有遇到过这样的情况:手机里装了各种地图导航软件,但每次出门前都要等上好几分钟才能定位成功,急得...
安卓手机系统更新原理,原理与流... 你有没有发现,你的安卓手机最近是不是总在提醒你更新系统呢?别急,别急,让我来给你揭秘一下安卓手机系统...
安卓系统通知管理,全面解析与优... 你有没有发现,手机里的通知就像是一群调皮的小精灵,时不时地跳出来和你互动?没错,说的就是安卓系统的通...
安卓系统手机哪买,揭秘哪里购买... 你有没有想过,拥有一部安卓系统手机是多么酷的事情呢?想象你可以自由安装各种应用,不受限制地探索各种功...
安卓系统 ipv4,基于安卓系... 你知道吗?在智能手机的世界里,有一个系统可是无人不知、无人不晓,那就是安卓系统。而在这个庞大的安卓家...
目前安卓是什么系统,探索安卓系... 亲爱的读者,你是否曾好奇过,如今安卓系统究竟是什么模样?在这个科技飞速发展的时代,操作系统如同人体的...
安卓6.0系统比5.0,从5.... 你有没有发现,自从手机更新了安卓6.0系统,感觉整个人都清爽了不少呢?没错,今天咱们就来聊聊这个话题...
安卓2.36系统升级,功能革新... 你知道吗?最近安卓系统又来了一次大变身,那就是安卓2.36系统升级!这可不是一个小打小闹的更新,而是...
安卓系统源码怎么打开,并可能需... 你有没有想过,安卓系统的源码就像是一扇神秘的门,隐藏着无数的技术秘密?想要打开这扇门,你得掌握一些小...
安卓8.0系统体验视频,智能革... 你有没有听说安卓8.0系统最近可是火得一塌糊涂啊!作为一个紧跟科技潮流的数码达人,我当然要来给你好好...
宣传系统漫画app安卓,探索安... 亲爱的读者们,你是否曾在某个午后,百无聊赖地打开手机,想要寻找一些轻松愉悦的读物?今天,我要给你介绍...
鸿蒙替换安卓系统吗,开启智能生... 你知道吗?最近科技圈里可是炸开了锅,因为华为的新操作系统鸿蒙系统,据说要大举进军手机市场,替换掉安卓...
手机安卓系统深度清理,解锁手机... 手机里的东西是不是越来越多,感觉就像一个装满了杂物的储物柜?别急,今天就来教你一招——手机安卓系统深...
安卓上的windows系统,融... 你有没有想过,在安卓手机上也能体验到Windows系统的魅力呢?没错,这就是今天我要跟你分享的神奇故...
安卓系统焦点变化事件,Andr... 你知道吗?在安卓系统的世界里,最近发生了一件超级有趣的事情——焦点变化事件。这可不是什么小打小闹,它...