zl程序教程

您现在的位置是:首页 >  APP

当前栏目

2020征文-手机深鸿会深大小组:鸿蒙HarmonyOS手机游戏之数字华容道

2023-04-18 15:47:33 时间

前言

12月16号HarmonyOS2.0手机开发者Beta版已经发布了,作为“1+8+N”战略的重要入口和生态核心,怎么能少得了手机应用开发呢,今天将由深鸿会深大学习小组从零基础开发第一个HarmonyOS手机小游戏——数字华容道(界面略丑陋,大佬别喷),此前已经在运动手表上成功开发了:HarmonyOS运动手表游戏合并、HarmonyOS手表游戏——数字华容道,同样是深鸿会深大小组学习完HarmonyOS后自行开发的一个鸿蒙demo,详细讲述了数字华容道在鸿蒙手机上开发思路。深鸿会深大学习小组是一群热衷于学习鸿蒙相关知识和开发鸿蒙相关应用的开发者们,我们的学习项目为:荔园Harmony、Awesome-HarmonyOS_木棉花,同时也欢迎与各位感兴趣的读者一起学习HarmonyOS开发,相互交流、共同进步。

概述

本个demo将从零基础开始完成鸿蒙小游戏APP在手机上的编译在项目中我们所使用到的软件为DevEco Studio,下载地址为:DevEco Studio下载、DevEco Studio安装教程,在项目中我们要实现的内容为数字华容道APP的开发。

1. 打开引用首先为数字华容道的初始界面,点击开始游戏即会切换到数字华容道的游戏界面。


2. 进入数字华容道的游戏界面显示4*4的方阵,方阵中分布有随意打乱的1至15的数字和一个空白方格,方阵下方显示一个“重新开始”的按钮和一个“返回”按钮,点击“重新开始”按钮即会重新生成随意打乱的1至15的数字和一个空白方格的方阵,点击“返回”按钮即会切换到数字华容道的初始界面,最下方有四个指示不同方向箭头的按钮,点击任一按钮或向上、下、左、右任一方向滑动,空白方格周围对应位置的方格便会随之向对应的方向移动一格。

 

3. 经过若干次滑动或点击后,当所有的数字按顺序排列后,则会弹出游戏成功的界面,再滑动或点击也不会有任何变化。 


正文

创建项目

DevEco Studio下载安装成功后,打开DevEco Studio,点击左上角的File,点击New,再选择New Project,选择Phone选项,选择默认的模板(java版),然后选择保存路径,将文件命名为MyPhoneApplication(文件名不能出现中文或者特殊字符,否则将无法成功创建项目文件),最后点击Finish。



实现初始界面布局

首先,我们要先实现数字华容道的初始界面,点击开始游戏即会切换到另一个空白的界面。


1. 先在entry>src>main>config.json文件中最下方"launchType": "standard"的后面添加以下代码,并且将上方的“label”:“MyPhoneApplication”修改成"label": "数字华容道",这样就实现去掉应用上方的标题栏和将应用名称改为数字华容道了

config.json最下面部分代码:

  1. "orientation""unspecified"
  2.  
  3. "name""com.example.myphoneapplication.MainAbility"
  4.  
  5. "icon""$media:icon"
  6.  
  7. "description""$string:mainability_description"
  8.  
  9. "label""数字华容道"
  10.  
  11. "type""page"
  12.  
  13. "launchType""standard"
  14.  
  15. "metaData": { 
  16.  
  17. "customizeData": [ 
  18.  
  19.  
  20. "name""hwc-theme"
  21.  
  22. "value""androidhwext:style/Theme.Emui.Light.NoTitleBar"
  23.  
  24. "extra""" 
  25.  
  26.  
  27.  

 2. 先将我们事先准备好的图片复制粘贴到entry>src>main>resources>base>media文件夹中(ctrl+c、ctrl+v复制粘贴),并且命名为game,点击OK。


在entry>src>main>resources>base>layout>ability_main.xml中添加布局,先将事先存在的Text组件删去,添加Image图片组件,引入我们刚才复制粘贴的图片,再添加一个Button按钮组件,加入唯一标识符id并配置好其他相应的属性。

  1. xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  2.  
  3. ohos:height="match_parent" 
  4.  
  5. ohos:width="match_parent" 
  6.  
  7. ohos:orientation="vertical"
  8.  
  9.  
  10. ohos:height="match_parent" 
  11.  
  12. ohos:width="match_parent" 
  13.  
  14. ohos:image_src="$media:game" 
  15.  
  16. ohos:layout_alignment="center" 
  17.  
  18. /> 
  19.  
  20.  
  21. ohos:id="$+id:button_game" 
  22.  
  23. ohos:height="150" 
  24.  
  25. ohos:width="515" 
  26.  
  27. ohos:text_alignment="center" 
  28.  
  29. ohos:top_margin="-810" 
  30.  
  31. ohos:left_margin="250" 
  32.  
  33. /> 

 3. 在entry>src>main>java>com.example.myphoneapplication>slice中右键选择New>Java Class增加一个空白的类以用来后面编写数字华容道的游戏界面,并且命名为SecondAbilitySlice。


将entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice中的代码修改成如下:

  1. package com.example.myphoneapplication.slice; 
  2.  
  3. import com.example.myphoneapplication.ResourceTable; 
  4.  
  5. import ohos.aafwk.ability.AbilitySlice; 
  6.  
  7. import ohos.aafwk.content.Intent; 
  8.  
  9. public class SecondAbilitySlice extends AbilitySlice { 
  10.  
  11. public void onStart(Intent intent) { 
  12.  
  13. super.onStart(intent); 
  14.  
  15.  
  16. @Override 
  17.  
  18. public void onActive() { 
  19.  
  20. super.onActive(); 
  21.  
  22.  
  23. @Override 
  24.  
  25. public void onForeground(Intent intent) { 
  26.  
  27. super.onForeground(intent); 
  28.  
  29.  

 4. 在entry>src>main>java>com.example.myphoneapplication>slice>MainAbilitySlice中的onStart函数中添加一个按钮指向我们(2)中添加的按钮,按钮添加一个响应点击事件的函数,用parsent函数跳转到SecondAbilitySlice。

  1. package com.example.myphoneapplication.slice; 
  2.  
  3. import com.example.myphoneapplication.ResourceTable; 
  4.  
  5. import ohos.aafwk.content.Intent; 
  6.  
  7. import ohos.agp.components.Button; 
  8.  
  9. import ohos.agp.components.Component; 
  10.  
  11. public class MainAbilitySlice extends ohos.aafwk.ability.AbilitySlice { 
  12.  
  13. @Override 
  14.  
  15. public void onStart(Intent intent) { 
  16.  
  17. super.onStart(intent); 
  18.  
  19. super.setUIContent(ResourceTable.Layout_ability_main); 
  20.  
  21. Button button = (Button) findComponentById(ResourceTable.Id_button_game); 
  22.  
  23. button.setClickedListener(new Component.ClickedListener() { 
  24.  
  25. @Override 
  26.  
  27. public void onClick(Component component) { 
  28.  
  29. present(new SecondAbilitySlice(),new Intent()); 
  30.  
  31.  
  32. }); 
  33.  
  34.  
  35. @Override 
  36.  
  37. public void onActive() { 
  38.  
  39. super.onActive(); 
  40.  
  41.  
  42. @Override 
  43.  
  44. public void onForeground(Intent intent) { 
  45.  
  46. super.onForeground(intent); 
  47.  
  48.  

 至此,这一部分就完成了。

实现数字的随机打乱

然后我们要在数字华容道的游戏界面生成随意打乱的1至15的数字和一个空白方格的方阵。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice编写代码。

先定义个一个位置布局layout和一个二维数组grids,创建函数initializeinitialize()分别对其初始化,在onStart函数中调用函数initializeinitialize()。

  1. private float starX, starY, distanceX, distanceY; 
  2.  
  3. private DirectionalLayout layout; 
  4.  
  5. private int[][] grids; 
  6.  
  7. public void onStart(Intent intent) { 
  8.  
  9. super.onStart(intent); 
  10.  
  11. initialize(); 
  12.  
  13.  
  14. public void initialize(){ 
  15.  
  16. layout = new DirectionalLayout(this); 
  17.  
  18. grids = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}}; 
  19.  

 然后定义函数drawGrids(int[][] grids)用于绘制4*4方阵和其二维数组对应的数字。

  1. public void drawGrids(int[][] grids){ 
  2.  
  3. layout.setLayoutConfig((new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT))); 
  4.  
  5. Component.DrawTask task=new Component.DrawTask() { 
  6.  
  7. public void onDraw(Component component, Canvas canvas) { 
  8.  
  9. Paint mPaint = new Paint(); 
  10.  
  11. mPaint.setColor(Color.GRAY); 
  12.  
  13. RectFloat rect=new RectFloat(2,230,1078,1306); 
  14.  
  15. canvas.drawRect(rect,mPaint); 
  16.  
  17. for(int row = 0; row < 4; row++){ 
  18.  
  19. for(int column = 0; column < 4; column++){ 
  20.  
  21. mPaint.setColor(Color.CYAN); 
  22.  
  23. RectFloat rectFloat=new RectFloat(22+column*262,250+row*262,272+column*262,500+row*262); 
  24.  
  25. canvas.drawRect(rectFloat,mPaint); 
  26.  
  27. mPaint.setColor(Color.YELLOW); 
  28.  
  29. mPaint.setTextSize(125); 
  30.  
  31. if(grids[row][column]!=0){ 
  32.  
  33. if(grids[row][column]<10){ 
  34.  
  35. canvas.drawText(mPaint, String.valueOf(grids[row][column]),105+column*262,425+row*262); 
  36.  
  37.  
  38. else
  39.  
  40. canvas.drawText(mPaint, String.valueOf(grids[row][column]),65+column*262,425+row*262); 
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. }; 
  48.  
  49. layout.addDrawTask(task); 
  50.  
  51. setUIContent(layout); 
  52.  

 再定义函数changeGrids(int[][] grids,int direction),每次接收一个方向,2表示上移,-1表示左移,1表示右移,-2表示下移,找出空白方格所在位置对应的二维数组下标,对应的方格和空白方格对应的二维数组的数值对调。

  1. public void changeGrids(int[][] grids,int direction){ 
  2.  
  3. int row_0 = 3; 
  4.  
  5. int column_0 = 3; 
  6.  
  7. int temp
  8.  
  9. for(int row = 0; row < 4; row++) { 
  10.  
  11. for (int column = 0; column < 4; column++) { 
  12.  
  13. if(grids[row][column] == 0){ 
  14.  
  15. row_0 = row; 
  16.  
  17. column_0 = column
  18.  
  19.  
  20.  
  21.  
  22. if(direction == -1 && (column_0 + 1) <= 3){ 
  23.  
  24. temp = grids[row_0][column_0 + 1]; 
  25.  
  26. grids[row_0][column_0 + 1] = grids[row_0][column_0]; 
  27.  
  28. grids[row_0][column_0] = temp
  29.  
  30. }else if (direction == 1 && (column_0 - 1) >= 0) { 
  31.  
  32. temp = grids[row_0][column_0 - 1]; 
  33.  
  34. grids[row_0][column_0 - 1] = grids[row_0][column_0]; 
  35.  
  36. grids[row_0][column_0] = temp
  37.  
  38. else if (direction == 2 && (row_0 + 1) <= 3) { 
  39.  
  40. temp = grids[row_0 + 1][column_0]; 
  41.  
  42. grids[row_0 + 1][column_0] = grids[row_0][column_0]; 
  43.  
  44. grids[row_0][column_0] = temp
  45.  
  46. else if (direction == -2 && (row_0 - 1) >= 0) { 
  47.  
  48. temp = grids[row_0 - 1][column_0]; 
  49.  
  50. grids[row_0 - 1][column_0] = grids[row_0][column_0]; 
  51.  
  52. grids[row_0][column_0] = temp
  53.  
  54.  

 定义函数createGrids(int[][] grids)用于随机生成一个表示方向的数字,循环调用函数changeGrids(grids,direction)用于随机打乱二维数组对应的数字。

  1. public void createGrids(int[][] grids){ 
  2.  
  3. int[] array = {-1,-2,1,2}; 
  4.  
  5. for(int i = 0; i < 100; i++){ 
  6.  
  7. int random = (int)Math.floor(Math.random()*4); 
  8.  
  9. int direction = array[random]; 
  10.  
  11. changeGrids(grids,direction); 
  12.  
  13.  

 最后在initialize()函数中调用createGrids(grids)函数和drawGrids(grids)函数。

  1. public void initialize(){ 
  2.  
  3. layout = new DirectionalLayout(this); 
  4.  
  5. grids = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}}; 
  6.  
  7. createGrids(grids); 
  8.  
  9. drawGrids(grids); 
  10.  

 至此,这一部分完成了。

实现滑动或点击调换数字

添加“重新开始”和“返回”按钮,在最下方添加四个指示不同方向箭头的按钮,点击任一按钮或向上、下、左、右任一方向滑动,空白方格周围对应位置的方格便会随之向对应的方向移动一格。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice编写代码。

先定义一个函数drawButton()用于绘制所有的按钮,四个指示不同方向箭头的按钮分别添加四个响应点击事件的函数,分别调用对应的changeGrids(grids,direction)函数实现空白方格周围对应位置的方格便会随之向对应的方向移动一格,并调用drawGrids(grids)函数用于绘制新的方阵。

  1. public void drawButton(){ 
  2.  
  3. Button button=new Button(this); 
  4.  
  5. button.setText("重新开始"); 
  6.  
  7. button.setTextSize(100); 
  8.  
  9. button.setTextAlignment(TextAlignment.CENTER); 
  10.  
  11. button.setTextColor(Color.WHITE); 
  12.  
  13. button.setMarginTop(1400); 
  14.  
  15. button.setMarginLeft(80); 
  16.  
  17. button.setPadding(20,20,20,20); 
  18.  
  19. ShapeElement background = new ShapeElement(); 
  20.  
  21. background.setRgbColor(new RgbColor(174, 158, 143)); 
  22.  
  23. background.setCornerRadius(100); 
  24.  
  25. button.setBackground(background); 
  26.  
  27. layout.addComponent(button); 
  28.  
  29. Button button0=new Button(this); 
  30.  
  31. button0.setText("返回"); 
  32.  
  33. button0.setTextSize(100); 
  34.  
  35. button0.setTextAlignment(TextAlignment.CENTER); 
  36.  
  37. button0.setTextColor(Color.WHITE); 
  38.  
  39. button0.setMarginTop(-170); 
  40.  
  41. button0.setMarginLeft(680); 
  42.  
  43. button0.setPadding(20,20,20,20); 
  44.  
  45. button0.setBackground(background); 
  46.  
  47. layout.addComponent(button0); 
  48.  
  49. ShapeElement background0 = new ShapeElement(); 
  50.  
  51. background0.setRgbColor(new RgbColor(174, 158, 143)); 
  52.  
  53. background0.setCornerRadius(100); 
  54.  
  55. Button button1=new Button(this); 
  56.  
  57. button1.setText("↑"); 
  58.  
  59. button1.setTextAlignment(TextAlignment.CENTER); 
  60.  
  61. button1.setTextColor(Color.WHITE); 
  62.  
  63. button1.setTextSize(100); 
  64.  
  65. button1.setMarginLeft(500); 
  66.  
  67. button1.setMarginTop(70); 
  68.  
  69. button1.setPadding(10,0,10,0); 
  70.  
  71. button1.setBackground(background0); 
  72.  
  73. button1.setClickedListener(new Component.ClickedListener() { 
  74.  
  75. @Override 
  76.  
  77. public void onClick(Component component) { 
  78.  
  79. changeGrids(grids,2); 
  80.  
  81.  
  82. }); 
  83.  
  84. layout.addComponent(button1); 
  85.  
  86. Button button2=new Button(this); 
  87.  
  88. button2.setText("←"); 
  89.  
  90. button2.setTextAlignment(TextAlignment.CENTER); 
  91.  
  92. button2.setTextColor(Color.WHITE); 
  93.  
  94. button2.setTextSize(100); 
  95.  
  96. button2.setMarginTop(10); 
  97.  
  98. button2.setMarginLeft(400); 
  99.  
  100. button2.setPadding(10,0,10,0); 
  101.  
  102. button2.setBackground(background0); 
  103.  
  104. button2.setClickedListener(new Component.ClickedListener() { 
  105.  
  106. @Override 
  107.  
  108. public void onClick(Component component) { 
  109.  
  110. changeGrids(grids,-1); 
  111.  
  112.  
  113. }); 
  114.  
  115. layout.addComponent(button2); 
  116.  
  117. Button button3=new Button(this); 
  118.  
  119. button3.setText("→"); 
  120.  
  121. button3.setTextAlignment(TextAlignment.CENTER); 
  122.  
  123. button3.setTextColor(Color.WHITE); 
  124.  
  125. button3.setTextSize(100); 
  126.  
  127. button3.setMarginLeft(600); 
  128.  
  129. button3.setMarginTop(-130); 
  130.  
  131. button3.setPadding(10,0,10,0); 
  132.  
  133. button3.setBackground(background0); 
  134.  
  135. button3.setClickedListener(new Component.ClickedListener() { 
  136.  
  137. @Override 
  138.  
  139. public void onClick(Component component) { 
  140.  
  141. changeGrids(grids,1); 
  142.  
  143.  
  144. }); 
  145.  
  146. layout.addComponent(button3); 
  147.  
  148. Button button4=new Button(this); 
  149.  
  150. button4.setText("↓"); 
  151.  
  152. button4.setTextAlignment(TextAlignment.CENTER); 
  153.  
  154. button4.setTextColor(Color.WHITE); 
  155.  
  156. button4.setTextSize(100); 
  157.  
  158. button4.setMarginLeft(500); 
  159.  
  160. button4.setMarginTop(10); 
  161.  
  162. button4.setPadding(10,0,10,0); 
  163.  
  164. button4.setBackground(background0); 
  165.  
  166. button4.setClickedListener(new Component.ClickedListener() { 
  167.  
  168. @Override 
  169.  
  170. public void onClick(Component component) { 
  171.  
  172. changeGrids(grids,-2); 
  173.  
  174.  
  175. }); 
  176.  
  177. layout.addComponent(button4); 
  178.  
  179. drawGrids(grids); 
  180.  

 然后添加一个函数slideGrids()为布局layout添加一个滑动事件,并获取滑动开始与结束的坐标,并计算出大致的滑动方向,分别调用对应的changeGrids(grids,direction)函数实现空白方格周围对应位置的方格便会随之向对应的方向移动一格,并调用drawGrids(grids)函数用于绘制新的方阵,并在开头添加相应的变量。

  1. private float starX, starY, distanceX, distanceY; 
  2.  
  3. public void slideGrids(){ 
  4.  
  5. layout.setTouchEventListener(new Component.TouchEventListener() { 
  6.  
  7. @Override 
  8.  
  9. public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  10.  
  11. MmiPoint point = touchEvent.getPointerScreenPosition(0); 
  12.  
  13. switch (touchEvent.getAction()) { 
  14.  
  15. case TouchEvent.PRIMARY_POINT_DOWN: 
  16.  
  17. starX = point.getX(); 
  18.  
  19. starY = point.getY(); 
  20.  
  21. break; 
  22.  
  23. case TouchEvent.PRIMARY_POINT_UP: 
  24.  
  25. distanceX = point.getX() - starX; 
  26.  
  27. distanceY = point.getY() - starY; 
  28.  
  29. break; 
  30.  
  31.  
  32. if (gameover() == false){ 
  33.  
  34. if (Math.abs(distanceX) > Math.abs(distanceY)) { 
  35.  
  36. if (distanceX > 200) { 
  37.  
  38. changeGrids(grids,1); 
  39.  
  40. else if (distanceX < -200) { 
  41.  
  42. changeGrids(grids,-1); 
  43.  
  44.  
  45. else if (Math.abs(distanceX) < Math.abs(distanceY)){ 
  46.  
  47. if (distanceY > 200) { 
  48.  
  49. changeGrids(grids,-2); 
  50.  
  51. else if (distanceY < -200) { 
  52.  
  53. changeGrids(grids,2); 
  54.  
  55.  
  56.  
  57.  
  58. drawGrids(grids); 
  59.  
  60. return false
  61.  
  62.  
  63. }); 
  64.  

 最后在initialize()函数中调用slideGrids()函数和drawButton()函数。

  1. public void initialize(){ 
  2.  
  3. layout = new DirectionalLayout(this); 
  4.  
  5. grids = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}}; 
  6.  
  7. createGrids(grids); 
  8.  
  9. slideGrids(); 
  10.  
  11. drawButton(); 
  12.  
  13. drawGrids(grids); 
  14.  

 至此,这一部分完成了。

实现游戏成功界面

点击“重新开始”按钮即会重新生成随意打乱的1至15的数字和一个空白方格的方阵,点击“返回”按钮即会切换到数字华容道的初始界面,经过若干次滑动或点击后,当所有的数字按顺序排列后,则会弹出游戏成功的界面,再滑动或点击也不会有任何变化。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice编写代码。

首先定义一个函数drawText()用于绘制游戏成功字样。

  1. public void drawText(){ 
  2.  
  3. Text text=new Text(this); 
  4.  
  5. text.setText("游戏成功"); 
  6.  
  7. text.setTextSize(100); 
  8.  
  9. text.setTextColor(Color.BLUE); 
  10.  
  11. text.setTextAlignment(TextAlignment.CENTER); 
  12.  
  13. text.setMarginsTopAndBottom(-2000,0); 
  14.  
  15. text.setMarginsLeftAndRight(350,0); 
  16.  
  17. layout.addComponent(text); 
  18.  
  19. setUIContent(layout); 
  20.  

 然后定义一个函数gameover()用于判断二维数组的数字是否按顺序排列,当二维数组的数字按顺序排列时返回true,否则返回false。

  1. public boolean gameover() { 
  2.  
  3. int[][] gameoverGrids = {{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}}; 
  4.  
  5. for (int row = 0; row < 4; row++) { 
  6.  
  7. for (int column = 0; column < 4; column++) { 
  8.  
  9. if (grids[row][column] != gameoverGrids[row][column]) { 
  10.  
  11. return false
  12.  
  13.  
  14.  
  15.  
  16. return true
  17.  

 再在drawButton()函数中重新开始按钮中添加一个响应点击事件的函数,用于调用函数initialize()实现重新生成随意打乱的1至15的数字和一个空白方格的方阵,返回按钮中添加一个响应点击事件的函数,用parsen函数返回数字华容道的初始界面,四个指示不同方向箭头的按钮的响应点击事件的函数中增加一个判断,当函数gameover()返回为false时才调用各自的changeGrids(grids,direction)函数,最后增加一个判断,当函数gameover()返回为true时调用函数drawText()。

  1. public void drawButton(){//部分代码没有贴出,可自行下载源代码查看 
  2.  
  3. button.setClickedListener(new Component.ClickedListener() { 
  4.  
  5. @Override 
  6.  
  7. public void onClick(Component component) { 
  8.  
  9. initialize(); 
  10.  
  11.  
  12. }); 
  13.  
  14. button0.setClickedListener(new Component.ClickedListener() { 
  15.  
  16. @Override 
  17.  
  18. public void onClick(Component component) { 
  19.  
  20. present(new SecondAbilitySlice(),new Intent()); 
  21.  
  22.  
  23. }); 
  24.  
  25. button1.setClickedListener(new Component.ClickedListener() { 
  26.  
  27. @Override 
  28.  
  29. public void onClick(Component component) { 
  30.  
  31. if (gameover() == false){ 
  32.  
  33. changeGrids(grids,2); 
  34.  
  35.  
  36.  
  37. }); 
  38.  
  39. button2.setClickedListener(new Component.ClickedListener() { 
  40.  
  41. @Override 
  42.  
  43. public void onClick(Component component) { 
  44.  
  45. if (gameover() == false){ 
  46.  
  47. changeGrids(grids,-1); 
  48.  
  49.  
  50.  
  51. }); 
  52.  
  53. button3.setClickedListener(new Component.ClickedListener() { 
  54.  
  55. @Override 
  56.  
  57. public void onClick(Component component) { 
  58.  
  59. if (gameover() == false){ 
  60.  
  61. changeGrids(grids,1); 
  62.  
  63.  
  64.  
  65. }); 
  66.  
  67. button4.setClickedListener(new Component.ClickedListener() { 
  68.  
  69. @Override 
  70.  
  71. public void onClick(Component component) { 
  72.  
  73. if (gameover() == false){ 
  74.  
  75. changeGrids(grids,-2); 
  76.  
  77.  
  78.  
  79. }); 
  80.  
  81. if(gameover()){ 
  82.  
  83. drawText(); 
  84.  
  85.  

 在函数slideGrids()函数中增加一个判断,当函数gameover()返回为false时才调用changeGrids(grids,direction)函数,最后增加一个判断,当函数gameover()返回为true时调用函数drawText()。

  1. public void slideGrids(){//部分代码没有贴出,可自行下载源代码查看 
  2.  
  3. if (gameover() == false){ 
  4.  
  5. //{...} 
  6.  
  7.  
  8. if(gameover()){ 
  9.  
  10. drawText(); 
  11.  
  12.  

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

结语

以上就是数字华容道小游戏在手机的主要编写思路以及代码,源码将放在附件中,欢迎大家下载,感兴趣的读者可以自行跟着编写学习,相信你们也能够完成的。更多深鸿会深大小组学习项目可以查看荔园Harmony,如果有遇到什么问题,或者查找出其中的错误之处,或者能够优化代码和界面,也欢迎各位在评论区留言讨论,让我们一起学习进步!

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任

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

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

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