自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

2020征文-手機(jī)深鴻會(huì)深大小組:鴻蒙HarmonyOS手機(jī)游戲之?dāng)?shù)字華容道

系統(tǒng) OpenHarmony
12月16號(hào)HarmonyOS2.0手機(jī)開發(fā)者Beta版已經(jīng)發(fā)布了,作為“1+8+N”戰(zhàn)略的重要入口和生態(tài)核心,怎么能少得了手機(jī)應(yīng)用開發(fā)呢,今天將由深鴻會(huì)深大學(xué)習(xí)小組從零基礎(chǔ)開發(fā)第一個(gè)HarmonyOS手機(jī)小游戲——數(shù)字華容道(界面略丑陋,大佬別噴)。

前言

12月16號(hào)HarmonyOS2.0手機(jī)開發(fā)者Beta版已經(jīng)發(fā)布了,作為“1+8+N”戰(zhàn)略的重要入口和生態(tài)核心,怎么能少得了手機(jī)應(yīng)用開發(fā)呢,今天將由深鴻會(huì)深大學(xué)習(xí)小組從零基礎(chǔ)開發(fā)第一個(gè)HarmonyOS手機(jī)小游戲——數(shù)字華容道(界面略丑陋,大佬別噴),此前已經(jīng)在運(yùn)動(dòng)手表上成功開發(fā)了:HarmonyOS運(yùn)動(dòng)手表游戲合并、HarmonyOS手表游戲——數(shù)字華容道,同樣是深鴻會(huì)深大小組學(xué)習(xí)完HarmonyOS后自行開發(fā)的一個(gè)鴻蒙demo,詳細(xì)講述了數(shù)字華容道在鴻蒙手機(jī)上開發(fā)思路。深鴻會(huì)深大學(xué)習(xí)小組是一群熱衷于學(xué)習(xí)鴻蒙相關(guān)知識(shí)和開發(fā)鴻蒙相關(guān)應(yīng)用的開發(fā)者們,我們的學(xué)習(xí)項(xiàng)目為:荔園Harmony、Awesome-HarmonyOS_木棉花,同時(shí)也歡迎與各位感興趣的讀者一起學(xué)習(xí)HarmonyOS開發(fā),相互交流、共同進(jìn)步。

概述

本個(gè)demo將從零基礎(chǔ)開始完成鴻蒙小游戲APP在手機(jī)上的編譯在項(xiàng)目中我們所使用到的軟件為DevEco Studio,下載地址為:DevEco Studio下載、DevEco Studio安裝教程,在項(xiàng)目中我們要實(shí)現(xiàn)的內(nèi)容為數(shù)字華容道APP的開發(fā)。

1. 打開引用首先為數(shù)字華容道的初始界面,點(diǎn)擊開始游戲即會(huì)切換到數(shù)字華容道的游戲界面。


2. 進(jìn)入數(shù)字華容道的游戲界面顯示4*4的方陣,方陣中分布有隨意打亂的1至15的數(shù)字和一個(gè)空白方格,方陣下方顯示一個(gè)“重新開始”的按鈕和一個(gè)“返回”按鈕,點(diǎn)擊“重新開始”按鈕即會(huì)重新生成隨意打亂的1至15的數(shù)字和一個(gè)空白方格的方陣,點(diǎn)擊“返回”按鈕即會(huì)切換到數(shù)字華容道的初始界面,最下方有四個(gè)指示不同方向箭頭的按鈕,點(diǎn)擊任一按鈕或向上、下、左、右任一方向滑動(dòng),空白方格周圍對(duì)應(yīng)位置的方格便會(huì)隨之向?qū)?yīng)的方向移動(dòng)一格。

 

3. 經(jīng)過若干次滑動(dòng)或點(diǎn)擊后,當(dāng)所有的數(shù)字按順序排列后,則會(huì)彈出游戲成功的界面,再滑動(dòng)或點(diǎn)擊也不會(huì)有任何變化。 


正文

創(chuàng)建項(xiàng)目

DevEco Studio下載安裝成功后,打開DevEco Studio,點(diǎn)擊左上角的File,點(diǎn)擊New,再選擇New Project,選擇Phone選項(xiàng),選擇默認(rèn)的模板(java版),然后選擇保存路徑,將文件命名為MyPhoneApplication(文件名不能出現(xiàn)中文或者特殊字符,否則將無法成功創(chuàng)建項(xiàng)目文件),最后點(diǎn)擊Finish。



實(shí)現(xiàn)初始界面布局

首先,我們要先實(shí)現(xiàn)數(shù)字華容道的初始界面,點(diǎn)擊開始游戲即會(huì)切換到另一個(gè)空白的界面。


1. 先在entry>src>main>config.json文件中最下方"launchType": "standard"的后面添加以下代碼,并且將上方的“label”:“MyPhoneApplication”修改成"label": "數(shù)字華容道",這樣就實(shí)現(xiàn)去掉應(yīng)用上方的標(biāo)題欄和將應(yīng)用名稱改為數(shù)字華容道了

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""數(shù)字華容道"
  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. 先將我們事先準(zhǔn)備好的圖片復(fù)制粘貼到entry>src>main>resources>base>media文件夾中(ctrl+c、ctrl+v復(fù)制粘貼),并且命名為game,點(diǎn)擊OK。


在entry>src>main>resources>base>layout>ability_main.xml中添加布局,先將事先存在的Text組件刪去,添加Image圖片組件,引入我們剛才復(fù)制粘貼的圖片,再添加一個(gè)Button按鈕組件,加入唯一標(biāo)識(shí)符id并配置好其他相應(yīng)的屬性。

  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增加一個(gè)空白的類以用來后面編寫數(shù)字華容道的游戲界面,并且命名為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函數(shù)中添加一個(gè)按鈕指向我們(2)中添加的按鈕,按鈕添加一個(gè)響應(yīng)點(diǎn)擊事件的函數(shù),用parsent函數(shù)跳轉(zhuǎn)到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.  

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

實(shí)現(xiàn)數(shù)字的隨機(jī)打亂

然后我們要在數(shù)字華容道的游戲界面生成隨意打亂的1至15的數(shù)字和一個(gè)空白方格的方陣。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice編寫代碼。

先定義個(gè)一個(gè)位置布局layout和一個(gè)二維數(shù)組grids,創(chuàng)建函數(shù)initializeinitialize()分別對(duì)其初始化,在onStart函數(shù)中調(diào)用函數(shù)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.  

 然后定義函數(shù)drawGrids(int[][] grids)用于繪制4*4方陣和其二維數(shù)組對(duì)應(yīng)的數(shù)字。

  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.  

 再定義函數(shù)changeGrids(int[][] grids,int direction),每次接收一個(gè)方向,2表示上移,-1表示左移,1表示右移,-2表示下移,找出空白方格所在位置對(duì)應(yīng)的二維數(shù)組下標(biāo),對(duì)應(yīng)的方格和空白方格對(duì)應(yīng)的二維數(shù)組的數(shù)值對(duì)調(diào)。

  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.  

 定義函數(shù)createGrids(int[][] grids)用于隨機(jī)生成一個(gè)表示方向的數(shù)字,循環(huán)調(diào)用函數(shù)changeGrids(grids,direction)用于隨機(jī)打亂二維數(shù)組對(duì)應(yīng)的數(shù)字。

  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()函數(shù)中調(diào)用createGrids(grids)函數(shù)和drawGrids(grids)函數(shù)。

  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.  

 至此,這一部分完成了。

實(shí)現(xiàn)滑動(dòng)或點(diǎn)擊調(diào)換數(shù)字

添加“重新開始”和“返回”按鈕,在最下方添加四個(gè)指示不同方向箭頭的按鈕,點(diǎn)擊任一按鈕或向上、下、左、右任一方向滑動(dòng),空白方格周圍對(duì)應(yīng)位置的方格便會(huì)隨之向?qū)?yīng)的方向移動(dòng)一格。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice編寫代碼。

先定義一個(gè)函數(shù)drawButton()用于繪制所有的按鈕,四個(gè)指示不同方向箭頭的按鈕分別添加四個(gè)響應(yīng)點(diǎn)擊事件的函數(shù),分別調(diào)用對(duì)應(yīng)的changeGrids(grids,direction)函數(shù)實(shí)現(xiàn)空白方格周圍對(duì)應(yīng)位置的方格便會(huì)隨之向?qū)?yīng)的方向移動(dòng)一格,并調(diào)用drawGrids(grids)函數(shù)用于繪制新的方陣。

  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.  

 然后添加一個(gè)函數(shù)slideGrids()為布局layout添加一個(gè)滑動(dòng)事件,并獲取滑動(dòng)開始與結(jié)束的坐標(biāo),并計(jì)算出大致的滑動(dòng)方向,分別調(diào)用對(duì)應(yīng)的changeGrids(grids,direction)函數(shù)實(shí)現(xiàn)空白方格周圍對(duì)應(yīng)位置的方格便會(huì)隨之向?qū)?yīng)的方向移動(dòng)一格,并調(diào)用drawGrids(grids)函數(shù)用于繪制新的方陣,并在開頭添加相應(yīng)的變量。

  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()函數(shù)中調(diào)用slideGrids()函數(shù)和drawButton()函數(shù)。

  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.  

 至此,這一部分完成了。

實(shí)現(xiàn)游戲成功界面

點(diǎn)擊“重新開始”按鈕即會(huì)重新生成隨意打亂的1至15的數(shù)字和一個(gè)空白方格的方陣,點(diǎn)擊“返回”按鈕即會(huì)切換到數(shù)字華容道的初始界面,經(jīng)過若干次滑動(dòng)或點(diǎn)擊后,當(dāng)所有的數(shù)字按順序排列后,則會(huì)彈出游戲成功的界面,再滑動(dòng)或點(diǎn)擊也不會(huì)有任何變化。


在entry>src>main>java>com.example.myphoneapplication>slice>SecondAbilitySlice編寫代碼。

首先定義一個(gè)函數(shù)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.  

 然后定義一個(gè)函數(shù)gameover()用于判斷二維數(shù)組的數(shù)字是否按順序排列,當(dāng)二維數(shù)組的數(shù)字按順序排列時(shí)返回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()函數(shù)中重新開始按鈕中添加一個(gè)響應(yīng)點(diǎn)擊事件的函數(shù),用于調(diào)用函數(shù)initialize()實(shí)現(xiàn)重新生成隨意打亂的1至15的數(shù)字和一個(gè)空白方格的方陣,返回按鈕中添加一個(gè)響應(yīng)點(diǎn)擊事件的函數(shù),用parsen函數(shù)返回?cái)?shù)字華容道的初始界面,四個(gè)指示不同方向箭頭的按鈕的響應(yīng)點(diǎn)擊事件的函數(shù)中增加一個(gè)判斷,當(dāng)函數(shù)gameover()返回為false時(shí)才調(diào)用各自的changeGrids(grids,direction)函數(shù),最后增加一個(gè)判斷,當(dāng)函數(shù)gameover()返回為true時(shí)調(diào)用函數(shù)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.  

 在函數(shù)slideGrids()函數(shù)中增加一個(gè)判斷,當(dāng)函數(shù)gameover()返回為false時(shí)才調(diào)用changeGrids(grids,direction)函數(shù),最后增加一個(gè)判斷,當(dāng)函數(shù)gameover()返回為true時(shí)調(diào)用函數(shù)drawText()。

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

 至此,整個(gè)demo全部完成了。

結(jié)語

以上就是數(shù)字華容道小游戲在手機(jī)的主要編寫思路以及代碼,源碼將放在附件中,歡迎大家下載,感興趣的讀者可以自行跟著編寫學(xué)習(xí),相信你們也能夠完成的。更多深鴻會(huì)深大小組學(xué)習(xí)項(xiàng)目可以查看荔園Harmony,如果有遇到什么問題,或者查找出其中的錯(cuò)誤之處,或者能夠優(yōu)化代碼和界面,也歡迎各位在評(píng)論區(qū)留言討論,讓我們一起學(xué)習(xí)進(jìn)步!

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

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

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2020-12-11 12:27:35

鴻蒙HarmonyOS

2020-12-14 09:58:28

鴻蒙HarmonyOS手表游戲

2020-12-23 09:58:37

HarmonyOSHAPAPP

2020-12-31 12:02:15

鴻蒙Hi3861環(huán)境搭建

2021-11-02 14:55:42

鴻蒙HarmonyOS應(yīng)用

2021-08-25 09:54:51

鴻蒙HarmonyOS應(yīng)用

2021-10-22 19:41:01

鴻蒙HarmonyOS應(yīng)用

2021-01-12 12:16:55

鴻蒙HarmonyOS游戲

2020-12-28 10:15:18

鴻蒙HarmonyOSListContain

2021-10-09 14:49:50

鴻蒙HarmonyOS應(yīng)用

2020-12-22 11:09:20

鴻蒙Feature AbiAbilitySlic

2012-11-04 14:54:24

2024-09-27 08:51:58

2020-12-22 09:48:18

鴻蒙HarmonyOS應(yīng)用程序

2020-12-23 11:24:34

鴻蒙開發(fā)IDE安裝

2013-10-15 09:48:03

C++Lambda函數(shù)式編程

2020-12-18 11:05:25

鴻蒙HarmonyOS游戲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)