2020征文-手表深鸿会深大小组:鸿蒙HarmonyOS手表游戏之黑白翻棋

系统 OpenHarmony
此次是深鸿会深大小组(Zzt_01-23)学习完HarmonyOS后,自行开发的第一个demo——黑白翻棋,详细讲述了黑白翻棋的编写思路,内含详细解释,同时也欢迎与各位感兴趣的读者一起学习HarmonyOS开发,相互交流、共同进步。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 前言

此次是深鸿会深大小组(Zzt_01-23)学习完HarmonyOS后,自行开发的第一个demo——黑白翻棋,详细讲述了黑白翻棋的编写思路,内含详细解释,同时也欢迎与各位感兴趣的读者一起学习HarmonyOS开发,相互交流、共同进步。

概述

本个demo将从零开始完成鸿蒙小游戏APP在可穿戴设备上的编译,此处以运动手表为例,在项目中我们所使用到的软件为DevEco Studio,下载地址为:DevEco Studio下载,在项目中我们要实现的内容为黑白翻棋APP的开发。

在初始界面中显示7*7的棋盘,棋盘中黑白色块为随意打乱的,棋盘上面显示游戏翻转的次数,棋盘下方显示一个“重新开始”的按钮,为用户提供重新开始改游戏。


点击7*7棋盘中任一色块,其上下左右四个色块也会跟着一起变色(在边缘的色块则只会改变其中若干个色块的颜色),棋盘上方的当前步数则会相应依次增加。


经过若干次点击后,当所有的色块都为白色时,则会弹出游戏成功界面,此时再点击棋盘,不会有任何变化,点击“重新开始”的按钮时则会重新返回步骤1界面所示。


正文

创建项目文件

DevEco Studio下载成功后,点击左上角的File,点击New,再选择New Project,选择Lite Wearable选项,选择默认的模板,然后选择保存路径,将文件命名为MyGame(文件名不能出现中文或者特殊字符,否则将无法成功创建项目文件),如图所示。


主要编写的文件为index.css、index.hml和index.js,打开路径如图所示,index.hml用于描述页面中包含哪些组件,index.css用于描述页面中的组件都长什么样,index.js用于描述页面中的组件是如何进行交互的。


实现开始界面的布局

首先我们要先在运动手表上画出一个7*7的棋盘,色块颜色先设定为全是白色,棋盘上方显示“当前步数:0”,棋盘下方有一个“重新开始”的按钮,如图所示:


1。首先在index.hml文件中创建一个基础容器div类名为container,在此容器中间添加一个文字组件text类名为steps,并且写上显示的固定部分”当前步数:”,为动态变换部分赋予一个名为currentSteps的变量,再添加一个画布组件canvas类名为canvas,增加一个引用属性ref,以便在此画布上画出7*7表格,最后添加一个普通按钮,类名为bit,并赋值“重新开始”。

  1. <div class="container" > 
  2.     <text class="steps"
  3.         当前步数:{{currentSteps}} 
  4.     </text> 
  5.     <canvas class="canvas" ref="canvas" ></canvas> 
  6.     <input type="button" value="重新开始" class="bit" /> 
  7. </div>​ 

 2。在index.css编写刚才添加组件的样式,首先编写container的样式,flex-direction为容器主轴方向,选择column(垂直方向从上到下),justify-content为容器当前行的主轴对齐格式,选择center(项目位于容器的中心),align-items为容器当前行的交叉轴对齐格式,选择center(元素在交叉轴居中),width、height分别为容器以像素为单位的宽度和高度,都设定为450px;编写steps的样式,font-size为设置文本的尺寸,设定为18px,text-align为设置文本的文本对齐方式,选择center(文本居中对齐),width、height分别设定为300px和20px,letter-spacing为设置文本的字符间距,设定为0px,margin-top为设置上外边距,设定为10px;编写canvas的样式,width、height都设定为320px,background-color为设置背景颜色,设定为#BBADA0;编写bit的样式,width、height分别设定为150px和30px,background-color设定为#AD9D8F,font-size设定为24px,margin-top设定为10px。

  1. .container { 
  2.     flex-direction: column
  3.     justify-content: center; 
  4.     align-items: center; 
  5.     width:450px; 
  6.     height:450px; 
  7. .steps { 
  8.     font-size: 18px; 
  9.     text-align:center; 
  10.     width:300px; 
  11.     height:20px; 
  12.     letter-spacing:0px; 
  13.     margin-top:10px; 
  14. .canvas{ 
  15.     width:320px; 
  16.     height:320px; 
  17.     background-color: #BBADA0; 
  18. .bit
  19.     width:150px; 
  20.     height:30px; 
  21.     background-color:#AD9D8F; 
  22.     font-size:24px; 
  23.     margin-top:10px; 
  24. }​ 

 3。在index.js编写描述页面中的组件是如何进行交互的,首先在data函数中为当前步数赋值为0。

  1. data: { 
  2.  
  3. currentSteps: 0, 
  4.  
  5. }​ 

 在文件开头定义一个全局变量量context,创建一个onReady()函数,用于定义2d绘画工具 。

  1. var context; 
  2.  
  3. onReady(){ 
  4.  
  5. context=this.$refs.canvas.getContext('2d'); 
  6.  

 用0表示白色,1代表黑色,这样我们就能定义一个用0和1表示键,颜色表示值的字典COLORS,并且定义全局常量边长SIDELEN为40,间距MARGIN为5,定义一个全局变量的二维数组grids,其中的值全为0。

  1. var grids=[[0, 0, 0, 0, 0, 0, 0], 
  2.  
  3. [0, 0, 0, 0, 0, 0, 0], 
  4.  
  5. [0, 0, 0, 0, 0, 0, 0], 
  6.  
  7. [0, 0, 0, 0, 0, 0, 0], 
  8.  
  9. [0, 0, 0, 0, 0, 0, 0], 
  10.  
  11. [0, 0, 0, 0, 0, 0, 0], 
  12.  
  13. [0, 0, 0, 0, 0, 0, 0], 
  14.  
  15. [0, 0, 0, 0, 0, 0, 0]]; 
  16.  
  17. const SIDELEN=40; 
  18.  
  19. const MARGIN=5; 
  20.  
  21. const COLORS = { 
  22.  
  23. "0""#FFFFFF"
  24.  
  25. "1""#000000" 
  26.  

 创建drawGrids()函数,先将grids的值利用toString()函数全部转化为字符串,fillStyle表社画图的背景颜色,引用字典即可,fillRect表示画矩形的大小,其中有四个参数,第一个参数指定矩形左上角的x坐标,第二参数指定矩形左上角的y坐标,第三个参数指定矩形的高度,第四个参数指定矩形的宽度,最后创建onShow()调用drawGrids()函数即可。

  1. onShow(){ 
  2.  
  3. this.drawGrids(); 
  4.  
  5. }, 
  6.  
  7. drawGrids(){ 
  8.  
  9. for (let row = 0 ;row < 7 ;row++){ 
  10.  
  11. for (let column = 0; column < 7;column++){ 
  12.  
  13. let gridStr = grids[row][column].toString(); 
  14.  
  15. context.fillStyle = COLORS[gridStr]; 
  16.  
  17. let leftTopX = column * (MARGIN + SIDELEN) + MARGIN; 
  18.  
  19. let leftTopY = row * (MARGIN + SIDELEN) + MARGIN; 
  20.  
  21. context.fillRect(leftTopX, leftTopY, SIDELEN, SIDELEN); 
  22.  
  23.  
  24.  

 运行即可得出开始界面布局了。

实现题目的随机生成和色块的翻转

其次我们要先在运动手表上随机生成一个色块被打乱的7*7的棋盘,并且点击棋盘中任一色块,其上下左右四个色块也会跟着一起变色(在边缘的色块则只会改变其中若干个色块的颜色),棋盘上方的当前步数则会相应依次增加,如图所示:


1。为了使点击任意一个色块时能得到其对应的二维数组的下标,我们需要给每个色块添加一个按钮button,并增加一个点击事件click,分别给这些按钮设定一个类名和点击按钮所调用的函数然后为了使按钮显示在棋盘格子的上方,需要添加一个栈stack类名设定位stack,使画布先进栈,按钮后进栈,这样就能达到预期效果了,index.hml代码如下:

  1. <div class="container" > 
  2.     <text class="steps"
  3.         当前步数:{{currentSteps}} 
  4.     </text> 
  5.     <stack class="stack"
  6.         <canvas class="canvas" ref="canvas" ></canvas> 
  7.  
  8.         <input type="button" class="bitgrid1"  onclick="getgrid1"/> 
  9.         <input type="button" class="bitgrid2"  onclick="getgrid2"/> 
  10.         <input type="button" class="bitgrid3"  onclick="getgrid3"/> 
  11.         <input type="button" class="bitgrid4"  onclick="getgrid4"/> 
  12.         <input type="button" class="bitgrid5"  onclick="getgrid5"/> 
  13.         <input type="button" class="bitgrid6"  onclick="getgrid6"/> 
  14.         <input type="button" class="bitgrid7"  onclick="getgrid7"/> 
  15.         <input type="button" class="bitgrid8"  onclick="getgrid8"/> 
  16.         <input type="button" class="bitgrid9"  onclick="getgrid9"/> 
  17.         <input type="button" class="bitgrid10"  onclick="getgrid10"/> 
  18.         <input type="button" class="bitgrid11"  onclick="getgrid11"/> 
  19.         <input type="button" class="bitgrid12"  onclick="getgrid12"/> 
  20.         <input type="button" class="bitgrid13"  onclick="getgrid13"/> 
  21.         <input type="button" class="bitgrid14"  onclick="getgrid14"/> 
  22.         <input type="button" class="bitgrid15"  onclick="getgrid15"/> 
  23.         <input type="button" class="bitgrid16"  onclick="getgrid16"/> 
  24.         <input type="button" class="bitgrid17"  onclick="getgrid17"/> 
  25.         <input type="button" class="bitgrid18"  onclick="getgrid18"/> 
  26.         <input type="button" class="bitgrid19"  onclick="getgrid19"/> 
  27.         <input type="button" class="bitgrid20"  onclick="getgrid20"/> 
  28.         <input type="button" class="bitgrid21"  onclick="getgrid21"/> 
  29.         <input type="button" class="bitgrid22"  onclick="getgrid22"/> 
  30.         <input type="button" class="bitgrid23"  onclick="getgrid23"/> 
  31.         <input type="button" class="bitgrid24"  onclick="getgrid24"/> 
  32.         <input type="button" class="bitgrid25"  onclick="getgrid25"/> 
  33.         <input type="button" class="bitgrid26"  onclick="getgrid26"/> 
  34.         <input type="button" class="bitgrid27"  onclick="getgrid27"/> 
  35.         <input type="button" class="bitgrid28"  onclick="getgrid28"/> 
  36.         <input type="button" class="bitgrid29"  onclick="getgrid29"/> 
  37.         <input type="button" class="bitgrid30"  onclick="getgrid30"/> 
  38.         <input type="button" class="bitgrid31"  onclick="getgrid31"/> 
  39.         <input type="button" class="bitgrid32"  onclick="getgrid32"/> 
  40.         <input type="button" class="bitgrid33"  onclick="getgrid33"/> 
  41.         <input type="button" class="bitgrid34"  onclick="getgrid34"/> 
  42.         <input type="button" class="bitgrid35"  onclick="getgrid35"/> 
  43.         <input type="button" class="bitgrid36"  onclick="getgrid36"/> 
  44.         <input type="button" class="bitgrid37"  onclick="getgrid37"/> 
  45.         <input type="button" class="bitgrid38"  onclick="getgrid38"/> 
  46.         <input type="button" class="bitgrid39"  onclick="getgrid39"/> 
  47.         <input type="button" class="bitgrid40"  onclick="getgrid40"/> 
  48.         <input type="button" class="bitgrid41"  onclick="getgrid41"/> 
  49.         <input type="button" class="bitgrid42"  onclick="getgrid42"/> 
  50.         <input type="button" class="bitgrid43"  onclick="getgrid43"/> 
  51.         <input type="button" class="bitgrid44"  onclick="getgrid44"/> 
  52.         <input type="button" class="bitgrid45"  onclick="getgrid45"/> 
  53.         <input type="button" class="bitgrid46"  onclick="getgrid46"/> 
  54.         <input type="button" class="bitgrid47"  onclick="getgrid47"/> 
  55.         <input type="button" class="bitgrid48"  onclick="getgrid48"/> 
  56.         <input type="button" class="bitgrid49"  onclick="getgrid49"/> 
  57.     </stack> 
  58.     <input type="button" value="重新开始" class="bit" /> 
  59. </div> 

2. 编写stack的样式,width、height都设定为320px,margin-top设定为10px;分别编写每个按钮的样式,left为指示距边界框左上角的以像素为单位的水平坐标,top为指示距边界框左上角的以像素为单位的垂直坐标,border-color为设置边框颜色,transparent指透明颜色。

  1. .stack{ 
  2.  
  3. width: 320px; 
  4.  
  5. height: 320px; 
  6.  
  7. margin-top: 10px; 
  8.  
  9.  
  10. .bitgrid1{ 
  11.  
  12. left:5px; 
  13.  
  14. top:5px; 
  15.  
  16. width:40px; 
  17.  
  18. height:40px; 
  19.  
  20. border-color:transparent; 
  21.  
  22. background-color:transparent; 
  23.  
  24.  
  25. .bitgrid2{ 
  26.  
  27. left:50px; 
  28.  
  29. top:5px; 
  30.  
  31. width:40px; 
  32.  
  33. height:40px; 
  34.  
  35. border-color:transparent; 
  36.  
  37. background-color:transparent; 
  38.  
  39.  
  40. .bitgrid3{ 
  41.  
  42. left:95px; 
  43.  
  44. top:5px; 
  45.  
  46. width:40px; 
  47.  
  48. height:40px; 
  49.  
  50. border-color:transparent; 
  51.  
  52. background-color:transparent; 
  53.  
  54.  
  55. .bitgrid4{ 
  56.  
  57. left:140px; 
  58.  
  59. top:5px; 
  60.  
  61. width:40px; 
  62.  
  63. height:40px; 
  64.  
  65. border-color:transparent; 
  66.  
  67. background-color:transparent; 
  68.  
  69.  
  70. .bitgrid5{ 
  71.  
  72. left:185px; 
  73.  
  74. top:5px; 
  75.  
  76. width:40px; 
  77.  
  78. height:40px; 
  79.  
  80. border-color:transparent; 
  81.  
  82. background-color:transparent; 
  83.  
  84.  
  85. .bitgrid6{ 
  86.  
  87. left:230px; 
  88.  
  89. top:5px; 
  90.  
  91. width:40px; 
  92.  
  93. height:40px; 
  94.  
  95. border-color:transparent; 
  96.  
  97. background-color:transparent; 
  98.  
  99.  
  100. .bitgrid7{ 
  101.  
  102. left:275px; 
  103.  
  104. top:5px; 
  105.  
  106. width:40px; 
  107.  
  108. height:40px; 
  109.  
  110. border-color:transparent; 
  111.  
  112. background-color:transparent; 
  113.  
  114.  
  115. .bitgrid8{ 
  116.  
  117. left:5px; 
  118.  
  119. top:50px; 
  120.  
  121. width:40px; 
  122.  
  123. height:40px; 
  124.  
  125. border-color:transparent; 
  126.  
  127. background-color:transparent; 
  128.  
  129.  
  130. .bitgrid9{ 
  131.  
  132. left:50px; 
  133.  
  134. top:50px; 
  135.  
  136. width:40px; 
  137.  
  138. height:40px; 
  139.  
  140. border-color:transparent; 
  141.  
  142. background-color:transparent; 
  143.  
  144.  
  145. .bitgrid10{ 
  146.  
  147. left:95px; 
  148.  
  149. top:50px; 
  150.  
  151. width:40px; 
  152.  
  153. height:40px; 
  154.  
  155. border-color:transparent; 
  156.  
  157. background-color:transparent; 
  158.  
  159.  
  160. .bitgrid11{ 
  161.  
  162. left:140px; 
  163.  
  164. top:50px; 
  165.  
  166. width:40px; 
  167.  
  168. height:40px; 
  169.  
  170. border-color:transparent; 
  171.  
  172. background-color:transparent; 
  173.  
  174.  
  175. .bitgrid12{ 
  176.  
  177. left:185px; 
  178.  
  179. top:50px; 
  180.  
  181. width:40px; 
  182.  
  183. height:40px; 
  184.  
  185. border-color:transparent; 
  186.  
  187. background-color:transparent; 
  188.  
  189.  
  190. .bitgrid13{ 
  191.  
  192. left:230px; 
  193.  
  194. top:50px; 
  195.  
  196. width:40px; 
  197.  
  198. height:40px; 
  199.  
  200. border-color:transparent; 
  201.  
  202. background-color:transparent; 
  203.  
  204.  
  205. .bitgrid14{ 
  206.  
  207. left:275px; 
  208.  
  209. top:50px; 
  210.  
  211. width:40px; 
  212.  
  213. height:40px; 
  214.  
  215. border-color:transparent; 
  216.  
  217. background-color:transparent; 
  218.  
  219.  
  220. .bitgrid15{ 
  221.  
  222. left:5px; 
  223.  
  224. top:95px; 
  225.  
  226. width:40px; 
  227.  
  228. height:40px; 
  229.  
  230. border-color:transparent; 
  231.  
  232. background-color:transparent; 
  233.  
  234.  
  235. .bitgrid16{ 
  236.  
  237. left:50px; 
  238.  
  239. top:95px; 
  240.  
  241. width:40px; 
  242.  
  243. height:40px; 
  244.  
  245. border-color:transparent; 
  246.  
  247. background-color:transparent; 
  248.  
  249.  
  250. .bitgrid17{ 
  251.  
  252. left:95px; 
  253.  
  254. top:95px; 
  255.  
  256. width:40px; 
  257.  
  258. height:40px; 
  259.  
  260. border-color:transparent; 
  261.  
  262. background-color:transparent; 
  263.  
  264.  
  265. .bitgrid18{ 
  266.  
  267. left:140px; 
  268.  
  269. top:95px; 
  270.  
  271. width:40px; 
  272.  
  273. height:40px; 
  274.  
  275. border-color:transparent; 
  276.  
  277. background-color:transparent; 
  278.  
  279.  
  280. .bitgrid19{ 
  281.  
  282. left:185px; 
  283.  
  284. top:95px; 
  285.  
  286. width:40px; 
  287.  
  288. height:40px; 
  289.  
  290. border-color:transparent; 
  291.  
  292. background-color:transparent; 
  293.  
  294.  
  295. .bitgrid20{ 
  296.  
  297. left:230px; 
  298.  
  299. top:95px; 
  300.  
  301. width:40px; 
  302.  
  303. height:40px; 
  304.  
  305. border-color:transparent; 
  306.  
  307. background-color:transparent; 
  308.  
  309.  
  310. .bitgrid21{ 
  311.  
  312. left:275px; 
  313.  
  314. top:95px; 
  315.  
  316. width:40px; 
  317.  
  318. height:40px; 
  319.  
  320. border-color:transparent; 
  321.  
  322. background-color:transparent; 
  323.  
  324.  
  325. .bitgrid22{ 
  326.  
  327. left:5px; 
  328.  
  329. top:140px; 
  330.  
  331. width:40px; 
  332.  
  333. height:40px; 
  334.  
  335. border-color:transparent; 
  336.  
  337. background-color:transparent; 
  338.  
  339.  
  340. .bitgrid23{ 
  341.  
  342. left:50px; 
  343.  
  344. top:140px; 
  345.  
  346. width:40px; 
  347.  
  348. height:40px; 
  349.  
  350. border-color:transparent; 
  351.  
  352. background-color:transparent; 
  353.  
  354.  
  355. .bitgrid24{ 
  356.  
  357. left:95px; 
  358.  
  359. top:140px; 
  360.  
  361. width:40px; 
  362.  
  363. height:40px; 
  364.  
  365. border-color:transparent; 
  366.  
  367. background-color:transparent; 
  368.  
  369.  
  370. .bitgrid25{ 
  371.  
  372. left:140px; 
  373.  
  374. top:140px; 
  375.  
  376. width:40px; 
  377.  
  378. height:40px; 
  379.  
  380. border-color:transparent; 
  381.  
  382. background-color:transparent; 
  383.  
  384.  
  385. .bitgrid26{ 
  386.  
  387. left:185px; 
  388.  
  389. top:140px; 
  390.  
  391. width:40px; 
  392.  
  393. height:40px; 
  394.  
  395. border-color:transparent; 
  396.  
  397. background-color:transparent; 
  398.  
  399.  
  400. .bitgrid27{ 
  401.  
  402. left:230px; 
  403.  
  404. top:140px; 
  405.  
  406. width:40px; 
  407.  
  408. height:40px; 
  409.  
  410. border-color:transparent; 
  411.  
  412. background-color:transparent; 
  413.  
  414.  
  415. .bitgrid28{ 
  416.  
  417. left:275px; 
  418.  
  419. top:140px; 
  420.  
  421. width:40px; 
  422.  
  423. height:40px; 
  424.  
  425. border-color:transparent; 
  426.  
  427. background-color:transparent; 
  428.  
  429.  
  430. .bitgrid29{ 
  431.  
  432. left:5px; 
  433.  
  434. top:185px; 
  435.  
  436. width:40px; 
  437.  
  438. height:40px; 
  439.  
  440. border-color:transparent; 
  441.  
  442. background-color:transparent; 
  443.  
  444.  
  445. .bitgrid30{ 
  446.  
  447. left:50px; 
  448.  
  449. top:185px; 
  450.  
  451. width:40px; 
  452.  
  453. height:40px; 
  454.  
  455. border-color:transparent; 
  456.  
  457. background-color:transparent; 
  458.  
  459.  
  460. .bitgrid31{ 
  461.  
  462. left:95px; 
  463.  
  464. top:185px; 
  465.  
  466. width:40px; 
  467.  
  468. height:40px; 
  469.  
  470. border-color:transparent; 
  471.  
  472. background-color:transparent; 
  473.  
  474.  
  475. .bitgrid32{ 
  476.  
  477. left:140px; 
  478.  
  479. top:185px; 
  480.  
  481. width:40px; 
  482.  
  483. height:40px; 
  484.  
  485. border-color:transparent; 
  486.  
  487. background-color:transparent; 
  488.  
  489.  
  490. .bitgrid33{ 
  491.  
  492. left:185px; 
  493.  
  494. top:185px; 
  495.  
  496. width:40px; 
  497.  
  498. height:40px; 
  499.  
  500. border-color:transparent; 
  501.  
  502. background-color:transparent; 
  503.  
  504.  
  505. .bitgrid34{ 
  506.  
  507. left:230px; 
  508.  
  509. top:185px; 
  510.  
  511. width:40px; 
  512.  
  513. height:40px; 
  514.  
  515. border-color:transparent; 
  516.  
  517. background-color:transparent; 
  518.  
  519.  
  520. .bitgrid35{ 
  521.  
  522. left:275px; 
  523.  
  524. top:185px; 
  525.  
  526. width:40px; 
  527.  
  528. height:40px; 
  529.  
  530. border-color:transparent; 
  531.  
  532. background-color:transparent; 
  533.  
  534.  
  535. .bitgrid36{ 
  536.  
  537. left:5px; 
  538.  
  539. top:230px; 
  540.  
  541. width:40px; 
  542.  
  543. height:40px; 
  544.  
  545. border-color:transparent; 
  546.  
  547. background-color:transparent; 
  548.  
  549.  
  550. .bitgrid37{ 
  551.  
  552. left:50px; 
  553.  
  554. top:230px; 
  555.  
  556. width:40px; 
  557.  
  558. height:40px; 
  559.  
  560. border-color:transparent; 
  561.  
  562. background-color:transparent; 
  563.  
  564.  
  565. .bitgrid38{ 
  566.  
  567. left:95px; 
  568.  
  569. top:230px; 
  570.  
  571. width:40px; 
  572.  
  573. height:40px; 
  574.  
  575. border-color:transparent; 
  576.  
  577. background-color:transparent; 
  578.  
  579.  
  580. .bitgrid39{ 
  581.  
  582. left:140px; 
  583.  
  584. top:230px; 
  585.  
  586. width:40px; 
  587.  
  588. height:40px; 
  589.  
  590. border-color:transparent; 
  591.  
  592. background-color:transparent; 
  593.  
  594.  
  595. .bitgrid40{ 
  596.  
  597. left:185px; 
  598.  
  599. top:230px; 
  600.  
  601. width:40px; 
  602.  
  603. height:40px; 
  604.  
  605. border-color:transparent; 
  606.  
  607. background-color:transparent; 
  608.  
  609.  
  610. .bitgrid41{ 
  611.  
  612. left:230px; 
  613.  
  614. top:230px; 
  615.  
  616. width:40px; 
  617.  
  618. height:40px; 
  619.  
  620. border-color:transparent; 
  621.  
  622. background-color:transparent; 
  623.  
  624.  
  625. .bitgrid42{ 
  626.  
  627. left:275px; 
  628.  
  629. top:230px; 
  630.  
  631. width:40px; 
  632.  
  633. height:40px; 
  634.  
  635. border-color:transparent; 
  636.  
  637. background-color:transparent; 
  638.  
  639.  
  640. .bitgrid43{ 
  641.  
  642. left:5px; 
  643.  
  644. top:275px; 
  645.  
  646. width:40px; 
  647.  
  648. height:40px; 
  649.  
  650. border-color:transparent; 
  651.  
  652. background-color:transparent; 
  653.  
  654.  
  655. .bitgrid44{ 
  656.  
  657. left:50px; 
  658.  
  659. top:275px; 
  660.  
  661. width:40px; 
  662.  
  663. height:40px; 
  664.  
  665. border-color:transparent; 
  666.  
  667. background-color:transparent; 
  668.  
  669.  
  670. .bitgrid45{ 
  671.  
  672. left:95px; 
  673.  
  674. top:275px; 
  675.  
  676. width:40px; 
  677.  
  678. height:40px; 
  679.  
  680. border-color:transparent; 
  681.  
  682. background-color:transparent; 
  683.  
  684.  
  685. .bitgrid46{ 
  686.  
  687. left:140px; 
  688.  
  689. top:275px; 
  690.  
  691. width:40px; 
  692.  
  693. height:40px; 
  694.  
  695. border-color:transparent; 
  696.  
  697. background-color:transparent; 
  698.  
  699.  
  700. .bitgrid47{ 
  701.  
  702. left:185px; 
  703.  
  704. top:275px; 
  705.  
  706. width:40px; 
  707.  
  708. height:40px; 
  709.  
  710. border-color:transparent; 
  711.  
  712. background-color:transparent; 
  713.  
  714.  
  715. .bitgrid48{ 
  716.  
  717. left:230px; 
  718.  
  719. top:275px; 
  720.  
  721. width:40px; 
  722.  
  723. height:40px; 
  724.  
  725. border-color:transparent; 
  726.  
  727. background-color:transparent; 
  728.  
  729.  
  730. .bitgrid49{ 
  731.  
  732. left:275px; 
  733.  
  734. top:275px; 
  735.  
  736. width:40px; 
  737.  
  738. height:40px; 
  739.  
  740. border-color:transparent; 
  741.  
  742. background-color:transparent; 
  743.  

 3. 先增加一个函数change(x,y),接受二维数组的下标,用来改变二维数字的值,当isShow为false才使二维数组的值发生变化,这样就能达到当游戏成功时再点击色块时颜色不再发生变化,如果值为0 则改为1,如果值为1则改为0,这样对应的色块颜色也能跟着发生变化。

  1. change(x,y){ 
  2.  
  3. if(this.isShow==false){ 
  4.  
  5. if(grids[x][y] == 0){ 
  6.  
  7. grids[x][y] = 1; 
  8.  
  9. }else
  10.  
  11. grids[x][y] = 0; 
  12.  
  13.  
  14.  

 之后增加一个函数changeOneGrids(x,y),接受二维数组的下标,调用刚才编写的函数change(x,y),改变其上下左右四个色块对应的二维数组的值,并且调用函数drawGrids()重新画图实现颜色的变化,当isShow为false才使currentSteps加1,这样就能达到当游戏成功时再点击色块时currentSteps不再发生变化。

  1. changeOneGrids(x,y){ 
  2.  
  3. if(x>-1 && y>-1 && x<7 && y<7){ 
  4.  
  5. this.change(x,y); 
  6.  
  7.  
  8. if(x+1>-1 && y>-1 && x+1<7 && y<7){ 
  9.  
  10. this.change(x+1,y); 
  11.  
  12.  
  13. if(x-1>-1 && y>-1 && x-1<7 && y<7){ 
  14.  
  15. this.change(x-1,y); 
  16.  
  17.  
  18. if(x>-1 && y+1>-1 && x<7 && y+1<7){ 
  19.  
  20. this.change(x,y+1); 
  21.  
  22.  
  23. if(x>-1 && y-1>-1 && x<7 && y-1<7){ 
  24.  
  25. this.change(x,y-1); 
  26.  
  27.  
  28. this.drawGrids(); 
  29.  
  30. if(this.isShow==false){ 
  31.  
  32. this.currentSteps+=1;; 
  33.  
  34.  

 再编写49个按钮对应的49个函数,作用是读取当前二维数组位置的下标,并且调用函数changeOneGrids(x,y)。

  1. getgrid1(){ 
  2.  
  3. this.changeOneGrids(0,0); 
  4.  
  5. }, 
  6.  
  7. getgrid2(){ 
  8.  
  9. this.changeOneGrids(0,1); 
  10.  
  11. }, 
  12.  
  13. getgrid3(){ 
  14.  
  15. this.changeOneGrids(0,2); 
  16.  
  17. }, 
  18.  
  19. getgrid4(){ 
  20.  
  21. this.changeOneGrids(0,3); 
  22.  
  23. }, 
  24.  
  25. getgrid5(){ 
  26.  
  27. this.changeOneGrids(0,4); 
  28.  
  29. }, 
  30.  
  31. getgrid6(){ 
  32.  
  33. this.changeOneGrids(0,5); 
  34.  
  35. }, 
  36.  
  37. getgrid7(){ 
  38.  
  39. this.changeOneGrids(0,6); 
  40.  
  41. }, 
  42.  
  43. getgrid8(){ 
  44.  
  45. this.changeOneGrids(1,0); 
  46.  
  47. }, 
  48.  
  49. getgrid9(){ 
  50.  
  51. this.changeOneGrids(1,1); 
  52.  
  53. }, 
  54.  
  55. getgrid10(){ 
  56.  
  57. this.changeOneGrids(1,2); 
  58.  
  59. }, 
  60.  
  61. getgrid11(){ 
  62.  
  63. this.changeOneGrids(1,3); 
  64.  
  65. }, 
  66.  
  67. getgrid12(){ 
  68.  
  69. this.changeOneGrids(1,4); 
  70.  
  71. }, 
  72.  
  73. getgrid13(){ 
  74.  
  75. this.changeOneGrids(1,5); 
  76.  
  77. }, 
  78.  
  79. getgrid14(){ 
  80.  
  81. this.changeOneGrids(1,6); 
  82.  
  83. }, 
  84.  
  85. getgrid15(){ 
  86.  
  87. this.changeOneGrids(2,0); 
  88.  
  89. }, 
  90.  
  91. getgrid16(){ 
  92.  
  93. this.changeOneGrids(2,1); 
  94.  
  95. }, 
  96.  
  97. getgrid17(){ 
  98.  
  99. this.changeOneGrids(2,2); 
  100.  
  101. }, 
  102.  
  103. getgrid18(){ 
  104.  
  105. this.changeOneGrids(2,3); 
  106.  
  107. }, 
  108.  
  109. getgrid19(){ 
  110.  
  111. this.changeOneGrids(2,4); 
  112.  
  113. }, 
  114.  
  115. getgrid20(){ 
  116.  
  117. this.changeOneGrids(2,5); 
  118.  
  119. }, 
  120.  
  121. getgrid21(){ 
  122.  
  123. this.changeOneGrids(2,6); 
  124.  
  125. }, 
  126.  
  127. getgrid22(){ 
  128.  
  129. this.changeOneGrids(3,0); 
  130.  
  131. }, 
  132.  
  133. getgrid23(){ 
  134.  
  135. this.changeOneGrids(3,1); 
  136.  
  137. }, 
  138.  
  139. getgrid24(){ 
  140.  
  141. this.changeOneGrids(3,2); 
  142.  
  143. }, 
  144.  
  145. getgrid25(){ 
  146.  
  147. this.changeOneGrids(3,3); 
  148.  
  149. }, 
  150.  
  151. getgrid26(){ 
  152.  
  153. this.changeOneGrids(3,4); 
  154.  
  155. }, 
  156.  
  157. getgrid27(){ 
  158.  
  159. this.changeOneGrids(3,5); 
  160.  
  161. }, 
  162.  
  163. getgrid28(){ 
  164.  
  165. this.changeOneGrids(3,6); 
  166.  
  167. }, 
  168.  
  169. getgrid29(){ 
  170.  
  171. this.changeOneGrids(4,0); 
  172.  
  173. }, 
  174.  
  175. getgrid30(){ 
  176.  
  177. this.changeOneGrids(4,1); 
  178.  
  179. }, 
  180.  
  181. getgrid31(){ 
  182.  
  183. this.changeOneGrids(4,2); 
  184.  
  185. }, 
  186.  
  187. getgrid32(){ 
  188.  
  189. this.changeOneGrids(4,3); 
  190.  
  191. }, 
  192.  
  193. getgrid33(){ 
  194.  
  195. this.changeOneGrids(4,4); 
  196.  
  197. }, 
  198.  
  199. getgrid34(){ 
  200.  
  201. this.changeOneGrids(4,5); 
  202.  
  203. }, 
  204.  
  205. getgrid35(){ 
  206.  
  207. this.changeOneGrids(4,6); 
  208.  
  209. }, 
  210.  
  211. getgrid36(){ 
  212.  
  213. this.changeOneGrids(5,0); 
  214.  
  215. }, 
  216.  
  217. getgrid37(){ 
  218.  
  219. this.changeOneGrids(5,1); 
  220.  
  221. }, 
  222.  
  223. getgrid38(){ 
  224.  
  225. this.changeOneGrids(5,2); 
  226.  
  227. }, 
  228.  
  229. getgrid39(){ 
  230.  
  231. this.changeOneGrids(5,3); 
  232.  
  233. }, 
  234.  
  235. getgrid40(){ 
  236.  
  237. this.changeOneGrids(5,4); 
  238.  
  239. }, 
  240.  
  241. getgrid41(){ 
  242.  
  243. this.changeOneGrids(5,5); 
  244.  
  245. }, 
  246.  
  247. getgrid42(){ 
  248.  
  249. this.changeOneGrids(5,6); 
  250.  
  251. }, 
  252.  
  253. getgrid43(){ 
  254.  
  255. this.changeOneGrids(6,0); 
  256.  
  257. }, 
  258.  
  259. getgrid44(){ 
  260.  
  261. this.changeOneGrids(6,1); 
  262.  
  263. }, 
  264.  
  265. getgrid45(){ 
  266.  
  267. this.changeOneGrids(6,2); 
  268.  
  269. }, 
  270.  
  271. getgrid46(){ 
  272.  
  273. this.changeOneGrids(6,3); 
  274.  
  275. }, 
  276.  
  277. getgrid47(){ 
  278.  
  279. this.changeOneGrids(6,4); 
  280.  
  281. }, 
  282.  
  283. getgrid48(){ 
  284.  
  285. this.changeOneGrids(6,5); 
  286.  
  287. }, 
  288.  
  289. getgrid49(){ 
  290.  
  291. this.changeOneGrids(6,6); 
  292.  

 最后是随机生成一个色块被打乱的7 7的棋盘,首先我们得先把二维数组的下标放进一个列表中,Math.random()函数是随机[0,1)内的小数,Math.floor(x)为得出小于或等于x的最大整数,每次随机生成一个数后,读取刚才的列表对应的下标,调用函数changeOneGrids(x,y),重复此操作若干次,这样就能随机生成一个色块被打乱的7*7的棋盘。

  1. initGrids(){ 
  2.  
  3. let array = []; 
  4.  
  5. for (let row = 0; row < 7; row++) { 
  6.  
  7. for (let column = 0; column < 7; column++) { 
  8.  
  9. if (grids[row][column] == 0) { 
  10.  
  11. array.push([row, column]) 
  12.  
  13.  
  14.  
  15.  
  16. for (let i = 0; i < 20; i++){ 
  17.  
  18. let randomIndex = Math.floor(Math.random() * array.length); 
  19.  
  20. let row = array[randomIndex][0]; 
  21.  
  22. let column = array[randomIndex][1]; 
  23.  
  24. this.changeOneGrids(row,column); 
  25.  
  26.  

 至此,这一部分内容已经全部编写完毕。

实现游戏结束页面

最后我们要实现当色块的颜色全部为白色时弹出游戏成功界面,并使按钮“重新开始”能够重新随机生成随机生成一个色块被打乱的7*7的棋盘,当前步数置为0,如图所示:


1. 首先我们得在栈stack组件中增加一个游戏成功的容器div类名为subcontainer,以isShow控制该容器是否进栈,增加文本组件text,类名gameover,并赋值“游戏成功”,再为重新开始按钮增加一个点击事件click,所调用的函数为restartGame。

  1. <div class="subcontainer" show="{{isShow}}"
  2.             <text class="gameover"
  3.                 游戏成功 
  4.             </text> 
  5.         </div> 
  6.  
  7. <input type="button" value="重新开始" class="bit" onclick="restartGame"/>​ 

2. 编写刚才增加组件的样式,编写subcontainer的样式和gameover的样式

  1. .subcontainer { 
  2.  
  3. left:50px; 
  4.  
  5. top:95px; 
  6.  
  7. width: 220px; 
  8.  
  9. height: 130px; 
  10.  
  11. justify-content: center; 
  12.  
  13. align-items: center; 
  14.  
  15. background-color: #E9C2A6; 
  16.  
  17.  
  18. .gameover { 
  19.  
  20. font-size: 38px; 
  21.  
  22. color: black; 
  23.  

 3. 首先给isShow赋值false,将开头的全局变量grids赋值删除,增加一个函数给grids赋值,并调用函数initGrids()

  1. var grids; 
  2.  
  3. data: { 
  4.  
  5. currentSteps: 0, 
  6.  
  7. isShow: false 
  8.  
  9.  
  10. onInit() { 
  11.  
  12. grids=[[0, 0, 0, 0, 0, 0, 0], 
  13.  
  14. [0, 0, 0, 0, 0, 0, 0], 
  15.  
  16. [0, 0, 0, 0, 0, 0, 0], 
  17.  
  18. [0, 0, 0, 0, 0, 0, 0], 
  19.  
  20. [0, 0, 0, 0, 0, 0, 0], 
  21.  
  22. [0, 0, 0, 0, 0, 0, 0], 
  23.  
  24. [0, 0, 0, 0, 0, 0, 0], 
  25.  
  26. [0, 0, 0, 0, 0, 0, 0]]; 
  27.  
  28. this.initGrids(); 
  29.  

 增加一个函数gameover(),当棋盘所有色块颜色为白色时返回true,否则返回false,即当二维数组的值全为0时返回true,否则返回false

  1. gameover(){ 
  2.  
  3. for (let row = 0 ;row < 7 ;row++){ 
  4.  
  5. for (let column = 0; column < 7;column++){ 
  6.  
  7. if (grids[row][column]==1){ 
  8.  
  9. return false
  10.  
  11.  
  12.  
  13.  
  14. return true
  15.  

 在函数changeOneGrids(x,y)中添加当函数gameover()为true时,将isShow赋值为true,使游戏成功界面显示在最上方,当isShow为false时,步数增加1

  1. changeOneGrids(x,y){ 
  2.  
  3. if(x>-1 && y>-1 && x<7 && y<7){ 
  4.  
  5. this.change(x,y); 
  6.  
  7.  
  8. if(x+1>-1 && y>-1 && x+1<7 && y<7){ 
  9.  
  10. this.change(x+1,y); 
  11.  
  12.  
  13. if(x-1>-1 && y>-1 && x-1<7 && y<7){ 
  14.  
  15. this.change(x-1,y); 
  16.  
  17.  
  18. if(x>-1 && y+1>-1 && x<7 && y+1<7){ 
  19.  
  20. this.change(x,y+1); 
  21.  
  22.  
  23. if(x>-1 && y-1>-1 && x<7 && y-1<7){ 
  24.  
  25. this.change(x,y-1); 
  26.  
  27.  
  28. this.drawGrids(); 
  29.  
  30. if(this.isShow==false){ 
  31.  
  32. this.currentSteps+=1;; 
  33.  
  34.  
  35. if(this.gameover()){ 
  36.  
  37. this.isShow=true
  38.  
  39.  

 最后编写重新按钮对应的函数restartGame(),作用是是二维数组、isShow和当前步数全部置为初始化界面

  1. restartGame(){ 
  2.  
  3. this.onInit(); 
  4.  
  5. this.drawGrids(); 
  6.  
  7. this.isShow = false
  8.  
  9. this.currentSteps = 0; 
  10.  

 至此,整个demo全部完成了。

结语

以上就是黑白翻棋小游戏代码的主要编写思路以及代码,源码将放在附件中,欢迎大家下载,感兴趣的读者可以自行跟着编写学习,相信你们也能够完成的。如果有遇到什么问题,或者查找出其中的错误之处,或者能够优化代码。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-12-11 12:27:35

鸿蒙HarmonyOS

2020-12-22 11:20:36

鸿蒙HarmonyOS游戏

2020-12-23 09:58:37

HarmonyOSHAPAPP

2021-09-17 14:47:33

鸿蒙HarmonyOS应用

2020-12-31 12:02:15

鸿蒙Hi3861环境搭建

2020-12-18 11:05:25

鸿蒙HarmonyOS游戏

2021-10-08 14:45:22

鸿蒙HarmonyOS应用

2022-08-22 17:28:34

ArkUI鸿蒙

2020-12-14 11:44:29

开发js智能手表wearablewea

2022-04-14 11:35:01

HarmonyOS手表Demo操作系统

2021-01-12 12:16:55

鸿蒙HarmonyOS游戏

2014-11-05 16:37:54

2022-04-13 11:49:37

HarmonyOS操作系统鸿蒙

2022-11-01 15:17:48

JS鸿蒙小游戏

2015-01-14 10:18:51

智能手表可穿戴

2020-09-30 12:58:03

鸿蒙
点赞
收藏

51CTO技术栈公众号