成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于ArkUI框架的舒爾特方格游戲

系統
舒爾特方格游戲有主界面和游戲界面兩個頁面組成,主界面拆開為title和body兩個自定義組件組成,游戲界面拆開為title,body和footer三個自定義組件組成,utils為隨機生成數字公共類。

[[440822]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1. 效果圖直接先上:

B站蠟筆小新介紹游戲規則: https://www.bilibili.com/video/BV1E3411t7cK?spm_id_from=333.999.0.0

動圖主界面游戲界面

基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區
基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區

2. 項目結構圖

基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區
基于ArkUI框架的舒爾特方格游戲-鴻蒙HarmonyOS技術社區

3. 項目開發介紹

舒爾特方格游戲有主界面和游戲界面兩個頁面組成,主界面拆開為title和body兩個自定義組件組成,游戲界面拆開為title,body和footer三個自定義組件組成,utils為隨機生成數字公共類。下面我們來一個一個界面和組件介紹:

3.1 主界面代碼,只是一個程序入口,具體頁面布局在自定義組件實現:

3.1.1 Index代碼

  1. import { Title } from '../common/home/title' 
  2. import { Body } from '../common/home/body' 
  3.  
  4. @Entry 
  5. @Component 
  6. struct Index { 
  7.   build() { 
  8.     Column() { 
  9.       // 標題 
  10.       Title(); 
  11.       // 游戲主界面 
  12.       Body(); 
  13.     } 
  14.     .alignItems(HorizontalAlign.Center) 
  15.   } 

 3.1.2 Title自定義組件代碼:

  1. @Component 
  2. export struct Title { 
  3.   build() { 
  4.     // 主界面標題 
  5.     Column() { 
  6.       Text("舒爾特方格"
  7.         .fontSize(34).margin({top: 30}) 
  8.         .fontWeight(FontWeight.Bold) 
  9.       Text("SchulteGrid"
  10.         .fontSize(20).margin({top: 3, bottom: 60}) 
  11.         .fontWeight(FontWeight.Bold) 
  12.     } 
  13.     .width('100%'
  14.   } 

 3.1.3 Body自定義組件代碼

  1. import router from '@system.router' 
  2.  
  3. @Component 
  4. export struct Body { 
  5.   build() { 
  6.     Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) { 
  7.       // 3x3, 4x4, 5x5 按鈕布局 
  8.       Row() { 
  9.         Button("3X3", { type: ButtonType.Circle, stateEffect: true }) 
  10.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  11.           .onClick(() => { this.startGame(3) }) 
  12.         Button("4X4", { type: ButtonType.Circle, stateEffect: true }) 
  13.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  14.           .margin({left: 30, right: 30}) 
  15.           .onClick(() => { this.startGame(4) }) 
  16.         Button("5X5", { type: ButtonType.Circle, stateEffect: true }) 
  17.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  18.           .onClick(() => { this.startGame(5) }) 
  19.       }.alignItems(VerticalAlign.Center).margin({bottom: 30}) 
  20.       // 6x6, 7x7 按鈕布局 
  21.       Row() { 
  22.         Button("6X6", { type: ButtonType.Circle, stateEffect: true }) 
  23.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  24.           .onClick(() => { this.startGame(6) }) 
  25.         Button("7X7", { type: ButtonType.Circle, stateEffect: true }) 
  26.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  27.           .margin({left: 30}).onClick(() => { this.startGame(7) }) 
  28.       }.alignItems(VerticalAlign.Center).margin({bottom: 30}) 
  29.       // 8x8, 9x9 按鈕布局 
  30.       Row() { 
  31.         Button("8X8", { type: ButtonType.Circle, stateEffect: true }) 
  32.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  33.           .onClick(() => { this.startGame(8) }) 
  34.         Button("9X9", { type: ButtonType.Circle, stateEffect: true }) 
  35.           .width(70).height(70).backgroundColor(0x317aff).fontSize(20) 
  36.           .margin({left: 30}) 
  37.           .onClick(() => { this.startGame(9) }) 
  38.       }.alignItems(VerticalAlign.Center) 
  39.     } 
  40.     .width('100%'
  41.     .height('100%'
  42.   } 
  43.  
  44.   // 開始游戲 
  45.   startGame(idx:number) { 
  46.     router.push({ 
  47.       uri: 'pages/game'
  48.       params: {index: idx} 
  49.     }) 
  50.   } 

3.2. 游戲界面代碼,具體頁面布局在自定義組件實現:

3.2.1 Game代碼:

  1. import router from '@system.router' 
  2. import { Title } from '../common/game/title' 
  3. import { Body } from '../common/game/body' 
  4. import { Footer } from '../common/game/footer' 
  5. import { getRandomData } from '../utils/utils' 
  6.  
  7. @Entry 
  8. @Component 
  9. struct Game { 
  10.   // 接收主界面傳遞過來的陣列數字 
  11.   private idx: number = router.getParams().index 
  12.   @State index: number = this.idx 
  13.   // 調用函數隨機生成相應的字符數字數組 
  14.   @State numArray: String[] = getRandomData(this.idx) 
  15.   // 與body和footer子組件綁定, 變化時, body和footer子組件也會跟著變化 
  16.   @State time: number = 0 
  17.  
  18.   build() { 
  19.     Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 
  20.       // 標題和返回按鈕 
  21.       Title() 
  22.       // 游戲界面 
  23.       Body({idx: $index, numArray: $numArray, time: $time}) 
  24.       // 狀態框 
  25.       Footer({idx: $indextime: $time}) 
  26.     } 
  27.     .width('100%'
  28.     .height('100%'
  29.   } 

 3.3.2 游戲Title自定義組件代碼:

  1. import router from '@system.router' 
  2.  
  3. @Component 
  4. export struct Title { 
  5.   build() { 
  6.     Row() { 
  7.       // 返回游戲主界面 
  8.       Image($r("app.media.back")) 
  9.         .objectFit(ImageFit.Contain) 
  10.         .width(50) 
  11.         .height(50) 
  12.         .margin({ right: 10 }) 
  13.         .onClick(()=>{ this.onBack() }) 
  14.       Text("游戲開始"
  15.         .fontSize(24) 
  16.         .fontColor(Color.White) 
  17.         .fontWeight(FontWeight.Bold) 
  18.     } 
  19.     .width('100%'
  20.     .padding({ top: 10, bottom: 10}) 
  21.     .backgroundColor(0x317aff) 
  22.   } 
  23.   // 回退 
  24.   onBack() { 
  25.     router.back(); 
  26.   } 

3.2.3 游戲Body自定義組件代碼:

  1. @Component 
  2. export struct Body { 
  3.   // 與游戲父組件綁定, 記錄當前的陣列數字 
  4.   @Link idx: number; 
  5.   // 與游戲父組件綁定, 顯示相應的數字按鈕 
  6.   @Link numArray: String[]; 
  7.   // 與游戲父組件綁定, 變化時, 父組件time變量也跟著變化, 同時footer子組件也會跟著變化 
  8.   @Link time: number; 
  9.  
  10.   // 根據不同的陣列, 按鈕寬高顯示不同的大小 
  11.   private btnSize: number[] = [32, 18, 12, 8, 6, 4, 4] 
  12.   // 根據不同的陣列, 按鈕字段顯示不同大小 
  13.   private btnFont: number[] = [32, 24, 22, 12, 7, 8, 6] 
  14.   // 根據不同的陣列, 顯示不同界面高度 
  15.   private gridHeight: number[] = [48, 48, 48, 44, 46, 50, 66] 
  16.   // 根據不同的陣列, 顯示不同的行列 
  17.   private template: string[] = ['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''1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr''1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr'
  18.   // 記錄當前點擊的數字 
  19.   private flagNum: number = 1 
  20.   // 開始計時 
  21.   private startTime: number = new Date().getTime() 
  22.  
  23.   build() { 
  24.     Grid() { 
  25.       // 循環顯示陣列數字按鈕 
  26.       ForEach(this.numArray, (day: string) => { 
  27.         GridItem() { 
  28.           Button(day, { type: ButtonType.Circle, stateEffect: true }) 
  29.             .width(this.btnSize[this.idx-3] * this.idx) 
  30.             .height(this.btnSize[this.idx-3] * this.idx) 
  31.             .backgroundColor(0x317aff).fontSize(this.btnFont[this.idx-3]) 
  32.             .onClick(() => { this.startGame(Number(day)) }) 
  33.         } 
  34.       }, day => day
  35.     } 
  36.     // 根據相應的陣列數字,顯示相應的列數字 
  37.     .columnsTemplate(this.template[this.idx-3]) 
  38.     // 根據相應的陣列數字,顯示相應的行數字 
  39.     .rowsTemplate(this.template[this.idx-3]) 
  40.     .columnsGap(10) 
  41.     .rowsGap(10) 
  42.     .width(96+'%'
  43.     .height(this.gridHeight[this.idx-3]+'%'
  44.   } 
  45.  
  46.   // 開始游戲 
  47.   startGame(num:number) { 
  48.     // 如果當前點擊的數字等于陣列數組長度, 說明點擊到最后一個數字, 彈出挑戰成功, 計算出總共耗時 
  49.     if (num == this.numArray.length && this.flagNum == this.numArray.length ) { 
  50.       AlertDialog.show({ message: '恭喜您挑戰成功'}) 
  51.       this.time = (new Date().getTime() - this.startTime) * 1.0 / 1000 
  52.     } 
  53.  
  54.     // 如果點擊的數字大于累計的數字,彈出提醒信息 
  55.     if (num > this.flagNum) { 
  56.       AlertDialog.show({ message: '請點擊小于此數字'}) 
  57.     // 如果點擊的數字小于累計的數字,彈出提醒信息 
  58.     } else if (num < this.flagNum) { 
  59.       AlertDialog.show({ message: '當前點擊的數字,已點擊過'}) 
  60.     // 否則累計數字加1 
  61.     } else { 
  62.       this.flagNum++ 
  63.     } 
  64.   } 

 3.2.4 游戲Footer自定義組件代碼:

  1. @Component 
  2. export struct Footer { 
  3.   // 與game父組件綁定, 記錄當前的陣列數字 
  4.   @Link idx: number; 
  5.   // 與game父組件綁定, 變化時, 父組件time變量也跟著變化, 同時footer子組件也會跟著變化 
  6.   @Link time: number; 
  7.  
  8.   build() { 
  9.     Stack({ alignContent: Alignment.Bottom }) { 
  10.       Row() { 
  11.         // 耗時 
  12.         Button({ type: ButtonType.Capsule, stateEffect: false }) { 
  13.           Row() { 
  14.             Image($r('app.media.trophy')).width(20).height(20).margin({ left: 12 }) 
  15.             Text(this.time + '"').fontSize(16).fontColor(0xffffff).margin({ left: 5, right: 12 }) 
  16.           }.alignItems(VerticalAlign.Center).width(100) 
  17.         }.backgroundColor(0x317aff).opacity(0.7).width(100) 
  18.  
  19.         // 顯示計時中 
  20.         Button({ type: ButtonType.Capsule, stateEffect: false }) { 
  21.           Row() { 
  22.             Image($r('app.media.time')).width(20).height(20).margin({ left: 12 }) 
  23.             Text('計時中').fontSize(16).fontColor(0xffffff).margin({ left: 5, right: 12 }) 
  24.           }.alignItems(VerticalAlign.Center).width(100) 
  25.         }.backgroundColor(0x317aff).opacity(0.7).width(100) 
  26.         .margin({left: 20, right: 20}) 
  27.  
  28.         // 幫助功能 
  29.         Button({ type: ButtonType.Capsule, stateEffect: true }) { 
  30.           Row() { 
  31.             Image($r('app.media.help')).width(20).height(20).margin({ left: 12 }) 
  32.             Text('幫助').fontSize(16).fontColor(0xffffff).margin({ left: 5, right: 12 }) 
  33.           }.alignItems(VerticalAlign.Center).width(100) 
  34.         }.backgroundColor(0x317aff).width(100) 
  35.         .onClick(() => { this.showHelp() }) 
  36.  
  37.       } 
  38.     }.width('100%').height(100).margin({ top: 5, bottom: 10 }) 
  39.   } 
  40.  
  41.   // 提示游戲幫助 
  42.   showHelp() { 
  43.     AlertDialog.show({ message: '以最快速度從 1 選到 ' + (this.idx*this.idx) }) 
  44.   } 

3.3. Utils公共函數實現:

  1. /** 
  2.  * 隨機生成1-count參數的整數 
  3.  * @param idx 
  4.  */ 
  5. export function getRandomData(idx:number): Array<String> { 
  6.   // 生成count個數字 
  7.   let count:number = idx * idx; 
  8.   // 存儲生成的字符數字 
  9.   let result:Array<String> = []; 
  10.  
  11.   do { 
  12.     // 隨機生成一個指定范圍的數字 
  13.     let num = Math.floor(Math.random() * count + 1); 
  14.     // 如果數字不在數組里, 存儲到數組 
  15.     if (-1 == result.indexOf(num+'')) { 
  16.       result.push(num+''); 
  17.     } 
  18.  
  19.     // 如果隨機生成的數字存儲到數組的長度等于陣列數, 跳出死循環 
  20.     if (count == result.length) { 
  21.       break; 
  22.     } 
  23.  
  24.   }while(true
  25.   // 返回數組 
  26.   return result; 
  27. }; 

 **總結:**看到主界面和游戲界面代碼,是不是很簡潔,聲明式開發范式之美,那你還等什么?跟上步伐開始聲明式開發吧!!!

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2023-05-31 15:42:06

游戲開發關系型數據庫

2022-07-06 20:40:27

舒爾特方格鴻蒙

2024-01-11 15:54:55

eTS語言TypeScript應用開發

2022-07-13 16:24:12

ArkUI(JS)打地鼠游戲

2022-03-17 15:28:18

五子棋HarmonyOSJSAPI

2021-12-01 15:40:23

鴻蒙HarmonyOS應用

2022-08-04 13:55:08

拼數字小游戲鴻蒙

2022-08-22 17:28:34

ArkUI鴻蒙

2021-12-01 15:38:33

鴻蒙HarmonyOS應用

2022-10-19 15:12:05

ArkUI鴻蒙

2021-11-02 14:52:17

鴻蒙HarmonyOS應用

2024-05-31 08:43:31

2021-11-01 11:08:28

鴻蒙HarmonyOS應用

2022-05-27 14:55:34

canvas畫布鴻蒙

2022-10-24 14:49:54

ArkUI心電圖組件

2022-11-02 16:06:54

ArkUIETS

2022-08-25 21:41:43

ArkUI鴻蒙

2022-06-27 14:12:32

css鴻蒙自定義

2015-07-16 13:46:55

網絡虛擬化網絡虛擬
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线观看久草 | 久久青视频 | 国产精品视频偷伦精品视频 | 精品国产乱码久久久久久图片 | 国产亚洲一区二区三区 | 9久久精品 | 欧洲精品视频一区 | 成人二区 | 欧美久久久久久 | 欧美国产日韩一区二区三区 | 99免费在线观看视频 | 日韩免费网站 | 夜夜爽99久久国产综合精品女不卡 | 久久精品免费观看 | 国产一区二区三区在线 | 一级黄色大片 | 亚洲三级在线观看 | 国产日韩欧美一区 | 精品免费国产一区二区三区 | 在线a视频网站 | 91青娱乐在线 | 成人一区二区在线 | 福利视频一区二区 | 日韩欧美专区 | 久久久噜噜噜久久中文字幕色伊伊 | 国产精品久久久久久久久动漫 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 成人超碰在线 | 国产免费福利在线 | 亚洲日本欧美日韩高观看 | 一区二区三区在线免费看 | 韩日在线| 91av免费版| 日韩欧美亚洲综合 | 亚州一区二区三区 | 91精品国产乱码麻豆白嫩 | 成人一区二区在线 | 亚洲视频免费 | 国产精品99久久久久久宅男 | 色婷婷久久久亚洲一区二区三区 | 日韩在线资源 |