ETS版的数字华容道

开发
相信大家伙都对新玩意ets挺好奇的,我也不例外。前几天我们木棉花小组一同用ETS写了数字华容道的格子布局,然后我就用日常的空余时间简单完善了一下这个demo啦。

[[432855]]

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

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

https://harmonyos.51cto.com

前言

相信大家伙都对新玩意ets挺好奇的,我也不例外。前几天我们木棉花小组一同用ETS写了数字华容道的格子布局,然后我就用日常的空余时间简单完善了一下这个demo啦,但是我对ets没有很懂,欢迎各位评论区指导一下哈O(∩_∩)O

概述

这是文件架构

#星光计划1.0#【木棉花】:ETS版的数字华容道-鸿蒙HarmonyOS技术社区

 

效果图如下

#星光计划1.0#【木棉花】:ETS版的数字华容道-鸿蒙HarmonyOS技术社区#星光计划1.0#【木棉花】:ETS版的数字华容道-鸿蒙HarmonyOS技术社区#星光计划1.0#【木棉花】:ETS版的数字华容道-鸿蒙HarmonyOS技术社区

正文

1. 新建一个空白工程

DevEco Studio下载安装成功后,打开DevEco Studio,点击左上角的File,点击New,再选择New Project,选择Empty Ability,然后点击Next,给项目命名ETS,选择设备类型Phone,选择语言类型ets最后点击Finish

#星光计划1.0#【木棉花】:ETS版的数字华容道-鸿蒙HarmonyOS技术社区

2. 主页面设置

导入router模块以实现页面跳转,在Flex容器组件下添加Text组件,Button组件,点击Button可跳转至页面(uri的地址),最后设置容器整体的宽高

  1. import router from '@system.router'
  2. @Entry 
  3. @Component 
  4. struct Index { 
  5.   build() {    
  6.     Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  7.       Text('数字华容道'
  8.         .fontSize(40) 
  9.         .margin({top:20,bottom:20}) 
  10.  
  11.       Button('4x4'
  12.         .fontSize(30) 
  13.         .margin({top:20,bottom:20}) 
  14.         .width(280) 
  15.         .height(60) 
  16.         .onClick(() => { 
  17.           router.push({ uri: 'pages/forth' }) 
  18.         }) 
  19.       Button('5x5'
  20.         .fontSize(30) 
  21.         .margin({top:20,bottom:20}) 
  22.         .width(280) 
  23.         .height(60) 
  24.         .onClick(() => { 
  25.           router.push({ uri: 'pages/fifth' }) 
  26.         }) 
  27.     } 
  28.     .width('100%'
  29.     .height('100%'
  30.   } 

3. 4x4游戏页面的设置

右击pages>>New>>eTS Page,新建一个页面,命名‘forth’,删除组件Text.然后在最下方自定义组件setText和setTime,前者是游戏格子一行的布局,后者是显示时间的组件;export是为了将自定义组件导入第二个页面‘5x5’而加的

  1. @Component 
  2. export struct setText{ 
  3.   @Link t:number[] 
  4.   build(){ 
  5.     Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  6.       ForEach(this.t,(item:number)=>Text(item==0 ? '':item.toString()) 
  7.         .fontSize(50) 
  8.         .fontWeight(FontWeight.Bold) 
  9.         .backgroundColor('#678912'
  10.         .fontColor('#FFFFFF'
  11.         .width(60) 
  12.         .height(60) 
  13.         .margin({ left: 5, right: 5, top: 5, bottom: 5 }) 
  14.         .textAlign(TextAlign.Center),    
  15.         item =>item.toString()) 
  16.     }} 
  17.  
  18. @Component 
  19. export struct setTime{ 
  20.   @Link h:string 
  21.   @Link m:string 
  22.   @Link s:string 
  23.   @Link ms:string 
  24.   build(){ 
  25.     Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  26.       Text(this.h+':'+this.m+':'+this.s+':'+this.ms).fontSize(40).textAlign(TextAlign.Center).margin({top:10,bottom:10}) 
  27.     } 
  28.   } 

定义变量

  1. var row_0=3 
  2. var column_0=3 
  3. var timer=null 
  4. var stop=true 
  5. import router from '@system.router'
  6. @Entry 
  7. @Component 
  8. struct Forth { 
  9.   @State grids:number[][]=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,0]] 
  10.   @State grids1:number[]=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3]] 
  11.   @State grids2:number[]=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3]] 
  12.   @State grids3:number[]=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3]] 
  13.   @State grids4:number[]=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3]] 
  14.   @State strhour:string='0' 
  15.   @State strmin:string='0' 
  16.   @State strsec:string='0' 
  17.   @State strmsec:string='0' 
  18.   @State hour:number = 0; 
  19.   @State min:number = 0; 
  20.   @State sec:number = 0; 
  21.   @State msec:number = 0; 

定义变量后,在Forth的build()里增加组件完成格子布局和计时器布局,组件竖直排列

  1. Column(){ 
  2.       setTime({h:$strhour,m:$strmin,s:$strsec,ms:$strmsec}) 
  3.       setText({t:$grids1}) 
  4.       setText({t:$grids2}) 
  5.       setText({t:$grids3}) 
  6.       setText({t:$grids4}) 

 在build上方定义函数change,通过方向值来判断实现数字的移动

  1. change(direction){ 
  2.    if(direction=='down') { 
  3.      if(row_0>0 && row_0<4){ 
  4.        let temp = this.grids[row_0-1][column_0] 
  5.        this.grids[row_0-1][column_0] = 0 
  6.        this.grids[row_0][column_0] = temp 
  7.        row_0--} 
  8.    }  

然后在build里添加4个按钮组件,分别是“上、下、左、右”,其中左跟右同一水平放置

  1. Button('上'
  2.         .width(60) 
  3.         .height(60) 
  4.         .fontSize(30) 
  5.         .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  6.         .align(Alignment.Center) 
  7.         .backgroundColor('#974B31'
  8.         .fontColor('#FFFFFF'
  9.         .onClick((event: ClickEvent) => { 
  10.  
  11.             this.change('up'
  12.             this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  13.             this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  14.             this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  15.             this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  16.       
  17.         }) 
  18.  
  19.       Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  20.         Button('左'
  21.           .width(60) 
  22.           .height(60) 
  23.           .fontSize(30) 
  24.           .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  25.           .align(Alignment.Center) 
  26.           .backgroundColor('#974B31'
  27.           .fontColor('#FFFFFF'
  28.           .onClick((event: ClickEvent) => { 
  29.               this.change('left'
  30.             this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  31.             this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  32.             this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  33.             this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  34.            
  35.  
  36.           }) 
  37.         Button('右'
  38.           .width(60) 
  39.           .height(60) 
  40.           .fontSize(30) 
  41.           .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  42.           .align(Alignment.Center) 
  43.           .backgroundColor('#974B31'
  44.           .fontColor('#FFFFFF'
  45.           .onClick((event: ClickEvent) => { 
  46.            
  47.              this.change('right'
  48.             this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  49.             this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  50.             this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  51.             this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  52.       
  53.           }) 
  54.       } 
  55.       Button('下'
  56.         .width(60) 
  57.         .height(60) 
  58.         .fontSize(30) 
  59.         .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  60.         .align(Alignment.Center) 
  61.         .backgroundColor('#974B31'
  62.         .fontColor('#FFFFFF'
  63.         .onClick((event: ClickEvent) => { 
  64.          
  65.              this.change('down'
  66.           this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  67.           this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  68.           this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  69.           this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  70.  
  71.         }) 

 实现数字的随机打乱和计时器的流动

为了使时间显示得好看点,若时间为个位数时十位补0,然后设置函数run_time用以定时器内循环

  1. onPageShow(){ 
  2.   this.settime(this.msec,this.sec,this.min,this.hour
  3.  
  4. ttime(msec,sec,min,hour){ 
  5.   if (msec < 10) { 
  6.     this.strmsec = "0" + msec.toString(); 
  7.   } else if (msec >= 10) { 
  8.     this.strmsec =msec.toString(); 
  9.   } 
  10.   if (sec < 10){ 
  11.     this.strsec = "0" + sec.toString(); 
  12.   } else if (sec >= 10) { 
  13.     this.strsec =sec.toString(); 
  14.   } 
  15.   if (min < 10){ 
  16.     this.strmin = "0" + min.toString(); 
  17.   } else if (min >= 10) { 
  18.     this.strmin = min.toString(); 
  19.   } 
  20.   if (hour < 10){ 
  21.     this.strhour = "0" + hour.toString(); 
  22.   } else if (hour >= 10) { 
  23.     this.strhour = hour.toString(); 
  24.   } 
  25.  
  26. run_time(){ 
  27.   this.msec+=1 
  28.   if(this.msec==100){ 
  29.     this.msec=0 
  30.     this.sec+=1 
  31.   } 
  32.   if(this.sec==60){ 
  33.     this.sec=0 
  34.     this.min+=1 
  35.   } 
  36.   if(this.min==60){ 
  37.     this.min=0 
  38.     this.hour+=1 
  39.   } 
  40.   this.settime(this.msec,this.sec,this.min,this.hour

随机打乱数字,重新开始时清空上一个定时器,并时间重置为0

  1. init(){ 
  2.     let array=["left","up","right","down"]; 
  3.     for (let i = 0; i < 100; i++){ 
  4.       let randomIndex = Math.floor(Math.random() * 4); 
  5.       let direction = array[randomIndex]; 
  6.       this.change(direction) 
  7.       this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  8.       this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  9.       this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  10.       this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  11.       this.msec=0 
  12.       this.hour=0 
  13.       this.sec=0 
  14.       this.min=0 
  15.       clearInterval(timer) 
  16.       timer=null 
  17.     } 
  18.   } 

在“上”按钮组件上方添加以下代码

  1. Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  2.      Button('开始游戏'
  3.        .width(160) 
  4.        .height(60) 
  5.        .fontSize(30) 
  6.        .margin({ left: 3, right: 3, top: 3, bottom: 5 }) 
  7.        .align(Alignment.Center) 
  8.        .backgroundColor('#974B31'
  9.        .fontColor('#FFFFFF'
  10.        .onClick((event: ClickEvent) => { 
  11.          this.init() 
  12.          timer = setInterval(() => 
  13.          this.run_time(), 10) 
  14.  
  15.        }) 
  16.  
  17.        Button('返回'
  18.          .width(88) 
  19.          .height(60) 
  20.          .fontSize(27) 
  21.          .margin({ left: 3, right: 3, top: 3, bottom: 5 }) 
  22.          .align(Alignment.Center) 
  23.          .backgroundColor('#974B31'
  24.          .fontColor('#FFFFFF'
  25.          .onClick(() => { 
  26.            router.push({ uri: 'pages/index' }) 
  27.          }) 
  28.  
  29.      Button('暂停'
  30.          .width(88) 
  31.          .height(60) 
  32.          .fontSize(27) 
  33.          .margin({ left: 3, right: 3, top: 3, bottom: 5 }) 
  34.          .align(Alignment.Center) 
  35.          .backgroundColor('#974B31'
  36.          .fontColor('#FFFFFF'
  37.          .onClick(() => { 
  38.            if(stop==true){ 
  39.            clearInterval(timer) 
  40.            timer=null 
  41.            stop=false 
  42.            } 
  43.            else if(stop==false){ 
  44.              stop=true 
  45.              timer = setInterval(() => 
  46.              this.run_time(), 10) 
  47.            } 
  48.          }) 
  49.    } 

设置游戏结束函数gameover,当游戏成功时清空定时器,且方向按钮无点击事件

  1. gameover() { 
  2.       for (let column = 0; column < column_0; column++) { 
  3.         if (this.grids1[column] != this.grids[0][column]){ 
  4.           return false 
  5.         } 
  6.         if(this.grids2[column]!=this.grids[1][column]){ 
  7.           return false 
  8.         } 
  9.         if(this.grids3[column]!=this.grids[2][column]){ 
  10.           return false 
  11.         } 
  12.         if(this.grids4[column]!=this.grids[3][column]){ 
  13.           return false 
  14.         } 
  15.       } 
  16.     return true 
  17.   } 

在四个方向按钮的点击事件中加入游戏是否成功的判断

  1. .onClick((event: ClickEvent) => { 
  2.           if(!this.gameover()) { 
  3.             this.change('up'
  4.             this.grids1 = [this.grids[0][0], this.grids[0][1], this.grids[0][2], this.grids[0][3]] 
  5.             this.grids2 = [this.grids[1][0], this.grids[1][1], this.grids[1][2], this.grids[1][3]] 
  6.             this.grids3 = [this.grids[2][0], this.grids[2][1], this.grids[2][2], this.grids[2][3]] 
  7.             this.grids4 = [this.grids[3][0], this.grids[3][1], this.grids[3][2], this.grids[3][3]] 
  8.           } 
  9.           if(this.gameover()){ 
  10.             clearInterval(timer) 
  11.             timer=null 
  12.           } 

4. 5x5的页面设置

套路跟4x4一样,新建一个页面,然后导入4x4页面自定义的两个组件,改一下数组数据,这里直接上代码

  1. var row_0=4 
  2. var column_0=4 
  3. var timer=null
  4. var stop=true 
  5. import router from '@system.router'
  6. import {setText,setTime} from './forth.ets' 
  7. @Entry 
  8. @Component 
  9. struct Fifth { 
  10.   @State grids:number[][]=[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,0]] 
  11.   @State grids1:number[]=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  12.   @State grids2:number[]=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  13.   @State grids3:number[]=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  14.   @State grids4:number[]=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  15.   @State grids5:number[]=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  16.   @State strhour:string='0' 
  17.   @State strmin:string='0' 
  18.   @State strsec:string='0' 
  19.   @State strmsec:string='0' 
  20.   @State hour:number = 0; 
  21.   @State min:number = 0; 
  22.   @State sec:number = 0; 
  23.   @State msec:number = 0; 
  24.   onPageShow(){ 
  25.  
  26.     this.settime(this.msec,this.sec,this.min,this.hour
  27.  
  28.   } 
  29.   gameover() { 
  30.     for (let column = 0; column < column_0; column++) { 
  31.       if (this.grids1[column] != this.grids[0][column]){ 
  32.         return false 
  33.       } 
  34.       if(this.grids2[column]!=this.grids[1][column]){ 
  35.         return false 
  36.       } 
  37.       if(this.grids3[column]!=this.grids[2][column]){ 
  38.         return false 
  39.       } 
  40.       if(this.grids4[column]!=this.grids[3][column]){ 
  41.         return false 
  42.       } 
  43.     } 
  44.     return true 
  45.   } 
  46.   init(){ 
  47.     let array=["left","up","right","down"]; 
  48.     for (let i = 0; i < 100; i++){ 
  49.       let randomIndex = Math.floor(Math.random() * 5); 
  50.       let direction = array[randomIndex]; 
  51.       this.change(direction) 
  52.       this.grids1=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  53.       this.grids2=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  54.       this.grids3=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  55.       this.grids4=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  56.       this.grids5=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  57.       this.msec=0 
  58.       this.hour=0 
  59.       this.sec=0 
  60.       this.min=0 
  61.       clearInterval(timer) 
  62.       timer=null 
  63.     } 
  64.   } 
  65.  
  66.   change(direction){ 
  67.     if(direction=='down') { 
  68.       if(row_0>0 && row_0<5){ 
  69.         let temp = this.grids[row_0-1][column_0] 
  70.         this.grids[row_0-1][column_0] = 0 
  71.         this.grids[row_0][column_0] = temp 
  72.  
  73.         row_0--} 
  74.  
  75.     } 
  76.  
  77.     if(direction=='up'){ 
  78.       if(row_0>-1 && row_0<4){ 
  79.         let temp = this.grids[row_0+1][column_0] 
  80.         this.grids[row_0+1][column_0] = 0 
  81.         this.grids[row_0][column_0]=temp 
  82.         row_0++} 
  83.  
  84.     } 
  85.  
  86.     if(direction=='right'){ 
  87.       if(column_0>0 && column_0<5){ 
  88.         let temp = this.grids[row_0][column_0-1] 
  89.         this.grids[row_0][column_0-1] = 0 
  90.         this.grids[row_0][column_0]=temp 
  91.         column_0--} 
  92.     } 
  93.  
  94.     if(direction=='left'){ 
  95.       if(column_0>-1 && column_0<4){ 
  96.         let temp = this.grids[row_0][column_0+1] 
  97.         this.grids[row_0][column_0+1] = 0 
  98.         this.grids[row_0][column_0]=temp 
  99.         column_0++} 
  100.     } 
  101.   } 
  102.   settime(msec,sec,min,hour){ 
  103.     if (msec < 10) { 
  104.       this.strmsec = "0" + msec.toString(); 
  105.     } else if (msec >= 10) { 
  106.       this.strmsec =msec.toString(); 
  107.     } 
  108.     if (sec < 10){ 
  109.       this.strsec = "0" + sec.toString(); 
  110.     } else if (sec >= 10) { 
  111.       this.strsec =sec.toString(); 
  112.     } 
  113.     if (min < 10){ 
  114.       this.strmin = "0" + min.toString(); 
  115.     } else if (min >= 10) { 
  116.       this.strmin = min.toString(); 
  117.     } 
  118.     if (hour < 10){ 
  119.       this.strhour = "0" + hour.toString(); 
  120.     } else if (hour >= 10) { 
  121.       this.strhour = hour.toString(); 
  122.     } 
  123.   } 
  124.   run_time(){ 
  125.     this.msec+=1 
  126.     if(this.msec==100){ 
  127.       this.msec=0 
  128.       this.sec+=1 
  129.     } 
  130.     if(this.sec==60){ 
  131.       this.sec=0 
  132.       this.min+=1 
  133.     } 
  134.     if(this.min==60){ 
  135.       this.min=0 
  136.       this.hour+=1 
  137.     } 
  138.     this.settime(this.msec,this.sec,this.min,this.hour
  139.   } 
  140.  
  141.   build() { 
  142.     Column(){ 
  143.       setTime({h:$strhour,m:$strmin,s:$strsec,ms:$strmsec}) 
  144.       setText({t:$grids1}) 
  145.       setText({t:$grids2}) 
  146.       setText({t:$grids3}) 
  147.       setText({t:$grids4}) 
  148.       setText({t:$grids5}) 
  149.  
  150.       Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  151.         Button('开始游戏'
  152.           .width(150) 
  153.           .height(50) 
  154.           .fontSize(28) 
  155.           .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  156.           .align(Alignment.Center) 
  157.           .backgroundColor('#974B31'
  158.           .fontColor('#FFFFFF'
  159.           .onClick((event: ClickEvent) => { 
  160.             this.init() 
  161.             timer = setInterval(() => 
  162.             this.run_time(),10) 
  163.           }) 
  164.  
  165.         Button('返回'
  166.           .width(90) 
  167.           .height(50) 
  168.           .fontSize(28) 
  169.           .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  170.           .align(Alignment.Center) 
  171.           .backgroundColor('#974B31'
  172.           .fontColor('#FFFFFF'
  173.           .onClick(() => { 
  174.             router.push({ uri: 'pages/index' }) 
  175.           }) 
  176.         Button('暂停'
  177.           .width(88) 
  178.           .height(60) 
  179.           .fontSize(27) 
  180.           .margin({ left: 3, right: 3, top: 3, bottom: 5 }) 
  181.           .align(Alignment.Center) 
  182.           .backgroundColor('#974B31'
  183.           .fontColor('#FFFFFF'
  184.           .onClick(() => { 
  185.             if(stop==true){ 
  186.               clearInterval(timer) 
  187.               timer=null 
  188.               stop=false 
  189.             } 
  190.             else if(stop==false){ 
  191.               stop=true 
  192.               timer = setInterval(() => 
  193.               this.run_time(), 10) 
  194.             } 
  195.           }) 
  196.       } 
  197.  
  198.       Button('上'
  199.         .width(60) 
  200.         .height(48) 
  201.         .fontSize(28) 
  202.         .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  203.         .align(Alignment.Center) 
  204.         .backgroundColor('#974B31'
  205.         .fontColor('#FFFFFF'
  206.         .onClick((event: ClickEvent) => { 
  207.            if(!this.gameover()) { 
  208.           this.change('up'
  209.          this.grids1=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  210.          this.grids2=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  211.          this.grids3=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  212.          this.grids4=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  213.          this.grids5=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  214.            } 
  215.            if(this.gameover()){ 
  216.              clearInterval(timer) 
  217.              timer=null 
  218.            } 
  219.         }) 
  220.  
  221.       Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
  222.         Button('左'
  223.           .width(60) 
  224.           .height(48) 
  225.           .fontSize(28) 
  226.           .margin({ left: 20, right: 20}) 
  227.           .align(Alignment.Center) 
  228.           .backgroundColor('#974B31'
  229.           .fontColor('#FFFFFF'
  230.           .onClick((event: ClickEvent) => { 
  231.             if(!this.gameover()) { 
  232.             this.change('left'
  233.             this.grids1=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  234.             this.grids2=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  235.             this.grids3=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  236.             this.grids4=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  237.             this.grids5=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  238.               } 
  239.           if(this.gameover()){ 
  240.             clearInterval(timer) 
  241.             timer=null 
  242.           } 
  243.           }) 
  244.         Button('右'
  245.           .width(60) 
  246.           .height(48) 
  247.           .fontSize(28) 
  248.           .margin({ left: 20, right: 20}) 
  249.           .align(Alignment.Center) 
  250.           .backgroundColor('#974B31'
  251.           .fontColor('#FFFFFF'
  252.           .onClick((event: ClickEvent) => { 
  253.             if(!this.gameover()) { 
  254.             this.change('right'
  255.             this.grids1=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  256.             this.grids2=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  257.             this.grids3=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  258.             this.grids4=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  259.             this.grids5=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  260.               } 
  261.           if(this.gameover()){ 
  262.             clearInterval(timer) 
  263.             timer=null 
  264.           } 
  265.           }) 
  266.       } 
  267.       Button('下'
  268.         .width(60) 
  269.         .height(48) 
  270.         .fontSize(28) 
  271.         .margin({ left: 5, right: 5, top: 3, bottom: 5 }) 
  272.         .align(Alignment.Center) 
  273.         .backgroundColor('#974B31'
  274.         .fontColor('#FFFFFF'
  275.         .onClick((event: ClickEvent) => { 
  276.           if(!this.gameover()) { 
  277.           this.change('down'
  278.           this.grids1=[this.grids[0][0],this.grids[0][1],this.grids[0][2],this.grids[0][3],this.grids[0][4]] 
  279.           this.grids2=[this.grids[1][0],this.grids[1][1],this.grids[1][2],this.grids[1][3],this.grids[1][4]] 
  280.           this.grids3=[this.grids[2][0],this.grids[2][1],this.grids[2][2],this.grids[2][3],this.grids[2][4]] 
  281.           this.grids4=[this.grids[3][0],this.grids[3][1],this.grids[3][2],this.grids[3][3],this.grids[3][4]] 
  282.           this.grids5=[this.grids[4][0],this.grids[4][1],this.grids[4][2],this.grids[4][3],this.grids[4][4]] 
  283.           } 
  284.           if(this.gameover()){ 
  285.             clearInterval(timer) 
  286.             timer=null 
  287.           } 
  288.         }) 
  289.  
  290.     }.width('100%').height('100%'
  291.   } 

结语

以上就是我这次的小分享啦❀❀!

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

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

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

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

https://harmonyos.51cto.com

 

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

2021-08-25 09:54:51

鸿蒙HarmonyOS应用

2021-10-09 14:49:50

鸿蒙HarmonyOS应用

2021-10-22 19:41:01

鸿蒙HarmonyOS应用

2012-11-04 14:54:24

2020-12-22 11:20:36

鸿蒙HarmonyOS游戏

2017-09-25 16:55:35

2013-10-15 09:48:03

C++Lambda函数式编程

2020-12-11 12:27:35

鸿蒙HarmonyOS

2015-11-05 14:34:46

2020-09-10 14:22:55

腾讯安全数字

2022-04-21 14:02:32

简道云全民开发数字化

2022-05-20 10:56:54

AbilityeTS FA调用

2010-09-11 11:12:23

2022-01-25 17:05:44

ArkUI_eTS操作系统鸿蒙

2017-06-07 11:10:44

锐捷

2022-05-07 15:34:16

ETS低代码应用

2021-11-04 11:47:45

腾讯医疗健康

2021-12-13 16:43:04

鸿蒙HarmonyOS应用

2022-08-12 19:13:07

etswifi连接操作

2022-11-30 15:02:33

腾讯全球数字生态大会数字化
点赞
收藏

51CTO技术栈公众号