HTML+CSS+jquery实现飞机大战游戏

本文分享了一位作者利用HTML、CSS和jQuery实现飞机大战游戏的过程。首先,通过HTML和CSS构建了游戏的基本页面结构,接着介绍了如何用JavaScript(包括jQuery)进行动态效果和交互的实现。虽然文中用简单的方块代替飞机,但提到了可以使用数组遍历方法绘制更复杂的飞机图形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        这几天闲着没事运用html+css+jquery写了个飞机大战的游戏 分享下自己的思路:

        一:界面构建:
                 1.首先,先用HTML+CSS构建基本的页面结构,这里的设计如下图:

/*先给网页上所有元素添加一个红色的边框 方便布局 布局完毕后移除*/
*{
   box-sizing: border-box;
   border:1px solid red;
}

HTML代码:

<div class="header">
			<!--<div id="time" class="time"></div>-->
			<div id="score" class="score">0000</div>
		</div>
		
		<div id="content" class="content">
			<div id="player" class="player" style="left: 375px;"></div>
		</div>
		
		<div class="footer">
			<div class="lifePoints" :duration="4000"><div class="lifeBar"></div></div>
			<div class="skills">
				<div id="ASkill" class="skill" style="background:firebrick;color:#fff">A</div>
				<div id="SSkill" class="skill" style="background:lightskyblue;color:#fff">S</div>
				<div id="DSkill" class="skill" style="background:purple;color:#fff">D</div>
				<div id="FSkill" class="skill" style="background:lightgoldenrodyellow">F</div>
				
			</div>
			<div class="magicPoints"><div class="magicBar"></div></div>
		</div>

css代码:

body{
				position: relative;
				padding:0;
				margin: 0;
				
			}
			.header{
				position: fixed;
				line-height: 50px;
				top:0;
				left: 0;
				text-align: center;
				height: 50px;
				width:100%;
				z-index: 999;
				background:#555;
				color:goldenrod;
				font-size:30px;
			}
			.content{
				position: relative;
				margin: 50px auto 0 auto;
				width:100%;
				/*height: calc(100vh - 170px);*/
				height: 500px;
			}
			.footer{
				position: fixed;
				width: 100%;
				bottom:0;
				left:0;
				height: 120px;
			}
			.lifePoints{
				position: relative;
				width: 100%;
				height: 20px;
			}
			.lifeBar{
				position: absolute;
				left: 0;
				height: 20px;
				width: 0%;
				background:deeppink;
				transition: all ease .5s; 
				border-radius: 10px;
			}
			.skills{
				float: left;
				width: 75%;
				height: 100px;
			}
			.skill{
				float: left;
				width: 25%;
				height: 100px;
				line-height: 100px;
				text-align: center;
			}
			.magicPoints{
				position: relative;
				float:left;
				width: 25%;
				height: 100px;
			}
			.magicBar{
				position: absolute;
				width: 100%;
				height: 0px;
				background:deepskyblue;
				transition: all ease .5s; 
				border-radius: 30%;
				bottom: 0;
			}
			.player{
				width: 50px;
				height: 50px;
				background:red;
				position: absolute;
				bottom: 0;
			
			}
			.enemy{
				width: 50px;
				height: 50px;
				background:black;
				position: absolute;
				top: 0;
			
			}
			.shoot{
				position: absolute;
				top:calc(100% - 50px);
				width:8px;
				height:8px;
				border-radius: 50%;
				background:red;
				z-index: 1;
			}
			[class$=Skill]{
				width:20px;
				height:20px;
			}
			.enemy[type="A"],.enemy[type="S"],.enemy[type="D"],.enemy[type="F"]{
				width:50px;
				height: 50px;
			}

js部分:

var data = {
			playerX: 0, //玩家x轴坐标
			playerY: 0, //玩家y轴坐标
			enemyX: [], //敌人X坐标集
			enemyY: [], //敌人Y坐标集
			shootX: [], //子弹X坐标集
			shootY: [], //子弹Y坐标集
			score: 0, //分数
			lifePoint: 100, //玩家生命值
			magicPoint: 0, //玩家魔力值,满100能放必杀,通过击杀敌人获得
			isBoss:false,//是否Boss
			//随机数方法
			random: function(min, max) {
				return Math.floor(Math.random() * max + min);
			},
			//创建生命条
			createLifeBar:function(){
				$(".lifeBar").css("width",data.lifePoint+"%")
			},
			//创建法力条
			createMagicBar:function(){
				$(".magicBar").css("height",data.magicPoint+"%")	
			},
			
			//更新玩家分数
			refreshScore:function(){
				$("#score").html(data.score);
				
			},
			
			//创建敌人
			createEnemy:function(){
				var temp=data.random(0,100);//生成0-100的随机数,用作概率判断
				var windowWidth = $(window).width();//浏览器宽度
				var left = data.random(0,windowWidth - 50)//敌人left值
				if(temp>0&&temp<=10){//A类精英,能且只能通过A技能击杀
					var html = '<div class="enemy" style="background:firebrick;left:'+left+'px" life="200" type="A" lifeUp&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值