【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
    

相关内容

热门资讯

电视安卓系统哪个品牌好,哪家品... 你有没有想过,家里的电视是不是该升级换代了呢?现在市面上电视品牌琳琅满目,各种操作系统也是让人眼花缭...
安卓会员管理系统怎么用,提升服... 你有没有想过,手机里那些你爱不释手的APP,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...