在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

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

3天內(nèi)不再提示

鴻蒙元服務實戰(zhàn)-笑笑五子棋(5)

萬少 ? 來源:jf_22972444 ? 作者:jf_22972444 ? 2025-03-31 09:36 ? 次閱讀

鴻蒙元服務實戰(zhàn)-笑笑五子棋(5)

來到最后一章了,這一章節(jié)講兩個部分。一是笑笑五子棋的卡片制作,二就是發(fā)布上架。

卡片介紹

Form Kit(卡片開發(fā)框架)提供了一種在桌面、鎖屏等系統(tǒng)入口嵌入顯示應用信息的開發(fā)框架和 API,可以將應用內(nèi)用戶關注的重要

信息或常用操作抽取到服務卡片(以下簡稱“卡片”)上,通過將卡片添加到桌面上,以達到信息展示、服務直達的便捷體驗效果。

image-20250105154516700

新建卡片

卡片類型分為兩種:

  1. 靜態(tài)卡片 功能稍弱
  2. 動態(tài)卡片 功能強一些

image-20250105154650672

  1. 選擇卡片的屬性
    image-20250105155715639
  2. 然后你就得到了以下文件
    image-20250105154810895

卡片文件解釋

EntryFormAbility.ets

entry/src/main/ets/entryformability/EntryFormAbility.ets

該文件可以定義卡片的生命周期,傳遞數(shù)據(jù)給卡片等

WidgetCard

entry/src/main/ets/widget/pages/WidgetCard.ets

該文件是卡片的主要展示和業(yè)務功能頁面。 卡片外觀、功能主要由它來提供

form_config.json

entry/src/main/resources/base/profile/form_config.json

該文件是卡片的配置文件,比如卡片的圖標、卡片的名字、卡片的種類等等都可以在這配置

獲取卡片寬度

卡片的 api 和元服務的 api 稍有區(qū)別,所以在開發(fā)的需要額外注意

這里在 entry/src/main/ets/entryformability/EntryFormAbility.ets 內(nèi),可以設置卡片創(chuàng)建的時獲取卡片的寬度

因為卡片有不同的規(guī)格尺寸,所以可以動態(tài)來獲取。

onAddForm(want: Want) {
    let formData: Record< string, number > = {
      "canwidth": px2vp((want.parameters?.[formInfo.FormParam.WIDTH_KEY] as number) * 2),
    };
    return formBindingData.createFormBindingData(formData);
  }

卡片中是無法使用 AppStorage,所以需要使用 Localstorage 來代替,進行數(shù)據(jù)傳遞

卡片中接收

@Entry
@Component
struct WidgetCard {
  @LocalStorageProp("canwidth")
  canwidth: number = 0
  @LocalStorageProp("canwidth")
  canheight: number = 0

完成卡片下棋邏輯

因為卡片的下棋邏輯和宿主-元服務本身幾乎一致。因此在實際開發(fā)中,可以將它們共同的邏輯抽離出來方便管理。這里就 cv 復用了。

@Entry
@Component
struct WidgetCard {
  @LocalStorageProp("canwidth")
  canwidth: number = 0
  @LocalStorageProp("canwidth")
  canheight: number = 0
  settings: RenderingContextSettings = new RenderingContextSettings(true);
  ctx: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  // 棋盤參數(shù)
  gridSize: number = 15;
  cellSize: number = this.canwidth / this.gridSize;
  radius: number = this.cellSize / 2 - 5; // 棋子的半徑
  // 棋盤數(shù)據(jù)
  board: number[][] = []
  currentPlayer: number = 1; // 當前玩家 (1: 黑子, 2: 白子)
  gameOver: boolean = false;
  @State
  textContent: string = ""
  // 處理玩家落子
  handleClick = async (event: ClickEvent) = > {
    if (this.gameOver) {
      return;
    }

    const x = event.x;
    const y = event.y;

    const col = Math.floor(x / this.cellSize);
    const row = Math.floor(y / this.cellSize);

    if (this.board[row] && this.board[row][col] === 0) {
      this.board[row][col] = this.currentPlayer;
      this.drawBoard();

      if (this.checkWin(row, col)) {
        this.textContent = this.currentPlayer === 1 ? '黑子勝利!' : '白子勝利!';
        this.gameOver = true;
        // AlertDialog.show({ message: this.textContent })


      } else {
        this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
        this.textContent = this.currentPlayer === 1 ? '輪到黑子落子' : '輪到白子落子';
      }
    } else {
      // promptAction.showToast({ message: `請點擊中棋盤對位位置` })
    }
  }

  aboutToAppear(): void {

  }

  // 繪制棋盤
  drawBoard = () = > {
    this.ctx.clearRect(0, 0, this.canwidth, this.canwidth);

    // 繪制網(wǎng)格
    this.ctx.strokeStyle = "#000";
    this.ctx.lineWidth = 1;
    for (let i = 0; i < this.gridSize; i++) {
      this.ctx.beginPath();
      this.ctx.moveTo(this.cellSize * i, 0);
      this.ctx.lineTo(this.cellSize * i, this.canwidth);
      this.ctx.stroke();

      this.ctx.beginPath();
      this.ctx.moveTo(0, this.cellSize * i);
      this.ctx.lineTo(this.canwidth, this.cellSize * i);
      this.ctx.stroke();
    }

    // 繪制已落的棋子
    for (let row = 0; row < this.gridSize; row++) {
      for (let col = 0; col < this.gridSize; col++) {
        if (this.board[row][col] !== 0) {
          this.ctx.beginPath();
          this.ctx.arc(col * this.cellSize + this.cellSize / 2, row * this.cellSize + this.cellSize / 2, this.radius, 0,
            2 * Math.PI);
          this.ctx.fillStyle = this.board[row][col] === 1 ? 'black' : 'white';
          this.ctx.fill();
          this.ctx.stroke();
        }
      }
    }
  }
  // 判斷是否有五子連珠
  checkWin = (row: number, col: number) = > {
    interface abc {
      dr: number
      dc: number
    }

    const directions: abc[] = [
      { dr: 0, dc: 1 }, // 水平
      { dr: 1, dc: 0 }, // 垂直
      { dr: 1, dc: 1 }, // 主對角線
      { dr: 1, dc: -1 }// 副對角線
    ];

    for (let i = 0; i < directions.length; i++) {
      const dr = directions[i].dr
      const dc = directions[i].dc
      let count = 1;

      // 向一個方向檢查
      for (let i = 1; i < 5; i++) {
        let r = row + dr * i;
        let c = col + dc * i;
        if (r >= 0 && r < this.gridSize && c >= 0 && c < this.gridSize && this.board[r][c] === this.currentPlayer) {
          count++;
        } else {
          break;
        }
      }

      // 向另一個方向檢查
      for (let i = 1; i < 5; i++) {
        let r = row - dr * i;
        let c = col - dc * i;
        if (r >= 0 && r < this.gridSize && c >= 0 && c < this.gridSize && this.board[r][c] === this.currentPlayer) {
          count++;
        } else {
          break;
        }
      }

      // 如果連續(xù)五個相同的棋子,則勝利
      if (count >= 5) {
        return true;
      }
    }

    return false;
  }
  // 初始化游戲
  initGame = () = > {
    this.board = []
    for (let index = 0; index < this.gridSize; index++) {
      const arr: number[] = []
      for (let index2 = 0; index2 < this.gridSize; index2++) {
        arr.push(0)

      }
      this.board.push(arr)

    }
    this.currentPlayer = 1;
    this.gameOver = false;
    this.textContent = '輪到黑子落子';
    this.drawBoard();
  }

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Canvas(this.ctx)
        .width(this.canwidth)
        .height(this.canwidth)
        .backgroundColor(Color.Orange)
        .onReady(() = > {
          this.cellSize = this.canwidth / this.gridSize;
          this.radius = this.cellSize / 2 - 5; // 棋子的半徑
          this.initGame()
        })
        .onClick(
          this.handleClick
        )

      Text(this.textContent)
        .fontSize(14)
        .padding(5)
        .fontColor(Color.White)
        .fontWeight(700)

    }
    .width("100%")
    .height("100%")
  }
}

調(diào)整卡片的圖標和名字

主要業(yè)務開發(fā)完畢了,可以調(diào)整卡片的展示信息

image-20250105160500387

這部分信息在 entry/src/main/resources/base/profile/form_config.json中配置:

  1. displayName 標題
  2. description 簡介
{
  "forms": [
    {
      "name": "widget",
      "displayName": "$string:widget_display_name",
      "description": "$string:widget_desc",
      "src": "./ets/widget/pages/WidgetCard.ets",
      "uiSyntax": "arkts",
      "window": {
        "designWidth": 720,
        "autoDesignWidth": true
      },
      "colorMode": "auto",
      "isDynamic": true,
      "isDefault": true,
      "updateEnabled": false,
      "scheduledUpdateTime": "10:30",
      "updateDuration": 1,
      "defaultDimension": "4*4",
      "supportDimensions": [
        "2*2",
        "4*4"
      ]
    }
  ]
}

發(fā)布上架

最后,如果要將卡片發(fā)布上架,還需要做一些小處理

  1. 設置你的元服務的展示圖標
  2. 配置證書
  3. 打包成 Hap
  4. 在 AGC 平臺上發(fā)布上架等等
  5. 具體流程可以參考底部的文章

參考鏈接

  1. 卡片開發(fā)
  2. [HarmonyOS Next 實戰(zhàn)卡片開發(fā) 01](https://juejin.cn/post/7432314654287872010)
  3. [HarmonyOS Next 實戰(zhàn)卡片開發(fā) 02](https://juejin.cn/post/7433035654400245811)
  4. [HarmonyOS Next 實戰(zhàn)卡片開發(fā) 03](https://juejin.cn/post/7433422988358189107)
  5. [HarmonyOS Next 最新 元服務新建到上架全流程](https://juejin.cn/post/7441761663701041161)

代碼倉庫

https://gitee.com/ukSir/laughing-at-gomoku

總結(jié)

至此,笑笑五子棋的開發(fā)上架流程已經(jīng)完畢。

如果你興趣想要了解更多的鴻蒙應用開發(fā)細節(jié)和最新資訊,歡迎在評論區(qū)留言或者私信或者看我個人信息,可以加入技術(shù)交流群。

審核編輯 黃宇

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

    關注

    2

    文章

    1537

    瀏覽量

    63023
  • Canvas
    +關注

    關注

    0

    文章

    20

    瀏覽量

    11105
  • 鴻蒙
    +關注

    關注

    57

    文章

    2457

    瀏覽量

    43447
收藏 人收藏

    評論

    相關推薦

    labview五子棋程序

    本帖最后由 桂花酒 于 2012-6-10 13:43 編輯 labview五子棋程序,想用的可以看看!本來是發(fā)的求助帖,找五子棋程序,不過剛才下了一個,鑒于論壇上現(xiàn)在已經(jīng)搜不著了(我原來下過,不過刪了)一起學習!
    發(fā)表于 06-10 13:16

    五子棋的棋盤怎么么做

    做課程設計,五子棋的棋盤怎么做
    發(fā)表于 07-02 23:18

    五子棋主程序前面板怎么設計

    五子棋主程序前面板怎么設計?后面版有程序了
    發(fā)表于 07-03 16:39

    用Verilog寫的一個五子棋

    在期末EDA課答辯之際寫了一個五子棋的代碼,主要用到了VGA和PS2接口的外設,因為在自己敲的時候來網(wǎng)上找過,沒找到有關的資料,所以自己寫好之后就想發(fā)上來和大家討論下。基本實現(xiàn)了雙人對戰(zhàn)五子棋的功能。感覺有很多紕漏,想請大家指點下。
    發(fā)表于 06-15 14:40

    五子棋

    求一個五子棋的程序
    發(fā)表于 06-26 16:09

    五子棋

    自己做的五子棋,說不上有什么難的
    發(fā)表于 08-12 21:44

    labview 虛擬五子棋

    `一個簡易的五子棋程序,喜歡的可以借鑒下`
    發(fā)表于 06-16 21:57

    怎樣去設計一種人機對弈五子棋程序

    五子棋游戲應達到幾方面的要求?怎樣去設計一種人機對弈五子棋程序?
    發(fā)表于 09-29 07:26

    五子棋程序源代碼-Matlab實現(xiàn)

    五子棋程序源代碼-M
    發(fā)表于 07-04 16:15 ?50次下載

    用java語言編寫的智能五子棋源程序

    用java語言編寫的智能五子棋源程序
    發(fā)表于 10-30 10:31 ?98次下載
    用java語言編寫的智能<b class='flag-5'>五子棋</b>源程序

    C語言五子棋

    C語言五子棋,有趣的東西,值得玩玩,不用看著黑框發(fā)呆
    發(fā)表于 01-12 16:49 ?2次下載

    Delphi教程_使用DrawGrid控件制作五子棋

    Delphi教程使用DrawGrid控件制作五子棋,很好的Delphi的學習資料。
    發(fā)表于 03-16 14:55 ?19次下載

    C語言教程之五子棋游戲的問題

    C語言教程之五子棋游戲的問題,很好的C語言資料,快來學習吧。
    發(fā)表于 04-25 17:07 ?0次下載

    基于LabVIEW的五子棋博弈算法

    針對目前五子棋人機對弈多數(shù)基于電腦、手機,缺少真實環(huán)境的問題,提出一種基于LabVIEW的博弈算法,并運用于真實的五子棋人機對弈。首先通過圖像采集系統(tǒng)獲取當前狀態(tài)下棋盤及人機雙方棋子的位置信息;然后
    發(fā)表于 12-17 11:32 ?29次下載

    鴻蒙服務實戰(zhàn)-笑笑五子棋(1)

    鴻蒙服務實戰(zhàn)-笑笑五子棋(1) 前言 作為鴻蒙應用的深度開發(fā)者都應該知道,經(jīng)歷了 波瀾壯闊 1
    的頭像 發(fā)表于 03-31 09:23 ?71次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>元</b><b class='flag-5'>服務實戰(zhàn)</b>-<b class='flag-5'>笑笑</b><b class='flag-5'>五子棋</b>(1)
    主站蜘蛛池模板: 色片免费网站 | 国产性夜夜春夜夜爽 | 五月婷婷激情在线 | 爱夜夜性夜夜夜夜夜夜爽 | 狼人射综合 | 日韩有码电影 | 国模视频在线 | 五月婷婷六月色 | 2021国产精品 | 资源视频在线观看 | 国产人成午夜免视频网站 | 看黄视频网站 | 中文字幕一区二区三区永久 | 精品久久香蕉国产线看观看亚洲 | 欧美伊人久久综合网 | 亚洲综合色视频 | 午夜视频免费 | 丁香六月婷婷综合 | 青青伊人91久久福利精品 | 国产一区二区三区美女在线观看 | 欧美精品亚洲网站 | avtt香蕉| 噜噜色噜噜| 加勒比一本大道香蕉在线视频 | 泰剧天堂 | 婷婷六月激情在线综合激情 | 中国xxxxx高清免费看视频 | 天天干天天操天天拍 | 极品色天使在线婷婷天堂亚洲 | 寄宿日记免费看 | 男人午夜禁片在线观看 | 玖操在线| 天堂网在线播放 | 六月激情丁香 | 四虎影院的网址 | 国产午夜精品久久理论片小说 | 在线三区| 欧美成人午夜不卡在线视频 | 又粗又爽又色男女乱淫播放男女 | luxu259在线中文字幕 | 色视频免费版高清在线观看 |