Vue 开发一个简略版的飞机大战小游戏


使用 Vue 开发一个简略版的飞机大战小游戏

如题,假设你为了向更多访问你博客的人展示你的技术,你决定小试身手开发一个飞机大战小游戏。

功能: 开始游戏前用户名必填,玩家可以发射子弹,敌军与行星随机出现,鼠标可操控玩家移动,敌军可发射子弹


一、实现思路

如题所述:

玩家可操控玩家飞机可发射子弹,敌军与行星随机生成;

这意味着我们需要一个单独的玩家飞机dom,以及敌军、行星与子弹 用 vue 循环生成的3个dom。

敌军与行星生成后的dom的位置由数据里的 x 与 y 值决定。

按下空格时产生的子弹由当时按下空格键的时候的飞机的位置来决定。

敌军随机发射的子弹由当时发射子弹的敌军的位置来决定。

游戏开始时用户名必填,那么我们只需要在 Vue 实例里为该 input 绑定一个数据,再为开始游戏按钮绑定点击事件。随后计算用户名的长度只要大于3,就调用游戏开始函数或初始化函数。

玩家鼠标操控移动飞机移动只需要为其父节点绑定鼠标移动事件,然后更改 player 里的 x 与 y 的数据 (x与y的值不能小于0,x与y的值不能大于父节点的宽高) 并且赋予 玩家飞机即可。

击毁敌军只需要拿 子弹与敌军 的 x,y 计算对比即可。

二、所需知识点

  1. Vue 事件绑定
  2. Vue 监听事件
  3. Vue 计算属性
  4. Vue Style操作

三、实现步骤

  • 第一步:创建 HTML 与 CSS 文件

    HTML

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vue 飞机大战</title>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body>
		<main>
			-
			<div class="game-plane" 
				@mousemove="touchmove"
				:style="{
      backgroundPosition:'0px '+ positionY +'px'}" ref='plane'>
				
				<div id="hit">
					<h2>击毁:{
  { hitCount }}</h2>
					<h2>与敌机相撞:{
  { boom }}</h2>
					<h2>被击中次数:{
  { HitTimes }}</h2>
					<h2>用户名:{
  { username }}</h2>
				</div>
				
				<!-- 玩家 -->
				<img src="image/player.png" alt="player" id="p" :style="{
      top:p.y + 'px',left:p.x+'px'}">
				
				<!-- 星球 -->
				<img v-for="(item,index) of plane.arr" :style="{
      top:item.y + 'px',left:item.x+'px'}" src="image/plane.png" alt="plane">
				
				<!-- 敌军 -->
				<img v-for="(item,index) of e.arr" :style="{
      top:item.y + 'px',left:item.x+'px'}" src="image/e.png" class="e" alt="e">
				
				<!-- 子弹 -->
				<img v-for="(item,index) of bullets.arr" class="b"
				 :style="{
      top:item.y + 'px',left:item.x+'px'}" 
				 :src="item.tag == 'p' ? 'image/p_b.png' : 'image/e_b.png' " 
				 alt="p_b">
				
			</div>
		
			<!-- 开始面板 -->
			<div class="alert" ref="alert">
				<div class="content">
					<div class="left">
						
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Vue开发飞机大战游戏的简要步骤: 1.创建Vue实例并定义数据,包括玩家、敌人、子弹等的位置和状态。 2.使用Vue的计算属性来计算玩家、敌人、子弹的样式和位置。 3.使用Vue的事件绑定来监听键盘事件,控制玩家移动和发射子弹。 4.使用Vue的监听事件来监听敌人和子弹的碰撞事件,更新敌人和子弹的状态。 5.使用Vue的Style操作来动态设置敌人、玩家、子弹的样式。 6.最后,将Vue实例挂载到HTML页面上。 下面是一个简单的Vue飞机大战游戏的代码示例: ```html <template> <div class="game"> <div class="player" :style="playerStyle"></div> <div class="enemy" v-for="(enemy, index) in enemies" :key="index" :style="enemyStyle(enemy)"></div> <div class="bullet" v-for="(bullet, index) in bullets" :key="index" :style="bulletStyle(bullet)"></div> </div> </template> <script> export default { data() { return { player: { x: 0, y: 0, width: 50, height: 50, }, enemies: [], bullets: [], }; }, computed: { playerStyle() { return { left: `${this.player.x}px`, top: `${this.player.y}px`, width: `${this.player.width}px`, height: `${this.player.height}px`, }; }, }, methods: { enemyStyle(enemy) { return { left: `${enemy.x}px`, top: `${enemy.y}px`, width: `${enemy.width}px`, height: `${enemy.height}px`, }; }, bulletStyle(bullet) { return { left: `${bullet.x}px`, top: `${bullet.y}px`, }; }, movePlayer(event) { switch (event.keyCode) { case 37: this.player.x -= 10; break; case 38: this.player.y -= 10; break; case 39: this.player.x += 10; break; case 40: this.player.y += 10; break; case 32: this.bullets.push({ x: this.player.x + this.player.width / 2, y: this.player.y, }); break; } }, moveEnemies() { setInterval(() => { this.enemies.push({ x: Math.floor(Math.random() * 500), y: 0, width: 50, height: 50, }); }, 1000); setInterval(() => { this.enemies.forEach((enemy, index) => { enemy.y += 10; if (enemy.y > 500) { this.enemies.splice(index, 1); } }); }, 100); }, moveBullets() { setInterval(() => { this.bullets.forEach((bullet, index) => { bullet.y -= 10; if (bullet.y < 0) { this.bullets.splice(index, 1); } }); }, 100); }, checkCollision() { setInterval(() => { this.enemies.forEach((enemy, enemyIndex) => { this.bullets.forEach((bullet, bulletIndex) => { if ( bullet.x >= enemy.x && bullet.x <= enemy.x + enemy.width && bullet.y <= enemy.y + enemy.height ) { this.enemies.splice(enemyIndex, 1); this.bullets.splice(bulletIndex, 1); } }); }); }, 100); }, }, mounted() { window.addEventListener("keydown", this.movePlayer); this.moveEnemies(); this.moveBullets(); this.checkCollision(); }, }; </script> <style> .game { position: relative; width: 500px; height: 500px; border: 1px solid #ccc; } .player { position: absolute; background-color: blue; } .enemy { position: absolute; background-color: red; } .bullet { position: absolute; width: 5px; height: 10px; background-color: black; } </style> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值