国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

鴻湖萬聯“競”開發板體驗:基于eTSUI框架的2048小游戲

電子發燒友開源社區 ? 來源:未知 ? 2022-12-06 12:10 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

前言

2048是一款比較流行的數字游戲,本demo基于ets ui框架,在grid組件基礎上實現。

過程

從以下地址下載代碼:https://gitee.com/qinyunti/knowledge_demo_smart_home.git

打開DevEco Studio 3.0.0.993

c541cfc2-751b-11ed-8abf-dac502259ad0.png

打開工程:

c5658dfe-751b-11ed-8abf-dac502259ad0.png

c5815a2a-751b-11ed-8abf-dac502259ad0.png

更新:

c5a7bfe4-751b-11ed-8abf-dac502259ad0.png

點擊Run,有如下提示按圖配置即可:

c5b8877a-751b-11ed-8abf-dac502259ad0.png

c5c81dde-751b-11ed-8abf-dac502259ad0.png

代碼分析

程序入口:src/main/ets/MainAbility/app.ets

import Logger from '../MainAbility/model/Logger'


const TAG: string = `[App]`


export default {
onCreate() {
Logger.info(TAG, `Application onCreate`)
},
onDestroy() {
Logger.info(TAG, `Application onDestroy`)
},
}

(左右移動查看全部內容)

邏輯代碼位于:src/main/ets/MainAbility/model/GameRule.ts

//gameStatus
enum GameStatus {
  BEFORE = -1,
  RUNNING = 1,
  OVER = 0
}


export class GameRule {
  private row: number = 4
  private column: number = 4


  private index(i: number, j: number) {
    return i * this.row + j
  }


  dataNumbers: number[]
  status: number = GameStatus.BEFORE
  score: number = 0


  constructor(dataNumbers: number[]) {
    this.dataNumbers = dataNumbers
  }


  //random
  randomNum() {
    do {
      let a = Math.floor(Math.random() * this.dataNumbers.length)
      if (this.dataNumbers[a] === 0) {
        let num = Math.random() > 0.3 ? 2 : 4
        this.dataNumbers[a] = num
        break
      }
    } while (this.dataNumbers.some((val) => {
      return val === 0
    }))
  }


  //init
  init() {
    this.dataNumbers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    this.status = GameStatus.RUNNING
    this.score = 0
    this.randomNum()
    this.randomNum()
  }


  //move
  move(direction: string) {
    let before = String(this.dataNumbers)
    let length = (direction === 'left' || direction === 'right') ? this.row : this.column
    for (let a = 0;a < length; a++) {
      this.moveInRow(direction, a)
    }
    let after = String(this.dataNumbers)
    if (before !== after) {
      this.randomNum()
      if (this.isGameOver()) {
        this.status = GameStatus.OVER
      }
    }
  }


  //prepare to move
  moveInRow(direction: string, a: number) {
    if (direction === 'left') {
      for (let b = 0;b < this.column - 1; b++) {
        let next = this.moveNext(a, b, direction)
        b = this.dataChange(a, b, next, direction).b
        if (next === -1) break
      }
    } else if (direction === 'right') {
      for (let b = this.column - 1;b > 0; b--) {
        let next = this.moveNext(a, b, direction)
        b = this.dataChange(a, b, next, direction).b
        if (next === -1) break
      }
    } else if (direction === 'up') {
      for (let b = 0;b < this.row - 1; b++) {
        let next = this.moveNext(b, a, direction)
        b = this.dataChange(b, a, next, direction).a
        if (next === -1) break
      }
    } else if (direction === 'down') {
      for (let b = this.row - 1;b > 0; b--) {
        let next = this.moveNext(b, a, direction)
        b = this.dataChange(b, a, next, direction).a
        if (next === -1) break
      }
    }
  }


  //new number moveStatus
  moveNext(a: number, b: number, direction: string) {
    if (direction === 'left') {
      for (let i = b + 1;i < this.column; i++) {
        if (this.dataNumbers[this.index(a, i)] !== 0) {
          return i
        }
      }
    } else if (direction === 'right') {
      for (let i = b - 1;i >= 0; i--) {
        if (this.dataNumbers[this.index(a, i)] !== 0) {
          return i
        }
      }
    } else if (direction === 'up') {
      for (let i = a + 1;i < 4; i++) {
        if (this.dataNumbers[this.index(i, b)] !== 0) {
          return i
        }
      }
    } else if (direction === 'down') {
      for (let i = a - 1;i >= 0; i--) {
        if (this.dataNumbers[this.index(i, b)] !== 0) {
          return i
        }
      }
    }
    return -1
  }


  //get gameStatus
  isGameOver() {
    for (let a = 0;a < this.row; a++) {
      for (let b = 0;b < this.column; b++) {
        let tempA = this.index(a, b)
        if (this.dataNumbers[tempA] === 0) {
          return false
        }
        if (a < this.row - 1) {
          if (this.dataNumbers[tempA] === this.dataNumbers[this.index(a + 1, b)]) {
            return false
          }
        }
        if (b < this.column - 1) {
          if (this.dataNumbers[tempA] === this.dataNumbers[tempA+1]) {
            return false
          }
        }
      }
    }
    return true
  }


  //move and merge
  dataChange(a: number, b: number, next: number, direction: string) {
    let tempA = this.index(a, b)
    let tempB = 0
    if (direction === 'left' || direction === 'right') {
      tempB = this.index(a, next)
    } else {
      tempB = this.index(next, b)
    }
    if (next !== -1) {
      if (this.dataNumbers[tempA] === 0) {
        this.dataNumbers[tempA] = this.dataNumbers[tempB]
        this.dataNumbers[tempB] = 0
        direction === 'left' && b--
        direction === 'right' && b++
        direction === 'up' && a--
        direction === 'down' && a++
      } else if (this.dataNumbers[tempA] === this.dataNumbers[tempB]) {
        this.dataNumbers[tempA] *= 2
        this.score += this.dataNumbers[tempA]
        this.dataNumbers[tempB] = 0
      }
    }
    return {
      a, b
    }
  }

(左右移動查看全部內容)

繪圖位于:src/main/ets/MainAbility/pages/Game2048.ets

class BasicDataSource implements IDataSource {
 private listeners: DataChangeListener[] = []


 public totalCount(): number {
  return 0
 }


 public getData(index: number): any {
  return undefined
 }


 registerDataChangeListener(listener: DataChangeListener): void {
  if (this.listeners.indexOf(listener) < 0) {
   this.listeners.push(listener)
  }
 }


 unregisterDataChangeListener(listener: DataChangeListener): void {
  const pos = this.listeners.indexOf(listener);
  if (pos >= 0) {
   this.listeners.splice(pos, 1)
  }
 }


 notifyDataReload(): void {
  this.listeners.forEach(listener => {
   listener.onDataReloaded()
  })
 }


 notifyDataAdd(index: number): void {
  this.listeners.forEach(listener => {
   listener.onDataAdded(index)
  })
 }


 notifyDataChange(index: number): void {
  this.listeners.forEach(listener => {
   listener.onDataChanged(index)
  })
 }
}


class MyDataSource extends BasicDataSource {
 public dataArray: string[] = []


 constructor(ele) {
  super()
  for (var index = 0;index < ele.length; index++) {
   this.dataArray.push(ele[index])
  }
 }


 public totalCount(): number {
  return this.dataArray.length
 }


 public getData(index: number): any {
  return this.dataArray[index]
 }


 public addData(index: number, data: string): void {
  this.dataArray.splice(index, 0)
  this.notifyDataAdd(index)
 }
}


enum GameStatus {
 BEFORE = -1,
 RUNNING = 1,
 OVER = 0
}


import display from '@ohos.display'
import Logger from '../model/Logger'
import dataStorage from '@ohos.data.storage'
import { GameRule } from '../model/GameRule'
import featureAbility from '@ohos.ability.featureAbility'


const TAG = '[Game2048]'


@Entry
@Component
struct Game2048 {
 @State dataNumbers: number[] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 private gameRule: GameRule = new GameRule(this.dataNumbers)
 @State maxScore: number = 123456
 @State curScore: number = 0
 @State @Watch("onGameOver") gameStatus: number = GameStatus.BEFORE
 @State textColor: string[] = ['#f0fff0', '#eee3da', '#ede0c8', '#f2b179', '#f59563', '#f67c5f', '#f65e3b', '#edcf72', '#edcc61', '#9c0', '#33b5e5', '#09c', '#a6c', '#93c']
 dialogController: CustomDialogController = new CustomDialogController({
  builder: ScorePannel({
   curScore: this.curScore,
   maxScore: this.maxScore,
   gameStart: this.gameStart.bind(this)
  }),
  autoCancel: false
 })
 @State screenSize: {
  x: number,
  y: number
 } = { x: px2vp(1080), y: px2vp(0) }


 //gameStatus listener
 onGameOver() {
  if (this.gameStatus === GameStatus.OVER) {
   this.curScore = this.gameRule.score
   this.dialogController.open()
  }
 }


 //restart game
 gameStart() {
  this.gameRule.init()
  this.dataNumbers = this.gameRule.dataNumbers
  this.gameStatus = GameStatus.RUNNING
  this.handleLocalData('put')
 }


 //dataView
 dataView() {
  this.dataNumbers = this.gameRule.dataNumbers
  this.gameStatus = this.gameRule.status
  this.curScore = this.gameRule.score
 }


 aboutToAppear() {
  display.getDefaultDisplay((err, data) => {
   if (data.height > data.width) {
    this.screenSize = { x: px2vp(data.width), y: px2vp(data.height) }
   } else {
    this.screenSize = { x: px2vp(750), y: px2vp(data.width) }
   }
   Logger.info(TAG, `寬 ${this.screenSize.x}`)
   Logger.info(TAG, `高 ${this.screenSize.y}`)
  })
  this.handleLocalData('has')
 }


 //handle local data
 handleLocalData(method: string) {
  let context = featureAbility.getContext()
  context.getFilesDir((err, path) => {
   let storage = dataStorage.getStorageSync(path + '/mystore')
   if (method === 'put') {
    storage.putSync('gameData', JSON.stringify(this.dataNumbers))
    let score: string = this.gameRule.score.toString()
    storage.putSync('score', score)
    storage.putSync('gameStatus', this.gameRule.status.toString())
    storage.flushSync()
   } else if (method === 'has') {
    if (storage.hasSync('gameData')) {
     this.gameRule.score = this.curScore = Number(storage.getSync('score', 'string'))
     this.gameStatus = this.gameRule.status = Number(storage.getSync('gameStatus', 'string'))
     this.dataNumbers = this.gameRule.dataNumbers = JSON.parse(storage.getSync('gameData', 'string').toString())
    }
   }
  })
 }


 build() {
  Column() {
   Column() {
    Row() {
     Image($r('app.media.logo2048'))
      .width(this.screenSize.x * 0.25)
      .height(this.screenSize.x * 0.25)


     Column() {
      Text('Score')
       .fontSize('30px')
       .fontColor('#efe1d3')
       .fontWeight(FontWeight.Bolder)


      Text(`${this.gameRule.score}`)
       .fontSize('30px')
       .fontColor('#fcf8f5')
       .fontWeight(FontWeight.Bolder)
     }
     .alignItems(HorizontalAlign.Center)
     .justifyContent(FlexAlign.Center)
     .backgroundColor('#bbada0')
     .width(this.screenSize.x * 0.25)
     .height(this.screenSize.x * 0.25)
     .borderRadius(15)


     Column() {
      Text('Max')
       .fontSize('50px')
       .fontColor('#efe1d3')
       .fontWeight(FontWeight.Bolder)


      Text(`${this.maxScore}`)
       .fontSize('30px')
       .fontColor('#fcf8f5')
       .fontWeight(FontWeight.Bolder)
     }
     .alignItems(HorizontalAlign.Center)
     .justifyContent(FlexAlign.Center)
     .backgroundColor('#bbada0')
     .width(this.screenSize.x * 0.25)
     .height(this.screenSize.x * 0.25)
     .borderRadius(15)
    }
    .alignItems(VerticalAlign.Center)
    .justifyContent(FlexAlign.SpaceAround)
    .margin({ bottom: 20 })
    .width(this.screenSize.x)


    Grid() {
     LazyForEach(new MyDataSource(this.dataNumbers), (item) => {
      GridItem() {
       Text(`${item === 0 ? '' : item}`)
        .fontSize('85px')
        .fontColor(item <= 4 ? '#000' : '#fcf8f5')
        .fontWeight(FontWeight.Bolder)
        .backgroundColor('#f0fff0')
        .width('100%')
        .height('100%')
        .textAlign(TextAlign.Center)
        .borderRadius(10)
        .backgroundColor(this.textColor[(Math.log(item) / Math.log(2))])
      }
     })
    }
    .columnsTemplate('1fr 1fr 1fr 1fr')
    .rowsTemplate('1fr 1fr 1fr 1fr')
    .columnsGap(10)
    .rowsGap(10)
    .width(this.screenSize.x)
    .padding(10)
    .backgroundColor('rgba(80,69,46,0.26)')
    .height(this.screenSize.x)
    .borderRadius(10)
    .gesture(GestureGroup(GestureMode.Exclusive,
    PanGesture({ direction: PanDirection.Left }).onActionEnd(() => {
     this.gameRule.status === 1 && this.gameRule.move('left')
     this.dataView()
     this.handleLocalData('put')
    }),
    PanGesture({ direction: PanDirection.Right }).onActionEnd(() => {
     this.gameRule.status === 1 && this.gameRule.move('right')
     this.dataView()
     this.handleLocalData('put')
    }),
    PanGesture({ direction: PanDirection.Up }).onActionEnd(() => {
     this.gameRule.status === 1 && this.gameRule.move('up')
     this.dataView()
     this.handleLocalData('put')
    }),
    PanGesture({ direction: PanDirection.Down }).onActionEnd(() => {
     this.gameRule.status === 1 && this.gameRule.move('down')
     this.dataView()
     this.handleLocalData('put')
    })
    ))


    if (this.gameStatus === -1) {
     Button('Start', { type: ButtonType.Normal })
      .borderRadius(5)
      .margin({ top: 50 })
      .width(200)
      .key('startGame')
      .onClick(() => {
       this.gameStart()
      })
    }
   }
   .alignItems(HorizontalAlign.Center)
   .justifyContent(FlexAlign.Center)
   .height('100%')
   .width('100%')
  }
  .alignItems(HorizontalAlign.Center)
  .justifyContent(FlexAlign.Start)
  .width('100%')
  .height('100%')
  .backgroundImage($r('app.media.gridBackground'))
  .backgroundImageSize(ImageSize.Cover)
 }
}


@CustomDialog
struct ScorePannel {
 controller: CustomDialogController
 gameStart: () => void
 curScore: number
 maxScore: number


 build() {
  Column() {
   Text('Game Over')
    .fontSize(30)
    .fontWeight(FontWeight.Medium)
    .margin({ top: 10 })


   Text('Score')
    .fontColor('#C8A584')
    .fontSize(20)
    .margin({ top: 10 })


   Text(`${this.curScore}`)
    .fontColor('#5D5D5D')
    .fontSize(40)
    .margin({ top: 10 })


   Text(`maxScore:${this.maxScore}`)
    .fontSize(20)
    .width('90%')
    .borderRadius(20)
    .margin({ top: 10 })
    .height(40)
    .textAlign(TextAlign.Center)


   Row() {
    Button('Reset', { type: ButtonType.Normal })
     .borderRadius(5)
     .margin({ top: 10 })
     .width(200)
     .onClick(() => {
      this.gameStart()
      this.controller.close()
     })
   }.justifyContent(FlexAlign.SpaceAround)
   .margin({ top: 10, bottom: 10 })
  }
  .backgroundColor('#f0f0f0')
  .borderRadius(25)
 }
}

(左右移動查看全部內容)

總結

基于eTS UI框架能夠,基于各種組件進行快速的UI應用開發。

更多熱點文章閱讀

  • LiteOS-A內核中的procfs文件系統分析
  • 移植speexdspOpenHarmony標準系統②
  • 移植speexdsp到OpenHarmony標準系統③
  • 移植speexdsp到OpenHarmony標準系統④
  • 基于OpenHarmony的智能門禁系統,讓出行更便捷

提示:本文由電子發燒友社區發布,轉載請注明以上來源。如需社區合作及入群交流,請添加微信EEFans0806,或者發郵箱liuyong@huaqiu.com。


原文標題:鴻湖萬聯“競”開發板體驗:基于eTSUI框架的2048小游戲

文章出處:【微信公眾號:電子發燒友開源社區】歡迎添加關注!文章轉載請注明出處。


聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 電子發燒友
    +關注

    關注

    34

    文章

    591

    瀏覽量

    34133
  • 開源社區
    +關注

    關注

    1

    文章

    95

    瀏覽量

    798

原文標題:鴻湖萬聯“競”開發板體驗:基于eTSUI框架的2048小游戲

文章出處:【微信號:HarmonyOS_Community,微信公眾號:電子發燒友開源社區】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    軟通動力攜手與軟通教育斬獲GIIC聯盟鴻蒙生態推委會七項榮譽

    1月28日,全球智慧物聯網聯盟(GIIC)鴻蒙生態推委會委員大會暨2025生態伙伴年會在深圳圓滿召開,軟通動力攜子公司、軟通教育共同出席大會。
    的頭像 發表于 01-30 11:10 ?300次閱讀

    軟通動力旗下亮相2025鴻蒙生態人才大會

    核心共建企業,軟通動力旗下受邀參會,通過深度參與大會發布活動、主題演講及成果展示,全方位呈現了其在技術落地、產教融合與生態協同方面的綜合實力。
    的頭像 發表于 12-31 09:50 ?388次閱讀
    軟通動力旗下<b class='flag-5'>鴻</b><b class='flag-5'>湖</b><b class='flag-5'>萬</b><b class='flag-5'>聯</b>亮相2025鴻蒙生態人才大會

    軟通動力旗下榮膺開源鴻蒙共建貢獻TOP單位

    11月28日,以“在一起,就可以”為主題的鴻蒙星光盛典生態論壇于深圳召開。在這場開源生態的年度盛會上,軟通動力及子公司,作為開源鴻蒙的核心共建單位受邀出席,榮膺“共建貢獻TOP
    的頭像 發表于 12-01 14:31 ?513次閱讀

    軟通動力子公司亮相2025高交會

    11月14日,第二十七屆中國國際高新技術成果交易會(簡稱“高交會”)在深圳國際會展中心盛大啟幕。在這場匯聚全球頂尖創新力量的國家級科技盛會上,軟通動力子公司
    的頭像 發表于 11-19 17:22 ?770次閱讀

    軟通動力攜手亮相2025開源鴻蒙技術大會

    徑。作為OpenHarmony項目群A類捐贈人,由軟通動力高級副總裁、
    的頭像 發表于 10-09 09:28 ?900次閱讀

    【作品合集】潤開HH-SCDAYU800A開發板測評

    潤開HH-SCDAYU800A開發板測評作品合集 產品介紹: HH-SCDAYU800A 基于平頭哥 TH1520 芯片,支持OpenHarmony 標準系統,版本為3.2/4.1;支持藍牙
    發表于 09-19 14:09

    基于DE1-SOC開發板的太空射擊游戲

    今天繼續常春藤名校之一——康奈爾大學的FPGA課程ECE 5760典型案例分享:基于DE1-SOC開發板的太空射擊游戲
    的頭像 發表于 08-04 10:47 ?3772次閱讀
    基于DE1-SOC<b class='flag-5'>開發板</b>的太空射擊<b class='flag-5'>游戲</b>

    軟通動力攜手亮相2025開放原子開源生態大會

    7月23日至24日,以“開源共融,智創未來”為主題的2025開放原子開源生態大會在北京隆重召開。作為開源生態的核心共建者,軟通動力攜子公司深度參與本次大會,并榮獲重要榮譽。軟通
    的頭像 發表于 07-25 14:43 ?1049次閱讀

    開發板深度體驗:從開源鴻蒙開發到AI場景實踐

    開發板KaihongBoard-3588S-SBCKaihongBoard-3576-SBC體驗開源鴻蒙能力學習開源鴻蒙開發●●●搭載KaihongOS
    的頭像 發表于 07-03 17:03 ?1615次閱讀
    開<b class='flag-5'>鴻</b><b class='flag-5'>開發板</b>深度體驗:從開源鴻蒙<b class='flag-5'>開發</b>到AI場景實踐

    【潤開HH-SCDAYU800A開發板試用體驗】潤開HH-SCDAYU800A開發板(二)

    (三)潤開HH-SCDAYU800A開發板固件燒錄 注意:本燒錄方法僅可用于Windows10及之前版本。 1.安裝USB驅動 1.1下載潤開HH-SCDAYU800A開發板燒錄
    發表于 07-02 09:30

    軟通動力攜手亮相華為開發者大會2025

    近日,華為開發者大會 2025在東莞松山盛大舉行,軟通動力攜子公司深度參與會議。作為開
    的頭像 發表于 06-26 14:15 ?1106次閱讀

    亮相2025開源鴻蒙開發者大會

    近日,開源鴻蒙開發者大會2025在深圳隆重開幕。本次大會由開源鴻蒙項目群工作委員會主辦,華為、潤開、中軟國際、開智谷、九
    的頭像 發表于 05-28 16:09 ?1549次閱讀

    軟通動力攜手亮相華為中國合作伙伴大會2025

    此前,3月20日至21日,華為中國合作伙伴大會2025在深圳盛大舉行。作為鉆石級贊助商,軟通動力攜子公司及旗下眾多品牌強勢亮相。在這場盛會上,
    的頭像 發表于 03-24 15:53 ?1298次閱讀

    軟通動力攜手亮相2025武漢鴻蒙產業發展交流會

    近日,以“鴻蒙生態 共筑未來”為主題的2025武漢鴻蒙產業發展交流會在武漢圓滿舉行。本次活動聚焦產業政策發布及鴻蒙生態與區域產業的深度融合,吸引了政產學研用等多方力量參會。作為鴻蒙生態核心共建單位,軟通動力及其子公司
    的頭像 發表于 03-20 15:51 ?1108次閱讀

    北京迅為RK3568開發板OpenHarmony系統南向驅動開發內核HDF驅動框架架構

    北京迅為RK3568開發板OpenHarmony系統南向驅動開發內核HDF驅動框架架構
    的頭像 發表于 03-11 14:13 ?1856次閱讀
    北京迅為RK3568<b class='flag-5'>開發板</b>OpenHarmony系統南向驅動<b class='flag-5'>開發</b>內核HDF驅動<b class='flag-5'>框架</b>架構