经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue实现飞机大战小游戏
来源:jb51  时间:2022/5/9 9:44:03  对本文有异议

使用 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

  1. <!DOCTYPE html>
  2. <html>
  3. ?? ?<head>
  4. ?? ??? ?<meta charset="utf-8">
  5. ?? ??? ?<title>Vue 飞机大战</title>
  6. ?? ??? ?<link rel="stylesheet" href="css/style.css" >
  7. ?? ?</head>
  8. ?? ?<body>
  9. ?? ??? ?<main>
  10. ?? ??? ??? ?-
  11. ?? ??? ??? ?<div class="game-plane"?
  12. ?? ??? ??? ??? ?@mousemove="touchmove"
  13. ?? ??? ??? ??? ?:style="{backgroundPosition:'0px '+ positionY +'px'}" ref='plane'>
  14. ?? ??? ??? ??? ?
  15. ?? ??? ??? ??? ?<div id="hit">
  16. ?? ??? ??? ??? ??? ?<h2>击毁:{{ hitCount }}</h2>
  17. ?? ??? ??? ??? ??? ?<h2>与敌机相撞:{{ boom }}</h2>
  18. ?? ??? ??? ??? ??? ?<h2>被击中次数:{{ HitTimes }}</h2>
  19. ?? ??? ??? ??? ??? ?<h2>用户名:{{ username }}</h2>
  20. ?? ??? ??? ??? ?</div>
  21. ?? ??? ??? ??? ?
  22. ?? ??? ??? ??? ?<!-- 玩家 -->
  23. ?? ??? ??? ??? ?<img src="image/player.png" alt="player" id="p" :style="{top:p.y + 'px',left:p.x+'px'}">
  24. ?? ??? ??? ??? ?
  25. ?? ??? ??? ??? ?<!-- 星球 -->
  26. ?? ??? ??? ??? ?<img v-for="(item,index) of plane.arr" :style="{top:item.y + 'px',left:item.x+'px'}" src="image/plane.png" alt="plane">
  27. ?? ??? ??? ??? ?
  28. ?? ??? ??? ??? ?<!-- 敌军 -->
  29. ?? ??? ??? ??? ?<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">
  30. ?? ??? ??? ??? ?
  31. ?? ??? ??? ??? ?<!-- 子弹 -->
  32. ?? ??? ??? ??? ?<img v-for="(item,index) of bullets.arr" class="b"
  33. ?? ??? ??? ??? ? :style="{top:item.y + 'px',left:item.x+'px'}"?
  34. ?? ??? ??? ??? ? :src="item.tag == 'p' ? 'image/p_b.png' : 'image/e_b.png' "?
  35. ?? ??? ??? ??? ? alt="p_b">
  36. ?? ??? ??? ??? ?
  37. ?? ??? ??? ?</div>
  38. ?? ??? ?
  39. ?? ??? ??? ?<!-- 开始面板 -->
  40. ?? ??? ??? ?<div class="alert" ref="alert">
  41. ?? ??? ??? ??? ?<div class="content">
  42. ?? ??? ??? ??? ??? ?<div class="left">
  43. ?? ??? ??? ??? ??? ??? ?<h1>Vue 飞机大战</h1>
  44. ?? ??? ??? ??? ??? ??? ?<p>作者:柴不是柴</p>
  45. ?? ??? ??? ??? ??? ??? ?<img :src="faceChange" class="face">
  46. ?? ??? ??? ??? ??? ?</div>
  47. ?? ??? ??? ??? ??? ?<div class="right">
  48. ?? ??? ??? ??? ??? ??? ?<input type="text" v-model="username" placeholder="请输入你的名字">
  49. ?? ??? ??? ??? ??? ??? ?<input type="submit" @click="startBtnClick" ?value="开始游戏">
  50. ?? ??? ??? ??? ??? ?</div>
  51. ?? ??? ??? ??? ?</div>
  52. ?? ??? ??? ?</div>
  53. ?? ??? ?</main>
  54. ?? ??? ?
  55. ?? ??? ?<script src="js/vue.js"></script>
  56. ?? ??? ?<script src="js/data.js"></script>
  57. ?? ??? ?<script src="js/app.js"></script>
  58. ?? ?</body>
  59. </html>

CSS

  1. * {
  2. ?? ?padding: 0;
  3. ?? ?margin: 0;
  4. }
  5.  
  6. main {
  7. ?? ?display: flex;
  8. ?? ?justify-content: center;
  9. ?? ?align-items: center;
  10. ?? ?width: 100%;
  11. ?? ?height: 100vh;
  12. ?? ?background-color: #282828;
  13. }
  14.  
  15. main .game-plane {
  16. ?? ?position: relative;
  17. ?? ?width: 1200px;
  18. ?? ?max-width: 1200px;
  19. ?? ?height: 900px;
  20. ?? ?background-image: url(../image/background.png);
  21. ?? ?background-size: 100% auto;
  22. ?? ?box-shadow: 0 2px 30px rgba(255,255,255,0.5);
  23. ?? ?overflow: hidden;
  24. }
  25.  
  26. main .game-plane img { position: absolute; }
  27.  
  28. .alert {
  29. ?? ?position: absolute;
  30. ?? ?top: calc(50% - 100px);
  31. ?? ?left: 0;
  32. ?? ?width: 100%;
  33. ?? ?height: 200px;
  34. ?? ?background: #FFF;
  35. ? ? box-shadow: 0 0 0 999em rgba(0, 0, 0, 0.5);
  36. }
  37.  
  38. .alert .content {
  39. ?? ?display: grid;
  40. ?? ?grid-template-columns: 4fr 6fr;
  41. ?? ?grid-template-rows: 100%;
  42. ?? ?gap: 20px;
  43. ?? ?margin: 0 auto;
  44. ?? ?max-width: 1200px;
  45. ?? ?width: 100%;
  46. ?? ?height: 100%;
  47. }
  48.  
  49. .alert .content .left {
  50. ?? ?display: flex;
  51. ?? ?flex-direction: column;
  52. ?? ?align-items: center;
  53. ?? ?justify-content: center;
  54. }
  55.  
  56. .alert .content .left * { margin: 5px 0; }
  57.  
  58. .alert .content .right {
  59. ?? ?display: flex;
  60. ?? ?flex-direction: column;
  61. ?? ?align-items: center;
  62. ?? ?justify-content: center;
  63. }
  64.  
  65. .alert .content .right input {
  66. ?? ?width: 100%;
  67. ?? ?display: block;
  68. ?? ?box-sizing: border-box;
  69. ?? ?padding: 10px;
  70. }
  71.  
  72. .e { transform: rotate(180deg); }
  73.  
  74. .b { width: 30px; }#hit {
  75. ?? ?position: absolute;
  76. ?? ?top: 20px;
  77. ?? ?left: 20px;
  78. ?? ?color: #FFF;
  79. }

第二步:创建一个全局 data 文件

  1. window.el = document.querySelector(".game-plane");
  2. window.data = {
  3. ?? ?p : {// 玩家 Player
  4. ?? ??? ?w : document.querySelector("#p").offsetWidth,
  5. ?? ??? ?h : document.querySelector("#p").offsetHeight,
  6. ?? ??? ?x : el.offsetWidth / 2 - document.querySelector("#p").offsetWidth / 2,
  7. ?? ??? ?y : el.offsetHeight - document.querySelector("#p").offsetHeight
  8. ?? ?},
  9. ?? ?
  10. ?? ?e : {// 敌机 enemy plane
  11. ?? ??? ?arr : [],
  12. ?? ??? ?speed : 6,
  13. ?? ?},
  14. ?? ?
  15. ?? ?plane : { arr : [] },// 星球?? ?
  16. ?? ?bullets : { arr : [] },// 子弹
  17. ?? ?hitCount : 0,// 击中总数
  18. ?? ?boom : 0,// 碰撞次数
  19. ?? ?HitTimes : 0,// 被击中次数
  20. ?? ?start : false,// 游戏是否开始
  21. ?? ?positionY : 0,// 背景 Y 值
  22. ?? ?timers : [],// 定时器
  23. ?? ?face : "ordinary",// 表情
  24. ?? ?username : "" // 玩家名
  25. }

第三步:创建Vue 实例

  1. var Game = new Vue({
  2. ?? ?el : "main",
  3. ?? ?data,
  4. ?? ?
  5. ?? ?methods:{
  6. ?? ??? ?startBtnClick() {
  7. ?? ??? ??? ?if ( this.username.length <= 2 ) return alert("用户名不可少于3位字符哦!");
  8. ?? ??? ??? ?this.init();
  9. ?? ??? ?},
  10. ?? ??? ?
  11. ?? ??? ?init() {// 初始化
  12. ?? ??? ??? ?let _this = this;
  13. ?? ??? ??? ?this.start = true;
  14. ?? ??? ??? ?this.$refs.alert.style.display = "none";
  15. ?? ??? ??? ?
  16. ?? ??? ??? ?this.createE();
  17. ?? ??? ??? ?this.createPlane();
  18. ?? ??? ??? ?this.timers.push( setInterval( this.bgMove,20 ) )
  19. ?? ??? ??? ?this.timers.push( setInterval(function() { _this.move('bullets') }, 20 ) )
  20. ?? ??? ?},
  21. ?? ??? ?
  22. ?? ??? ?bgMove () { // 背景移动 顺带判断玩家是否装上敌军
  23. ?? ??? ??? ?this.positionY += 5;?
  24. ?? ??? ??? ?if ( this.hit_check(this.p) ) this.boom++;
  25. ?? ??? ?},
  26. ?? ??? ?
  27. ?? ??? ?touchmove(){// 飞机移动
  28. ?? ??? ??? ?let touch,x,y;
  29. ?? ??? ??? ?if ( !this.start ) return;
  30. ?? ??? ??? ?
  31. ?? ??? ??? ?if(event.touches) touch = event.touches[0];
  32. ?? ??? ??? ?else touch = event;
  33. ?? ??? ??? ?
  34. ?? ??? ??? ?x = touch.clientX - this.$refs.plane.offsetLeft - this.p.w / 2;
  35. ?? ??? ??? ?y = touch.clientY - this.$refs.plane.offsetTop - this.p.h / 2;
  36. ?? ??? ??? ?
  37. ?? ??? ??? ?y = y < 0 ? 0 : y > (this.$refs.plane.offsetHeight - this.p.h) ? this.$refs.plane.offsetHeight - this.p.h : y;
  38. ?? ??? ??? ?x = x < 0 ? 0 : x > (this.$refs.plane.offsetWidth - this.p.w) ? this.$refs.plane.offsetWidth - this.p.w : x;
  39. ?? ??? ??? ?
  40. ?? ??? ??? ?this.p.x = x;
  41. ?? ??? ??? ?this.p.y = y;
  42. ?? ??? ?},
  43. ?? ??? ?
  44. ?? ??? ?createE() { // 创建敌军
  45. ?? ??? ??? ?let _this = this,x;
  46. ?? ??? ??? ?
  47. ?? ??? ??? ?this.timers.push( setInterval( function() {
  48. ?? ??? ??? ??? ?x = Math.ceil( Math.random() * ( _this.$refs.plane.offsetWidth - 80 ) );
  49. ?? ??? ??? ??? ?_this.build('e',{ x: x, y: 5 }) ?? ?
  50. ?? ??? ??? ?}, 1000 ));
  51. ?? ??? ??? ?
  52. ?? ??? ??? ?this.timers.push( setInterval( function() { _this.move('e') }, 20 ));
  53. ?? ??? ?},
  54. ?? ??? ?
  55. ?? ??? ?createPlane() {// 创建行星
  56. ?? ??? ??? ?let _this = this,x;
  57. ?? ??? ??? ?
  58. ?? ??? ??? ?this.timers.push( setInterval( function() {
  59. ?? ??? ??? ??? ?x = Math.ceil( Math.random() * ( _this.$refs.plane.offsetWidth - 80 ) );
  60. ?? ??? ??? ??? ?_this.build('plane',{ x: x, y: 5 })?
  61. ?? ??? ??? ?}, 2000 ));
  62. ?? ??? ??? ?
  63. ?? ??? ??? ?this.timers.push( setInterval( function() { _this.move('plane') }, 20 ));
  64. ?? ??? ?},
  65. ?? ??? ?
  66. ?? ??? ?createButter(table,e) {// 创建子弹
  67. ?? ??? ??? ?if ( !this.start ) return;
  68. ?? ??? ??? ?
  69. ?? ??? ??? ?let bullter = {
  70. ?? ??? ??? ??? ?x:(e.x + (e.w ? e.w : 30) / 2),
  71. ?? ??? ??? ??? ?y:e.y - (e.h ? e.h : -30),
  72. ?? ??? ??? ??? ?speed : table == "p" ? -6 : 10,
  73. ?? ??? ??? ??? ?tag : table
  74. ?? ??? ??? ?};
  75. ?? ??? ??? ?
  76. ?? ??? ??? ?this.build('bullets',bullter);
  77. ?? ??? ?},
  78. ?? ??? ?
  79. ?? ??? ?build(table,data) {// 公共创建
  80. ?? ??? ??? ?let _this = this;
  81. ?? ??? ??? ?this[table].arr.push( data );
  82. ?? ??? ?},
  83. ?? ??? ?
  84. ?? ??? ?move(table) {// 公共移动
  85. ?? ??? ??? ?for( let i = 0; i < this[table].arr.length; i ++ ){
  86. ?? ??? ??? ??? ?let e = this[table].arr[i],
  87. ?? ??? ??? ??? ??? ?math = Math.random() * 100,
  88. ?? ??? ??? ??? ??? ?speed = this[table].speed ? this[table].speed : 5;
  89. ?? ??? ??? ??? ?
  90. ?? ??? ??? ??? ?if ( table == 'bullets' ) speed = e.speed;
  91. ?? ??? ??? ??? ?
  92. ?? ??? ??? ??? ?e.y += speed;
  93. ?? ??? ??? ? ?
  94. ?? ??? ??? ??? ?if ( table !== 'bullets' ) {// 如果不是子弹dom的移动
  95. ?? ??? ??? ??? ??? ?if( e.y > this.$refs.plane.offsetHeight - 55 ) this[table].arr.splice(i,1);
  96. ?? ??? ??? ??? ??? ?
  97. ?? ??? ??? ??? ??? ?if ( table == 'e' && math < 1 ) { this.createButter('e',e); }
  98. ?? ??? ??? ??? ?} else {
  99. ?? ??? ??? ??? ??? ?if ( e.tag == 'p' ) {
  100. ?? ??? ??? ??? ??? ??? ?if ( this.hit_check(e) ) this[table].arr.splice(i,1);
  101. ?? ??? ??? ??? ??? ??? ?else if ( e.y < 0 ) this[table].arr.splice(i,1);
  102. ?? ??? ??? ??? ??? ?} else {
  103. ?? ??? ??? ??? ??? ??? ?if ( this.hit(e,this.p) ) {
  104. ?? ??? ??? ??? ??? ??? ??? ?this[table].arr.splice(i,1);
  105. ?? ??? ??? ??? ??? ??? ??? ?this.HitTimes++;
  106. ?? ??? ??? ??? ??? ??? ?}
  107. ?? ??? ??? ??? ??? ??? ?else if ( e.y > this.$refs.plane.offsetHeight - 30 ) this[table].arr.splice(i,1);
  108. ?? ??? ??? ??? ??? ?}
  109. ?? ??? ??? ??? ?}
  110. ?? ??? ??? ?}
  111. ?? ??? ?},
  112. ?? ??? ?
  113. ?? ??? ?hit_check(b) {// 是否击毁敌军
  114. ?? ??? ??? ?for( let i = 0; i < this.e.arr.length; i ++ ){
  115. ?? ??? ??? ??? ?if( this.hit(b,this.e.arr[i]) ){?
  116. ?? ??? ??? ??? ??? ?this.e.arr.splice(i,1);
  117. ?? ??? ??? ??? ??? ?this.hitCount++;
  118. ?? ??? ??? ??? ??? ?return true;
  119. ?? ??? ??? ??? ?}
  120. ?? ??? ??? ?}
  121. ?? ??? ?},
  122. ?? ??? ?
  123. ?? ??? ?hit(b,e) {// 碰撞
  124. ?? ??? ??? ?let d = this.judgeHit( b.x, b.y, e.x, e.y );
  125. ?? ??? ??? ?if( d < 35 ) return true;
  126. ?? ??? ?},
  127. ?? ??? ?
  128. ?? ??? ?judgeHit(x1, y1, x2, y2) {// 计算两个点的距离差
  129. ?? ??? ??? ?let a = x1 - x2,
  130. ?? ??? ??? ??? ?b = y1 - y2,
  131. ?? ??? ??? ??? ?result = Math.sqrt( Math.pow( a, 2) + Math.pow( b, 2 ) );
  132. ?? ??? ??? ?return Math.round( result );
  133. ?? ??? ?},
  134. ?? ??? ?
  135. ?? ??? ?pause() {// 暂停
  136. ?? ??? ??? ?this.start = false;
  137. ?? ??? ??? ?this.timers.forEach(element => { clearInterval(element); })
  138. ?? ??? ?}
  139. ?? ?},
  140. ?? ?
  141. ?? ?watch: {
  142. ?? ??? ?username () {// 监听玩家输入事件
  143. ?? ??? ??? ?if ( this.username.length > 2 ) this.face = "shy";
  144. ?? ??? ??? ?else this.face = "ordinary";
  145. ?? ??? ?}
  146. ?? ?},
  147.  
  148. ?? ?mounted(){
  149. ?? ??? ?let _this = this;
  150. ?? ??? ?document.onkeyup = function(e) {
  151. ?? ??? ??? ?( e.keyCode == 32 ) && _this.createButter("p",_this.p);
  152. ?? ??? ??? ?// ( e.keyCode == 80 ) && _this.pause();
  153. ?? ??? ?}
  154. ?? ?},
  155. ?? ?
  156. ?? ?computed:{ faceChange() { return "image/"+this.face + ".png"; } }
  157. });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号