基于HarmonyOS ArkUI 3.0 框架的木棉花扫雷(下)

系统 OpenHarmony
基于HarmonyOS ArkUI 3.0 框架的木棉花扫雷(上)已经给大家分享了木棉花扫雷的欢迎页面和主页面的实战开发,这里继续给大家分享游戏页面的实战开发。

[[437926]]

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

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

https://harmonyos.51cto.co

前言

基于HarmonyOS ArkUI 3.0 框架的木棉花扫雷(上)已经给大家分享了木棉花扫雷的欢迎页面和主页面的实战开发,这里继续给大家分享游戏页面的实战开发O(∩_∩)O~

效果图

【木棉花】基于HarmonyOS ArkUI 3.0 框架的木棉花扫雷(下)-鸿蒙HarmonyOS技术社区

代码文件结构

【木棉花】基于HarmonyOS ArkUI 3.0 框架的木棉花扫雷(下)-鸿蒙HarmonyOS技术社区

正文

1. 添加顶部功能栏

定义状态变量time并初始化为0,用于记录当前的时间值。定义状态变量mine,调用router.getParams().difficulty来获取到页面跳转来时携带的difficulty对应的数据,即地雷总数,其中同样需要导入router模块。

定义全局变量timeoutID,添加一个名为initialize()的函数通过setInterval()对timeoutID进行初始化为定时器,用于逐秒对time进行递增。在自定义组件的生命周期函数aboutToAppear()中调用函数initialize()。

在菜单按钮组件的点击事件中清除定时器并通过router.back()返回到主页面。

game.ets:

  1. import router from '@system.router' 
  2.  
  3. var timeoutID; 
  4.  
  5. @Entry 
  6. @Component 
  7. struct Game { 
  8.   @State time: number = 0 
  9.   @State mine: number = router.getParams().difficulty 
  10.  
  11.   aboutToAppear(){ 
  12.     this.initialize() 
  13.   } 
  14.  
  15.   initialize(){ 
  16.     timeoutID = setInterval(() =>{ 
  17.       this.time += 1 
  18.     }, 1000); 
  19.   } 
  20.  
  21.   build() { 
  22.     Column(){ 
  23.       Row() { 
  24.         Button('菜 单', { type: ButtonType.Normal, stateEffect: true }) 
  25.           .width(95) 
  26.           .height(50) 
  27.           .borderRadius(8) 
  28.           .borderColor('#6379A8'
  29.           .borderWidth(2) 
  30.           .fontSize(26) 
  31.           .fontWeight(700) 
  32.           .fontColor('#1E2B46'
  33.           .backgroundColor('#C1D0E6'
  34.           .margin({ left:10, top: 2 }) 
  35.           .onClick(() => { 
  36.             clearInterval(timeoutID) 
  37.             router.back() 
  38.           }) 
  39.         setImage({ str: 'time.png' }) 
  40.         setText({ num: $time }) 
  41.         setImage({ str: 'lei.png' }) 
  42.         setText({ num: $mine }) 
  43.       } 
  44.       .width('100%'
  45.       .height(60) 
  46.       .backgroundColor('#C1D0E6'
  47.       .margin({ top: 80 }) 
  48.  
  49.     } 
  50.     .width('100%'
  51.     .height('100%'
  52.     .backgroundColor('#D6DDE7'
  53.   } 
  54.  
  55. @Component 
  56. struct setImage { 
  57.   private str: string 
  58.  
  59.   build() { 
  60.     Image($rawfile(this.str)) 
  61.       .height(50) 
  62.       .width(50) 
  63.       .scale({ x: 0.9, y: 0.9 }) 
  64.       .margin({ left:10, top: 3 }) 
  65.   } 
  66.  
  67. @Component 
  68. struct setText { 
  69.   @Link num: number 
  70.  
  71.   build() { 
  72.     Text(this.num.toString()) 
  73.       .width(60) 
  74.       .height(30) 
  75.       .borderRadius(10) 
  76.       .borderColor('#4162AA'
  77.       .borderWidth(1) 
  78.       .fontSize(26) 
  79.       .textAlign(TextAlign.Center) 
  80.       .fontWeight(700) 
  81.       .fontColor('#FFFFFF'
  82.       .backgroundColor('#4162AA'
  83.       .margin({ left:3, top: 5 }) 
  84.       .padding(0) 
  85.   } 

2. 实现扫雷主体

定义变量row和column,调用router.getParams().Number_row和router.getParams().Number_column来获取到页面跳转来时携带的数据,即网格行数和网格列数。定义状态变量Number_row和Number_column并初始化为1~9的字符数组。定义状态变量statesGrids并初始化为16x16全为0的二维数组,用于记录网格中对应位置格子的状态。定义状态变量isCountDown并初始化为true,用于刷新页面组件状态。

在自定义组件的生命周期函数aboutToAppear()中根据row和column的值对Number_row和Number_column进行更新。

在函数initialize()中随机将地雷放置到网格中,即对应位置的grids置为-1,其余非-1的格子对应的位置放置该格子周围的地雷数量。

添加一个名为estimatemine(i, j)的函数用于翻开该格子周围非地雷的格子。

在容器Grid中通过两个循环渲染ForEach对网格进行绘制,在每一个格子中,若对应位置的statesGrids为1,则表示翻开状态,若对应位置的statesGrids为0,则表示没翻开状态。在翻开状态中,若对应位置的grids为-1,则表示是地雷,否则表示该格子周围地雷的数量。在每个格子的点击事件中,若若对应位置的statesGrids为0,则置为1,若对应位置的grids为0,则调用函数estimatemine(i, j)。最后对isCountDown进行取反,用于刷新页面组件状态。

game.ets:

  1. import router from '@system.router' 
  2.  
  3. const colors={ 
  4.   "0""#E0E4F0"
  5.   "1""#2348A0"
  6.   "2""#247411"
  7.   "3""#AF121B"
  8.   "4""#04289E"
  9.   "5""#D2090E"
  10.   "6""#008000"
  11.   "7""#000080"
  12.   "8""#800000"
  13.   "10""#3C7CF6"
  14.   "11""#E0E4F0" 
  15.  
  16. var grids; 
  17. var timeoutID; 
  18.  
  19. @Entry 
  20. @Component 
  21. struct Game { 
  22.   private row: number = router.getParams().Number_row 
  23.   private column: number = router.getParams().Number_column 
  24.   @State time: number = 0 
  25.   @State mine: number = router.getParams().difficulty 
  26.   @State Number_row: string[] = ['1''2''3''4''5''6''7''8''9'
  27.   @State Number_column: string[] = ['1''2''3''4''5''6''7''8''9'
  28.   @State statesGrids: number[][] = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  29.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  30.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  31.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  32.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  33.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  34.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  35.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  36.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  37.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  38.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  39.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  40.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  41.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  42.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  43.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  44.   @State isCountDown: boolean = true 
  45.  
  46.   aboutToAppear(){ 
  47.     if(this.row == 12){ 
  48.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12'
  49.     } else if(this.row == 16){ 
  50.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  51.     } 
  52.  
  53.     if(this.column == 12){ 
  54.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12'
  55.     } else if(this.column == 16){ 
  56.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  57.     } 
  58.  
  59.     this.initialize() 
  60.   } 
  61.  
  62.   initialize(){ 
  63.     grids = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  64.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  65.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  66.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  67.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  68.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  69.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  70.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  71.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  72.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  73.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  74.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  75.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  76.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  77.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  78.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  79.  
  80.     let i = 0; 
  81.     while(i < this.mine){ 
  82.       let random_row = Math.floor(Math.random() * this.row); 
  83.       let random_column = Math.floor(Math.random() * this.column); 
  84.       if(grids[random_row][random_column] == 0){ 
  85.         grids[random_row][random_column] = -1 
  86.         i++ 
  87.       } 
  88.     } 
  89.  
  90.     for(let i = 0; i < this.row; i++){ 
  91.       for(let j = 0; j < this.column; j++){ 
  92.         if(grids[i][j] != -1){ 
  93.           let k = 0 
  94.           for(let ii = i - 1; ii <= i + 1; ii++){ 
  95.             for(let jj = j - 1; jj <= j + 1; jj++){ 
  96.               if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && grids[ii][jj] == -1){ 
  97.                 k++ 
  98.               } 
  99.             } 
  100.           } 
  101.           grids[i][j] = k 
  102.         } 
  103.       } 
  104.     } 
  105.  
  106.     timeoutID = setInterval(() =>{ 
  107.       this.time += 1 
  108.     }, 1000); 
  109.   } 
  110.  
  111.   estimatemine(i, j){ 
  112.     for(let ii = i - 1; ii <= i + 1; ii++){ 
  113.       for(let jj = j - 1; jj <= j + 1; jj++){ 
  114.         if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && (ii != i || jj != j)){ 
  115.           if(this.statesGrids[ii][jj] == 0){ 
  116.             this.statesGrids[ii][jj] = 1 
  117.             if(grids[ii][jj] == 0){ 
  118.               this.estimatemine(ii, jj) 
  119.             } 
  120.           } 
  121.         } 
  122.       } 
  123.     } 
  124.   } 
  125.  
  126.   build() { 
  127.     Column(){ 
  128.       Row() { 
  129.         Button('菜 单', { type: ButtonType.Normal, stateEffect: true }) 
  130.           .width(95) 
  131.           .height(50) 
  132.           .borderRadius(8) 
  133.           .borderColor('#6379A8'
  134.           .borderWidth(2) 
  135.           .fontSize(26) 
  136.           .fontWeight(700) 
  137.           .fontColor('#1E2B46'
  138.           .backgroundColor('#C1D0E6'
  139.           .margin({ left:10, top: 2 }) 
  140.           .onClick(() => { 
  141.             clearInterval(timeoutID) 
  142.             router.back() 
  143.           }) 
  144.         setImage({ str: 'time.png' }) 
  145.         setText({ num: $time }) 
  146.         setImage({ str: 'lei.png' }) 
  147.         setText({ num: $mine }) 
  148.       } 
  149.       .width('100%'
  150.       .height(60) 
  151.       .backgroundColor('#C1D0E6'
  152.       .margin({ top: 80 }) 
  153.  
  154.       Grid() { 
  155.         ForEach(this.Number_row, (day_row: string) => { 
  156.           ForEach(this.Number_column, (day_column: string) => { 
  157.             GridItem() { 
  158.               Button({ type: ButtonType.Normal, stateEffect: true }){ 
  159.                 if (this.isCountDown || !this.isCountDown) { 
  160.                   if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 1){ 
  161.                     if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == -1){ 
  162.                       Image($rawfile('mine.png')) 
  163.                         .height(358 / this.column - 2) 
  164.                         .width(358 / this.column - 2) 
  165.                         .scale({ x: 0.9, y: 0.9 }) 
  166.                     }else
  167.                       Text(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0 ? ' ' : grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()) 
  168.                         .height(358 / this.column - 2) 
  169.                         .width(358 / this.column - 2) 
  170.                         .fontSize(358 / this.column - 4) 
  171.                         .fontColor(colors[grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()]) 
  172.                         .textAlign(TextAlign.Center) 
  173.                         .fontWeight(600) 
  174.                     } 
  175.                   } 
  176.                 } 
  177.               } 
  178.               .height(353 / this.column - 2) 
  179.               .width(353 / this.column - 2) 
  180.               .backgroundColor(colors['1' + this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1]]) 
  181.               .onClick(() => { 
  182.                 if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  183.                   this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 1 
  184.                   if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  185.                     this.estimatemine(parseInt(day_row) - 1, parseInt(day_column) - 1) 
  186.                   } 
  187.                 } 
  188.                 if(this.isCountDown){ 
  189.                   this.isCountDown = false 
  190.                 }else
  191.                   this.isCountDown = true 
  192.                 } 
  193.               }) 
  194.             } 
  195.           }, day_column => day_column) 
  196.         }, day_row => day_row) 
  197.       } 
  198.       .columnsTemplate(this.row == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  199.           this.row == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  200.       .rowsTemplate(this.column == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  201.           this.column == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  202.       .columnsGap(2) 
  203.       .rowsGap(2) 
  204.       .width(355) 
  205.       .backgroundColor('#8CC8F5'
  206.       .height(355) 
  207.       .margin({ top: 10 }) 
  208.     } 
  209.     .width('100%'
  210.     .height('100%'
  211.     .backgroundColor('#D6DDE7'
  212.   } 
  213.  
  214. @Component 
  215. struct setImage { 
  216.   private str: string 
  217.  
  218.   build() { 
  219.     Image($rawfile(this.str)) 
  220.       .height(50) 
  221.       .width(50) 
  222.       .scale({ x: 0.9, y: 0.9 }) 
  223.       .margin({ left:10, top: 3 }) 
  224.   } 
  225.  
  226. @Component 
  227. struct setText { 
  228.   @Link num: number 
  229.  
  230.   build() { 
  231.     Text(this.num.toString()) 
  232.       .width(60) 
  233.       .height(30) 
  234.       .borderRadius(10) 
  235.       .borderColor('#4162AA'
  236.       .borderWidth(1) 
  237.       .fontSize(26) 
  238.       .textAlign(TextAlign.Center) 
  239.       .fontWeight(700) 
  240.       .fontColor('#FFFFFF'
  241.       .backgroundColor('#4162AA'
  242.       .margin({ left:3, top: 5 }) 
  243.       .padding(0) 
  244.   } 

3. 实现插旗功能

定义状态变量statesBtn并初始化为1,用于记录当前响应的是哪个按钮,当为1时表示响应翻开按钮的功能,当为0时表示响应插旗按钮的功能。

在容器Grid的每个按钮中,增加对statesBtn的判断以响应相应的按钮功能,当为插旗功能时,当对应位置的statesGrids为2则表示为旗子,当对应位置的statesGrids为3时则表示“?”。在点击事件中增加对statesBtn的判断以响应相应的按钮功能,当为插旗功能时,实现对应位置的statesGrids在0、2、3之间轮换,即没有翻开状态、旗子状态、问号状态。

game.ets:

  1. import router from '@system.router' 
  2.  
  3. const colors={ 
  4.   "0""#E0E4F0"
  5.   "1""#2348A0"
  6.   "2""#247411"
  7.   "3""#AF121B"
  8.   "4""#04289E"
  9.   "5""#D2090E"
  10.   "6""#008000"
  11.   "7""#000080"
  12.   "8""#800000"
  13.   "10""#3C7CF6"
  14.   "11""#E0E4F0"
  15.   "20""#CBD8E8"
  16.   "21""#4774D8"
  17.   "30""#4774D8"
  18.   "31""#CBD8E8" 
  19.  
  20. var grids; 
  21. var timeoutID; 
  22.  
  23. @Entry 
  24. @Component 
  25. struct Game { 
  26.   private row: number = router.getParams().Number_row 
  27.   private column: number = router.getParams().Number_column 
  28.   @State time: number = 0 
  29.   @State mine: number = router.getParams().difficulty 
  30.   @State Number_row: string[] = ['1''2''3''4''5''6''7''8''9'
  31.   @State Number_column: string[] = ['1''2''3''4''5''6''7''8''9'
  32.   @State statesGrids: number[][] = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  33.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  34.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  35.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  36.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  37.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  38.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  39.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  40.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  41.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  42.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  43.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  44.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  45.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  46.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  47.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  48.   @State isCountDown: boolean = true 
  49.   @State statesBtn: number = 1 
  50.  
  51.   aboutToAppear(){ 
  52.     if(this.row == 12){ 
  53.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12'
  54.     } else if(this.row == 16){ 
  55.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  56.     } 
  57.  
  58.     if(this.column == 12){ 
  59.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12'
  60.     } else if(this.column == 16){ 
  61.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  62.     } 
  63.  
  64.     this.initialize() 
  65.   } 
  66.  
  67.   initialize(){ 
  68.     grids = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  69.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  70.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  71.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  72.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  73.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  74.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  75.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  76.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  77.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  78.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  79.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  80.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  81.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  82.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  83.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  84.  
  85.     let i = 0; 
  86.     while(i < this.mine){ 
  87.       let random_row = Math.floor(Math.random() * this.row); 
  88.       let random_column = Math.floor(Math.random() * this.column); 
  89.       if(grids[random_row][random_column] == 0){ 
  90.         grids[random_row][random_column] = -1 
  91.         i++ 
  92.       } 
  93.     } 
  94.  
  95.     for(let i = 0; i < this.row; i++){ 
  96.       for(let j = 0; j < this.column; j++){ 
  97.         if(grids[i][j] != -1){ 
  98.           let k = 0 
  99.           for(let ii = i - 1; ii <= i + 1; ii++){ 
  100.             for(let jj = j - 1; jj <= j + 1; jj++){ 
  101.               if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && grids[ii][jj] == -1){ 
  102.                 k++ 
  103.               } 
  104.             } 
  105.           } 
  106.           grids[i][j] = k 
  107.         } 
  108.       } 
  109.     } 
  110.  
  111.     timeoutID = setInterval(() =>{ 
  112.       this.time += 1 
  113.     }, 1000); 
  114.   } 
  115.  
  116.   estimatemine(i, j){ 
  117.     for(let ii = i - 1; ii <= i + 1; ii++){ 
  118.       for(let jj = j - 1; jj <= j + 1; jj++){ 
  119.         if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && (ii != i || jj != j)){ 
  120.           if(this.statesGrids[ii][jj] == 0){ 
  121.             this.statesGrids[ii][jj] = 1 
  122.             if(grids[ii][jj] == 0){ 
  123.               this.estimatemine(ii, jj) 
  124.             } 
  125.           } 
  126.         } 
  127.       } 
  128.     } 
  129.   } 
  130.  
  131.   build() { 
  132.     Column(){ 
  133.       Row() { 
  134.         Button('菜 单', { type: ButtonType.Normal, stateEffect: true }) 
  135.           .width(95) 
  136.           .height(50) 
  137.           .borderRadius(8) 
  138.           .borderColor('#6379A8'
  139.           .borderWidth(2) 
  140.           .fontSize(26) 
  141.           .fontWeight(700) 
  142.           .fontColor('#1E2B46'
  143.           .backgroundColor('#C1D0E6'
  144.           .margin({ left:10, top: 2 }) 
  145.           .onClick(() => { 
  146.             clearInterval(timeoutID) 
  147.             router.back() 
  148.           }) 
  149.         setImage({ str: 'time.png' }) 
  150.         setText({ num: $time }) 
  151.         setImage({ str: 'lei.png' }) 
  152.         setText({ num: $mine }) 
  153.       } 
  154.       .width('100%'
  155.       .height(60) 
  156.       .backgroundColor('#C1D0E6'
  157.       .margin({ top: 80 }) 
  158.  
  159.       Grid() { 
  160.         ForEach(this.Number_row, (day_row: string) => { 
  161.           ForEach(this.Number_column, (day_column: string) => { 
  162.             GridItem() { 
  163.               Button({ type: ButtonType.Normal, stateEffect: true }){ 
  164.                 if (this.isCountDown || !this.isCountDown) { 
  165.                   if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 1){ 
  166.                     if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == -1){ 
  167.                       Image($rawfile('mine.png')) 
  168.                         .height(358 / this.column - 2) 
  169.                         .width(358 / this.column - 2) 
  170.                         .scale({ x: 0.9, y: 0.9 }) 
  171.                     }else
  172.                       Text(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0 ? ' ' : grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()) 
  173.                         .height(358 / this.column - 2) 
  174.                         .width(358 / this.column - 2) 
  175.                         .fontSize(358 / this.column - 4) 
  176.                         .fontColor(colors[grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()]) 
  177.                         .textAlign(TextAlign.Center) 
  178.                         .fontWeight(600) 
  179.                     } 
  180.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 2){ 
  181.                     Image($rawfile('flag.png')) 
  182.                       .height(358 / this.column - 2) 
  183.                       .width(358 / this.column - 2) 
  184.                       .scale({ x: 0.9, y: 0.9 }) 
  185.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 3){ 
  186.                     Text('?'
  187.                       .height(358 / this.column - 2) 
  188.                       .width(358 / this.column - 2) 
  189.                       .fontSize(358 / this.column - 2) 
  190.                       .fontColor('#FFFFFF'
  191.                       .textAlign(TextAlign.Center) 
  192.                       .fontWeight(800) 
  193.                   } 
  194.                 } 
  195.               } 
  196.               .height(353 / this.column - 2) 
  197.               .width(353 / this.column - 2) 
  198.               .backgroundColor(colors['1' + this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1]]) 
  199.               .onClick(() => { 
  200.                 if(this.statesBtn == 1){ 
  201.                   if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  202.                     this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 1 
  203.                     if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  204.                       this.estimatemine(parseInt(day_row) - 1, parseInt(day_column) - 1) 
  205.                     } 
  206.                   } 
  207.                 }else if(this.statesBtn == 0){ 
  208.                   if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0 && this.mine > 0) { 
  209.                     this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 2 
  210.                     this.mine-- 
  211.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 2) { 
  212.                     this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 3 
  213.                     this.mine++ 
  214.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 3) { 
  215.                     this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 0 
  216.                   } 
  217.                 } 
  218.                 if(this.isCountDown){ 
  219.                   this.isCountDown = false 
  220.                 }else
  221.                   this.isCountDown = true 
  222.                 } 
  223.               }) 
  224.             } 
  225.           }, day_column => day_column) 
  226.         }, day_row => day_row) 
  227.       } 
  228.       .columnsTemplate(this.row == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  229.           this.row == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  230.       .rowsTemplate(this.column == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  231.           this.column == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  232.       .columnsGap(2) 
  233.       .rowsGap(2) 
  234.       .width(355) 
  235.       .backgroundColor('#8CC8F5'
  236.       .height(355) 
  237.       .margin({ top: 10 }) 
  238.  
  239.       Row(){ 
  240.         setbtn({ str: '翻  开', col: '2', sta: $statesBtn }) 
  241.         setbtn({ str: '插  旗', col: '3', sta: $statesBtn }) 
  242.       } 
  243.     } 
  244.     .width('100%'
  245.     .height('100%'
  246.     .backgroundColor('#D6DDE7'
  247.   } 
  248.  
  249. @Component 
  250. struct setImage { 
  251.   private str: string 
  252.  
  253.   build() { 
  254.     Image($rawfile(this.str)) 
  255.       .height(50) 
  256.       .width(50) 
  257.       .scale({ x: 0.9, y: 0.9 }) 
  258.       .margin({ left:10, top: 3 }) 
  259.   } 
  260.  
  261. @Component 
  262. struct setText { 
  263.   @Link num: number 
  264.  
  265.   build() { 
  266.     Text(this.num.toString()) 
  267.       .width(60) 
  268.       .height(30) 
  269.       .borderRadius(10) 
  270.       .borderColor('#4162AA'
  271.       .borderWidth(1) 
  272.       .fontSize(26) 
  273.       .textAlign(TextAlign.Center) 
  274.       .fontWeight(700) 
  275.       .fontColor('#FFFFFF'
  276.       .backgroundColor('#4162AA'
  277.       .margin({ left:3, top: 5 }) 
  278.       .padding(0) 
  279.   } 
  280.  
  281. @Component 
  282. struct setbtn { 
  283.   private str: string 
  284.   private col: string 
  285.   @Link sta: number 
  286.  
  287.   build() { 
  288.     Button(this.str, { type: ButtonType.Normal, stateEffect: true }) 
  289.       .width(130) 
  290.       .height(50) 
  291.       .borderRadius(8) 
  292.       .borderColor('#6379A8'
  293.       .borderWidth(2) 
  294.       .fontSize(26) 
  295.       .fontWeight(700) 
  296.       .fontColor('#1E2B46'
  297.       .backgroundColor(colors[this.col + this.sta]) 
  298.       .margin(10) 
  299.       .onClick(() => { 
  300.         if(this.col == '2'){ 
  301.           this.sta = 1 
  302.         }else if(this.col == '3'){ 
  303.           this.sta = 0 
  304.         } 
  305.       }) 
  306.   } 

4. 实现自定义弹窗和重新开始

定义状态变量success和over并初始化为false和true,用于记录游戏是否成功和游戏是否失败,当success为true时表示游戏成功,当over为false时表示游戏失败。

添加名为gamesuccess()和gameover()用于判断游戏是否成功和游戏是否失败。

使用装饰器@CustomDialog自定义弹窗界面。

在函数initialize()的定时器中对success和over进行实时更新,并且当游戏成功或游戏失败时停止定时器和通过open()启动自定义弹窗界面。

在容器Grid的按钮组件的点击事件中增加判断,当游戏没有成功或游戏没有失败时才响应点击事件。

在重新开始按钮的点击事件中对所有变量进行初始化,并且调用函数initialize()。

game.ets:

  1. import router from '@system.router' 
  2.  
  3. const colors={ 
  4.   "0""#E0E4F0"
  5.   "1""#2348A0"
  6.   "2""#247411"
  7.   "3""#AF121B"
  8.   "4""#04289E"
  9.   "5""#D2090E"
  10.   "6""#008000"
  11.   "7""#000080"
  12.   "8""#800000"
  13.   "10""#3C7CF6"
  14.   "11""#E0E4F0"
  15.   "20""#CBD8E8"
  16.   "21""#4774D8"
  17.   "30""#4774D8"
  18.   "31""#CBD8E8" 
  19.  
  20. var grids; 
  21. var timeoutID; 
  22.  
  23. @Entry 
  24. @Component 
  25. struct Game { 
  26.   private row: number = router.getParams().Number_row 
  27.   private column: number = router.getParams().Number_column 
  28.   @State time: number = 0 
  29.   @State mine: number = router.getParams().difficulty 
  30.   @State Number_row: string[] = ['1''2''3''4''5''6''7''8''9'
  31.   @State Number_column: string[] = ['1''2''3''4''5''6''7''8''9'
  32.   @State statesGrids: number[][] = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  33.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  34.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  35.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  36.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  37.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  38.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  39.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  40.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  41.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  42.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  43.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  44.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  45.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  46.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  47.                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  48.   @State isCountDown: boolean = true 
  49.   @State statesBtn: number = 1 
  50.   @State success: boolean = false 
  51.   @State over: boolean = true 
  52.   dialogController: CustomDialogController = new CustomDialogController({ 
  53.     builder: CustomDialogExample({ success : $success, difficulty : $mine, time : $time}), 
  54.     autoCancel: true 
  55.   }) 
  56.  
  57.   aboutToAppear(){ 
  58.     if(this.row == 12){ 
  59.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12'
  60.     } else if(this.row == 16){ 
  61.       this.Number_row = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  62.     } 
  63.  
  64.     if(this.column == 12){ 
  65.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12'
  66.     } else if(this.column == 16){ 
  67.       this.Number_column = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16'
  68.     } 
  69.  
  70.     this.initialize() 
  71.   } 
  72.  
  73.   initialize(){ 
  74.     grids = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  75.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  76.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  77.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  78.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  79.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  80.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  81.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  82.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  83.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  84.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  85.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  86.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  87.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  88.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  89.              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
  90.  
  91.     let i = 0; 
  92.     while(i < this.mine){ 
  93.       let random_row = Math.floor(Math.random() * this.row); 
  94.       let random_column = Math.floor(Math.random() * this.column); 
  95.       if(grids[random_row][random_column] == 0){ 
  96.         grids[random_row][random_column] = -1 
  97.         i++ 
  98.       } 
  99.     } 
  100.  
  101.     for(let i = 0; i < this.row; i++){ 
  102.       for(let j = 0; j < this.column; j++){ 
  103.         if(grids[i][j] != -1){ 
  104.           let k = 0 
  105.           for(let ii = i - 1; ii <= i + 1; ii++){ 
  106.             for(let jj = j - 1; jj <= j + 1; jj++){ 
  107.               if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && grids[ii][jj] == -1){ 
  108.                 k++ 
  109.               } 
  110.             } 
  111.           } 
  112.           grids[i][j] = k 
  113.         } 
  114.       } 
  115.     } 
  116.  
  117.     timeoutID = setInterval(() =>{ 
  118.       this.time += 1 
  119.       this.success = this.gamesuccess() 
  120.       this.over = this.gameover() 
  121.       if(this.success || !this.over){ 
  122.         clearInterval(timeoutID) 
  123.         this.dialogController.open() 
  124.       } 
  125.     }, 1000); 
  126.   } 
  127.  
  128.   estimatemine(i, j){ 
  129.     for(let ii = i - 1; ii <= i + 1; ii++){ 
  130.       for(let jj = j - 1; jj <= j + 1; jj++){ 
  131.         if(0 <= ii && ii < this.row && 0 <= jj && jj < this.column && (ii != i || jj != j)){ 
  132.           if(this.statesGrids[ii][jj] == 0){ 
  133.             this.statesGrids[ii][jj] = 1 
  134.             if(grids[ii][jj] == 0){ 
  135.               this.estimatemine(ii, jj) 
  136.             } 
  137.           } 
  138.         } 
  139.       } 
  140.     } 
  141.   } 
  142.  
  143.   gamesuccess(){ 
  144.     for(let i = 0; i < this.row; i++){ 
  145.       for(let j = 0; j < this.column; j++){ 
  146.         if(grids[i][j] != -1){ 
  147.           if(this.statesGrids[i][j] != 1){ 
  148.             return false 
  149.           } 
  150.         } 
  151.       } 
  152.     } 
  153.     return true 
  154.   } 
  155.  
  156.   gameover(){ 
  157.     for(let i = 0; i < this.row; i++){ 
  158.       for(let j = 0; j < this.column; j++){ 
  159.         if(grids[i][j] == -1){ 
  160.           if(this.statesGrids[i][j] == 1){ 
  161.             return false 
  162.           } 
  163.         } 
  164.       } 
  165.     } 
  166.     return true 
  167.   } 
  168.  
  169.   build() { 
  170.     Column(){ 
  171.       Row() { 
  172.         Button('菜 单', { type: ButtonType.Normal, stateEffect: true }) 
  173.           .width(95) 
  174.           .height(50) 
  175.           .borderRadius(8) 
  176.           .borderColor('#6379A8'
  177.           .borderWidth(2) 
  178.           .fontSize(26) 
  179.           .fontWeight(700) 
  180.           .fontColor('#1E2B46'
  181.           .backgroundColor('#C1D0E6'
  182.           .margin({ left:10, top: 2 }) 
  183.           .onClick(() => { 
  184.             clearInterval(timeoutID) 
  185.             router.back() 
  186.           }) 
  187.         setImage({ str: 'time.png' }) 
  188.         setText({ num: $time }) 
  189.         setImage({ str: 'lei.png' }) 
  190.         setText({ num: $mine }) 
  191.       } 
  192.       .width('100%'
  193.       .height(60) 
  194.       .backgroundColor('#C1D0E6'
  195.       .margin({ top: 80 }) 
  196.  
  197.       Grid() { 
  198.         ForEach(this.Number_row, (day_row: string) => { 
  199.           ForEach(this.Number_column, (day_column: string) => { 
  200.             GridItem() { 
  201.               Button({ type: ButtonType.Normal, stateEffect: true }){ 
  202.                 if (this.isCountDown || !this.isCountDown) { 
  203.                   if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 1){ 
  204.                     if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == -1){ 
  205.                       Image($rawfile('mine.png')) 
  206.                         .height(358 / this.column - 2) 
  207.                         .width(358 / this.column - 2) 
  208.                         .scale({ x: 0.9, y: 0.9 }) 
  209.                     }else
  210.                       Text(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0 ? ' ' : grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()) 
  211.                         .height(358 / this.column - 2) 
  212.                         .width(358 / this.column - 2) 
  213.                         .fontSize(358 / this.column - 4) 
  214.                         .fontColor(colors[grids[parseInt(day_row) - 1][parseInt(day_column) - 1].toString()]) 
  215.                         .textAlign(TextAlign.Center) 
  216.                         .fontWeight(600) 
  217.                     } 
  218.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 2){ 
  219.                     Image($rawfile('flag.png')) 
  220.                       .height(358 / this.column - 2) 
  221.                       .width(358 / this.column - 2) 
  222.                       .scale({ x: 0.9, y: 0.9 }) 
  223.                   }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 3){ 
  224.                     Text('?'
  225.                       .height(358 / this.column - 2) 
  226.                       .width(358 / this.column - 2) 
  227.                       .fontSize(358 / this.column - 2) 
  228.                       .fontColor('#FFFFFF'
  229.                       .textAlign(TextAlign.Center) 
  230.                       .fontWeight(800) 
  231.                   } 
  232.                 } 
  233.               } 
  234.               .height(353 / this.column - 2) 
  235.               .width(353 / this.column - 2) 
  236.               .backgroundColor(colors['1' + this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1]]) 
  237.               .onClick(() => { 
  238.                 if(!this.success && this.over){ 
  239.                   if(this.statesBtn == 1){ 
  240.                     if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  241.                       this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 1 
  242.                       if(grids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0){ 
  243.                         this.estimatemine(parseInt(day_row) - 1, parseInt(day_column) - 1) 
  244.                       } 
  245.                     } 
  246.                   }else if(this.statesBtn == 0){ 
  247.                     if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 0 && this.mine > 0) { 
  248.                       this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 2 
  249.                       this.mine-- 
  250.                     }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 2) { 
  251.                       this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 3 
  252.                       this.mine++ 
  253.                     }else if(this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] == 3) { 
  254.                       this.statesGrids[parseInt(day_row) - 1][parseInt(day_column) - 1] = 0 
  255.                     } 
  256.                   } 
  257.                   if(this.isCountDown){ 
  258.                     this.isCountDown = false 
  259.                   }else
  260.                     this.isCountDown = true 
  261.                   } 
  262.                 } 
  263.               }) 
  264.             } 
  265.           }, day_column => day_column) 
  266.         }, day_row => day_row) 
  267.       } 
  268.       .columnsTemplate(this.row == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  269.         this.row == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  270.       .rowsTemplate(this.column == 12 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : 
  271.           this.column == 16 ? '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr' : '1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  272.       .columnsGap(2) 
  273.       .rowsGap(2) 
  274.       .width(355) 
  275.       .backgroundColor('#8CC8F5'
  276.       .height(355) 
  277.       .margin({ top: 10 }) 
  278.  
  279.       Row(){ 
  280.         setbtn({ str: '翻  开', col: '2', sta: $statesBtn }) 
  281.         setbtn({ str: '插  旗', col: '3', sta: $statesBtn }) 
  282.       } 
  283.       Button('重新开始', { type: ButtonType.Normal, stateEffect: true }) 
  284.         .width(150) 
  285.         .height(50) 
  286.         .borderRadius(8) 
  287.         .borderColor('#6379A8'
  288.         .borderWidth(2) 
  289.         .fontSize(26) 
  290.         .fontWeight(700) 
  291.         .fontColor('#1E2B46'
  292.         .backgroundColor('#CBD8E8'
  293.         .margin(10) 
  294.         .onClick(() => { 
  295.           clearInterval(timeoutID) 
  296.           this.statesBtn = 1 
  297.           this.success = false 
  298.           this.over = true 
  299.           this.time = 0 
  300.           this.mine = router.getParams().difficulty 
  301.           if(this.isCountDown){ 
  302.             this.isCountDown = false 
  303.           }else
  304.             this.isCountDown = true 
  305.           } 
  306.           for(let i = 0; i < this.row; i++){ 
  307.             for(let j = 0; j < this.column; j++){ 
  308.               this.statesGrids[i][j] = 0 
  309.             } 
  310.           } 
  311.           this.initialize() 
  312.         }) 
  313.     } 
  314.     .width('100%'
  315.     .height('100%'
  316.     .backgroundColor('#D6DDE7'
  317.   } 
  318.  
  319. @Component 
  320. struct setImage { 
  321.   private str: string 
  322.  
  323.   build() { 
  324.     Image($rawfile(this.str)) 
  325.       .height(50) 
  326.       .width(50) 
  327.       .scale({ x: 0.9, y: 0.9 }) 
  328.       .margin({ left:10, top: 3 }) 
  329.   } 
  330.  
  331. @Component 
  332. struct setText { 
  333.   @Link num: number 
  334.  
  335.   build() { 
  336.     Text(this.num.toString()) 
  337.       .width(60) 
  338.       .height(30) 
  339.       .borderRadius(10) 
  340.       .borderColor('#4162AA'
  341.       .borderWidth(1) 
  342.       .fontSize(26) 
  343.       .textAlign(TextAlign.Center) 
  344.       .fontWeight(700) 
  345.       .fontColor('#FFFFFF'
  346.       .backgroundColor('#4162AA'
  347.       .margin({ left:3, top: 5 }) 
  348.       .padding(0) 
  349.   } 
  350.  
  351. @Component 
  352. struct setbtn { 
  353.   private str: string 
  354.   private col: string 
  355.   @Link sta: number 
  356.  
  357.   build() { 
  358.     Button(this.str, { type: ButtonType.Normal, stateEffect: true }) 
  359.       .width(130) 
  360.       .height(50) 
  361.       .borderRadius(8) 
  362.       .borderColor('#6379A8'
  363.       .borderWidth(2) 
  364.       .fontSize(26) 
  365.       .fontWeight(700) 
  366.       .fontColor('#1E2B46'
  367.       .backgroundColor(colors[this.col + this.sta]) 
  368.       .margin(10) 
  369.       .onClick(() => { 
  370.         if(this.col == '2'){ 
  371.           this.sta = 1 
  372.         }else if(this.col == '3'){ 
  373.           this.sta = 0 
  374.         } 
  375.       }) 
  376.   } 
  377.  
  378. @CustomDialog 
  379. struct CustomDialogExample { 
  380.   controller: CustomDialogController 
  381.   @Link success: boolean 
  382.   @Link difficulty: number 
  383.   @Link time: number 
  384.  
  385.   build() { 
  386.     Column() { 
  387.       Text(this.success ? '挑战成功' : '挑战失败'
  388.         .width('100%'
  389.         .fontSize(28) 
  390.         .fontWeight(900) 
  391.         .fontColor('#FFFFFF'
  392.         .textAlign(TextAlign.Center) 
  393.         .margin({ top: 5, bottom: 5 }) 
  394.       Text('当前难度:' + (this.difficulty == 10 ? '初级' : this.difficulty == 30 ? '中级' : 
  395.       this.difficulty == 50 ? '高级' : ' ')) 
  396.         .width('100%'
  397.         .fontSize(26) 
  398.         .fontWeight(600) 
  399.         .fontColor('#1E2B46'
  400.         .textAlign(TextAlign.Center) 
  401.         .margin({ top: 5, bottom: 5 }) 
  402.       Text('用时:' + (Math.floor(this.time / 3600) < 10 ? '0' + Math.floor(this.time / 3600).toString() :  Math.floor(this.time / 3600).toString()) 
  403.       + ':' + (Math.floor(this.time % 3600 / 60) < 10 ? '0' + Math.floor(this.time % 3600 / 60).toString() : Math.floor(this.time % 3600 / 60).toString()) 
  404.       + ':' + (Math.floor(this.time % 3600 % 60) < 10 ? '0' + Math.floor(this.time % 3600 % 60).toString() : Math.floor(this.time % 3600 % 60).toString())) 
  405.         .width('100%'
  406.         .fontSize(26) 
  407.         .fontWeight(600) 
  408.         .fontColor('#1E2B46'
  409.         .textAlign(TextAlign.Center) 
  410.         .margin({ top: 5, bottom: 5 }) 
  411.       Row() { 
  412.         Button("返回主页", { type: ButtonType.Normal, stateEffect: true }) 
  413.           .width(130) 
  414.           .height(50) 
  415.           .borderRadius(8) 
  416.           .borderColor('#6379A8'
  417.           .borderWidth(2) 
  418.           .fontSize(22) 
  419.           .fontWeight(600) 
  420.           .fontColor('#1E2B46'
  421.           .backgroundColor('#D6DDE7'
  422.           .margin({ left: 25, top: 5, bottom: 5 }) 
  423.           .onClick(() => { 
  424.             router.back() 
  425.           }) 
  426.         Button("返回界面", { type: ButtonType.Normal, stateEffect: true }) 
  427.           .width(130) 
  428.           .height(50) 
  429.           .borderRadius(8) 
  430.           .borderColor('#6379A8'
  431.           .borderWidth(2) 
  432.           .fontSize(22) 
  433.           .fontWeight(600) 
  434.           .fontColor('#1E2B46'
  435.           .backgroundColor('#D6DDE7'
  436.           .margin({ left: 25, top: 5, bottom: 5 }) 
  437.           .onClick(() => { 
  438.             this.controller.close() 
  439.           }) 
  440.       } 
  441.       .width('100%'
  442.     }.backgroundColor('#CBD2E4'
  443.   } 

 文章相关附件可以点击下面的原文链接前往下载

https://harmonyos.51cto.com/resource/1548

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

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

https://harmonyos.51cto.co

 

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

2021-12-01 15:38:33

鸿蒙HarmonyOS应用

2022-02-09 19:37:00

Ability鸿蒙开发

2021-11-02 14:52:17

鸿蒙HarmonyOS应用

2021-11-01 11:08:28

鸿蒙HarmonyOS应用

2021-09-17 14:47:33

鸿蒙HarmonyOS应用

2021-10-28 14:58:15

鸿蒙HarmonyOS应用

2022-10-24 14:49:54

ArkUI心电图组件

2022-11-02 16:06:54

ArkUIETS

2021-11-02 14:55:42

鸿蒙HarmonyOS应用

2022-06-27 14:12:32

css鸿蒙自定义

2022-07-13 16:24:12

ArkUI(JS)打地鼠游戏

2022-09-21 14:51:21

ArkUI信件弹出

2022-08-05 19:27:22

通用API鸿蒙

2022-09-14 15:17:26

ArkUI鸿蒙

2022-10-17 14:36:09

ArkUI虚拟摇杆组件

2022-03-17 15:28:18

五子棋HarmonyOSJSAPI

2024-01-11 15:54:55

eTS语言TypeScript应用开发

2021-12-27 15:10:55

鸿蒙HarmonyOS应用

2022-08-22 17:28:34

ArkUI鸿蒙

2022-09-05 15:22:27

ArkUIets
点赞
收藏

51CTO技术栈公众号