【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
    

相关内容

热门资讯

安卓系统自带的网页,功能与特色... 你有没有发现,每次打开安卓手机,那熟悉的系统界面里总有一个默默无闻的小家伙——安卓系统自带的网页浏览...
美咖云系统安卓版,开启智能生活... 你有没有发现,最近手机上多了一个叫“美咖云系统安卓版”的小家伙?它就像一个魔法师,轻轻一点,就能让你...
安卓系统推荐最好的手机,盘点性... 你有没有想过,拥有一部性能卓越的手机,就像是拥有了移动的宝藏库?在这个信息爆炸的时代,一部好手机不仅...
安卓11系统能精简吗,释放潜能 你有没有发现,随着手机越来越智能,系统也越来越庞大?安卓11系统,这个最新的操作系统,是不是也让你觉...
安卓自动重启系统软件,揭秘原因... 手机突然自动重启,是不是感觉整个人都不好了?别急,今天就来和你聊聊这个让人头疼的安卓自动重启系统软件...
苹果手机x刷安卓系统,探索安卓... 你有没有想过,你的苹果手机X竟然也能刷上安卓系统?是的,你没听错,就是那个一直以来都和我们苹果手机X...
安卓系统智商低吗,智商低下的真... 你有没有想过,为什么安卓系统的智商总被调侃得好像有点低呢?是不是觉得它总是慢吞吞的,有时候还犯点小错...
安卓系统手机联系人,揭秘你的社... 你有没有发现,手机里的联系人列表就像是一个小小的社交圈呢?里面藏着我们的亲朋好友、工作伙伴,甚至还有...
安卓系统免费铃声下载,打造个性... 手机里那首老掉牙的铃声是不是让你觉得有点out了呢?别急,今天就来给你支个招,让你轻松给安卓手机换上...
安卓系统用哪个桌面好,打造个性... 你有没有发现,手机桌面可是我们每天都要面对的“脸面”呢?换一个好看的桌面,心情都能跟着好起来。那么,...
虚拟大师是安卓10系统,功能与... 你知道吗?最近在手机圈里,有个新玩意儿引起了不小的轰动,那就是虚拟大师!而且,更让人惊喜的是,这个虚...
安卓系统与苹果优缺点,系统优缺... 说到手机操作系统,安卓和苹果绝对是两大巨头,它们各有各的特色,就像两道不同的美味佳肴,让人难以抉择。...
安卓win双系统主板,融合与创... 你有没有想过,一台电脑如果既能流畅运行安卓系统,又能轻松驾驭Windows系统,那该有多爽啊?没错,...
安卓系统可精简软件,轻松提升手... 你有没有发现,手机里的安卓系统越来越庞大,软件也越装越多,有时候感觉手机就像个“大肚子”,不仅运行速...
安卓系统基于linux的代码,... 你有没有想过,那个陪伴你每天刷抖音、玩游戏、办公的安卓系统,其实背后有着一套复杂的基于Linux的代...
苹果和安卓的拍照系统,谁更胜一... 你有没有发现,现在手机拍照已经成为我们生活中不可或缺的一部分呢?无论是记录生活的点滴,还是捕捉美丽的...
苹果和安卓系统不同吗,系统差异... 你有没有想过,为什么你的手机里装的是苹果的iOS系统,而朋友的手机却是安卓系统呢?这两种系统,看似都...
安卓系统有多少级,揭秘其多级架... 你有没有想过,那个陪伴我们日常生活的安卓系统,它其实有着丰富的层级结构呢?没错,就是那个让我们的手机...
华为鸿蒙系统与安卓的,技术融合... 你知道吗?最近科技圈可是炸开了锅,华为鸿蒙系统与安卓的较量成为了大家热议的话题。这不,今天我就来给你...
什么安卓手机是苹果系统,搭载苹... 你有没有想过,为什么有些人宁愿花大价钱买苹果手机,而有些人却对安卓手机情有独钟呢?其实,这个问题背后...