HarmonyOS鸿蒙Next应用开发对角数字游戏练习

HarmonyOS鸿蒙Next应用开发对角数字游戏练习 一、效果与说明

这是一个数字游戏项目,输入1~9数字到方格中完成横竖对角线相加之和都为15。

如下图:

图片

二、部分代码展示

export default {
  data: {
    result: '将1~9数字分别填入方格中使横竖对角线之和都为15',
    answer: []
  },
  onInit() {},
  one(e) {
    this.answer[0] = parseInt(e.value);
  },
  two(e) {
    this.answer[1] = parseInt(e.value);
  },
  three(e) {
    this.answer[2] = parseInt(e.value);
  },
  four(e) {
    this.answer[3] = parseInt(e.value);
  },
  five(e) {
    this.answer[4] = parseInt(e.value);
  },
  six(e) {
    this.answer[5] = parseInt(e.value);
  },
  seven(e) {
    this.answer[6] = parseInt(e.value);
  },
  eight(e) {
    this.answer[7] = parseInt(e.value);
  },
  nine(e) {
    this.answer[8] = parseInt(e.value);
  },
  submit() {
    if (this.answer[0] + this.answer[1] + this.answer[2] == 15 
      && this.answer[3] + this.answer[4] + this.answer[5] == 15
      && this.answer[6] + this.answer[7] + this.answer[8] == 15
      && this.answer[0] + this.answer[3] + this.answer[6] == 15
      && this.answer[1] + this.answer[4] + this.answer[7] == 15
      && this.answer[2] + this.answer[5] + this.answer[8] == 15
      && this.answer[0] + this.answer[4] + this.answer[8] == 15
      && this.answer[2] + this.answer[4] + this.answer[6] == 15) {
        this.result = '胜利'
    } else {
        this.result = '失败'
    }
  }
}

hml

<div style="display : flex; flex-direction : column; margin-top : 20px; margin-left : 100px;">
  <div style="width: 400px; height: 400px;">
    <label style="font-size: 50px;color: green;">{{result}}</label>
  </div>
  <div style="display : flex; flex-direction : row; margin-top : 40px; margin-bottom : 40px;">
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="one" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="two" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="three" maxlength="1" type="number" value="">
    </div>
  </div>
  <div style="display : flex; flex-direction : row; margin-bottom : 40px;">
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="four" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="five" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="six" maxlength="1" type="number" value="">
    </div>
  </div>
  <div style="display : flex; flex-direction : row; margin-bottom : 40px;">
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="seven" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="eight" maxlength="1" type="number" value="">
    </div>
    <div style="width : 100px; height : 100px; background-color : green; text-align : center; margin-right : 40px;">
      <input onchange="nine" maxlength="1" type="number" value="">
    </div>
  </div>
  <div style="width: 400px;height: 50px;">
    <input type="button" onclick="submit" value="submit">
  </div>
</div>
3 回复

是不是有些自定义的内容你没有填好哦

更多关于HarmonyOS鸿蒙Next应用开发对角数字游戏练习的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


感谢大佬分享!

能不能请教下SurfaceProvider的绘制问题

https://developer.huawei.com/consumer/cn/forum/topic/0202583757907400486?fid=0101303901040230869

实在是绘制不出来,不知道这一块要怎么弄

在HarmonyOS鸿蒙Next中开发对角数字游戏,首先需设计游戏逻辑,如生成随机数字矩阵并检查对角线数字是否相同。使用ArkTS语言编写UI组件,如Text显示数字,Button用于交互。通过@State管理游戏状态,@Link实现组件间数据绑定。利用ForEach循环渲染矩阵,if条件判断游戏胜负。最后,通过@Entry@Component构建应用入口和主界面,实现完整的游戏体验。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!