当前位置: 首页 > wzjs >正文

最实用的手机app软件郑州做网站优化电话

最实用的手机app软件,郑州做网站优化电话,在什么网站可以自承包活来做,建设银行理财网站1. 引言 在本案例中,我们将使用鸿蒙NEXT框架开发一个简单的记忆翻牌游戏。该游戏的核心逻辑是玩家通过翻转卡片来寻找匹配的对。本文将详细介绍游戏的实现过程,包括卡片的展示、匹配逻辑以及用户交互。 2. 开发环境准备 电脑系统:windows 1…

1. 引言

在本案例中,我们将使用鸿蒙NEXT框架开发一个简单的记忆翻牌游戏。该游戏的核心逻辑是玩家通过翻转卡片来寻找匹配的对。本文将详细介绍游戏的实现过程,包括卡片的展示、匹配逻辑以及用户交互。

2. 开发环境准备

电脑系统:windows 10
开发工具:DevEco Studio NEXT Beta1 Build Version: 5.0.3.806
工程版本:API 12
真机:Mate 60 Pro
语言:ArkTS、ArkUI

3. 项目结构

本项目主要由两个类组成:
GameCell:表示游戏中的单个卡片,负责卡片的状态管理和动画效果。
MemoryGame:游戏组件,管理游戏逻辑和用户界面。
GameCell类
GameCell类是游戏的基本单元,包含以下属性和方法:
属性:

value: 卡片的值(如"A", "B"等)。
isVisible: 控制卡片是否可见。
isFrontVisible: 控制卡片是否正面朝上。
isMatched: 标记卡片是否已被匹配。
isAnimationRunning: 动画是否正在运行。
rotationAngle: 卡片的旋转角度。

方法:

revealFace(animationTime, callback): 展示卡片正面并执行动画。
hideFace(animationTime, callback): 隐藏卡片正面并执行动画。
reset(): 重置卡片状态。
动画实现
在revealFace和hideFace方法中,我们使用animateToImmediately函数来实现卡片的翻转动画。通过设置动画的持续时间、迭代次数和曲线类型,确保用户体验流畅。
MemoryGame类
MemoryGame类是游戏的核心组件,负责管理游戏状态和用户交互。
属性:

gameCells: 存储所有卡片的数组。
cellSize: 卡片的大小。
cellSpacing: 卡片之间的间距。
transitionDuration: 动画过渡的持续时间。
firstSelectedIndex和secondSelectedIndex: 记录用户选择的卡片索引。
isGameOver: 游戏是否结束的标志。

方法:

aboutToAppear(): 初始化游戏卡片并洗牌。
shuffleCards(): 洗牌算法,随机打乱卡片顺序。
checkForMatch(): 检查用户选择的两张卡片是否匹配。
build(): 构建游戏界面。

游戏逻辑
在checkForMatch方法中,我们判断用户选择的两张卡片是否相同。如果相同,则将其标记为已匹配;如果不同,则在延迟后隐藏卡片。游戏结束时,弹出对话框提示用户胜利,并提供重新开始的选项。
用户界面
在build方法中,我们使用Column和Flex布局来组织卡片的展示。每张卡片通过Text组件显示其值,并根据状态调整背景颜色和旋转角度。用户点击卡片时,触发相应的翻转和匹配逻辑。

4. 结论

通过本案例,我们展示了如何使用鸿蒙NEXT框架开发一个简单的记忆翻牌游戏。该项目不仅涵盖了基本的游戏逻辑,还展示了如何实现动画效果和用户交互。希望这个案例能为您在鸿蒙NEXT开发中提供一些启发和帮助。

5. 完整代码

import { promptAction } from '@kit.ArkUI' // 导入用于显示对话框的模块// 使用装饰器来观察数据变化
@ObservedV2
class GameCell { // 定义单元格类@Trace value: string; // 单元格的值,即卡片上的图案@Trace isVisible: boolean = false; // 控制卡片是否可见isFrontVisible: boolean = false; // 控制卡片是否正面朝上isMatched: boolean = false; // 标记卡片是否已被匹配isAnimationRunning: boolean = false; // 动画是否正在运行@Trace rotationAngle: number = 0; // 卡片的旋转角度constructor(value: string) { // 构造函数,初始化单元格this.value = value; // 设置单元格的值}// 展示卡片正面的方法revealFace(animationTime: number, callback?: () => void) {if (this.isAnimationRunning) { // 如果已经有动画在运行,则返回return;}this.isAnimationRunning = true; // 设置动画状态为运行中animateToImmediately({ // 开始动画duration: animationTime, // 动画持续时间iterations: 1, // 动画迭代次数curve: Curve.Linear, // 动画曲线类型onFinish: () => { // 动画完成后的回调animateToImmediately({ // 再次开始动画duration: animationTime, // 动画持续时间iterations: 1, // 动画迭代次数curve: Curve.Linear, // 动画曲线类型onFinish: () => { // 动画完成后的回调this.isFrontVisible = true; // 设置卡片为正面朝上this.isAnimationRunning = false; // 设置动画状态为停止if (callback) { // 如果有回调函数,则执行callback();}}}, () => { // 动画开始时的回调this.isVisible = true; // 设置卡片为可见this.rotationAngle = 0; // 设置旋转角度为0});}}, () => { // 动画开始时的回调this.isVisible = false; // 设置卡片为不可见this.rotationAngle = 90; // 设置旋转角度为90度});}// 重置卡片状态的方法reset() {this.isVisible = false; // 设置卡片为不可见this.rotationAngle = 180; // 设置旋转角度为180度this.isFrontVisible = false; // 设置卡片为背面朝上this.isAnimationRunning = false; // 设置动画状态为停止this.isMatched = false; // 设置卡片为未匹配}// 隐藏卡片正面的方法hideFace(animationTime: number, callback?: () => void) {if (this.isAnimationRunning) { // 如果已经有动画在运行,则返回return;}this.isAnimationRunning = true; // 设置动画状态为运行中animateToImmediately({ // 开始动画duration: animationTime, // 动画持续时间iterations: 1, // 动画迭代次数curve: Curve.Linear, // 动画曲线类型onFinish: () => { // 动画完成后的回调animateToImmediately({ // 再次开始动画duration: animationTime, // 动画持续时间iterations: 1, // 动画迭代次数curve: Curve.Linear, // 动画曲线类型onFinish: () => { // 动画完成后的回调this.isFrontVisible = false; // 设置卡片为背面朝上this.isAnimationRunning = false; // 设置动画状态为停止if (callback) { // 如果有回调函数,则执行callback();}}}, () => { // 动画开始时的回调this.isVisible = false; // 设置卡片为不可见this.rotationAngle = 180; // 设置旋转角度为180度});}}, () => { // 动画开始时的回调this.isVisible = true; // 设置卡片为可见this.rotationAngle = 90; // 设置旋转角度为90度});}
}// 定义组件入口
@Entry
@Component
struct MemoryGame { // 定义游戏组件@State gameCells: GameCell[] = []; // 存储游戏中的所有单元格@State cellSize: number = 150; // 单元格的大小@State cellSpacing: number = 5; // 单元格之间的间距@State transitionDuration: number = 150; // 过渡动画的持续时间@State firstSelectedIndex: number | null = null; // 记录第一次选择的卡片索引@State secondSelectedIndex: number | null = null; // 记录第二次选择的卡片索引@State isGameOver: boolean = false; // 游戏是否结束@State startTime: number = 0; // 游戏开始时间aboutToAppear(): void { // 组件即将显示时触发let cardValues: string[] = ["A", "B", "C", "D", "E", "F", "G", "H"]; // 定义卡片的值for (let value of cardValues) { // 遍历卡片值this.gameCells.push(new GameCell(value)); // 添加到游戏单元格数组中this.gameCells.push(new GameCell(value)); // 每个值添加两次以形成对}this.shuffleCards(); // 洗牌}// 洗牌方法shuffleCards() {this.firstSelectedIndex = null; // 清除第一次选择索引this.secondSelectedIndex = null; // 清除第二次选择索引this.isGameOver = false; // 游戏未结束this.startTime = Date.now(); // 设置游戏开始时间为当前时间for (let i = 0; i < 16; i++) { // 重置所有单元格状态this.gameCells[i].reset();}for (let i = this.gameCells.length - 1; i > 0; i--) { // 洗牌算法const randomIndex = Math.floor(Math.random() * (i + 1)); // 随机索引let tempValue = this.gameCells[i].value; // 临时保存值this.gameCells[i].value = this.gameCells[randomIndex].value; // 交换值this.gameCells[randomIndex].value = tempValue; // 交换值}}// 检查卡片是否匹配的方法checkForMatch() {if (this.firstSelectedIndex !== null && this.secondSelectedIndex !== null) { // 确保两个索引都不为空const firstCell = this.gameCells[this.firstSelectedIndex]; // 获取第一个选中的单元格const secondCell = this.gameCells[this.secondSelectedIndex]; // 获取第二个选中的单元格if (firstCell.value === secondCell.value) { // 如果两个单元格的值相同firstCell.isMatched = true; // 标记为已匹配secondCell.isMatched = true; // 标记为已匹配this.firstSelectedIndex = null; // 清除第一次选择索引this.secondSelectedIndex = null; // 清除第二次选择索引if (this.gameCells.every(cell => cell.isMatched)) { // 如果所有单元格都已匹配this.isGameOver = true; // 游戏结束console.info("游戏结束"); // 打印信息promptAction.showDialog({ // 显示对话框title: '游戏胜利!', // 对话框标题message: '恭喜你,用时:' + ((Date.now() - this.startTime) / 1000).toFixed(3) + '秒', // 对话框消息buttons: [{ text: '重新开始', color: '#ffa500' }] // 对话框按钮}).then(() => { // 对话框关闭后执行this.shuffleCards(); // 重新开始游戏});}} else { // 如果两个单元格的值不同setTimeout(() => { // 延迟一段时间后if (this.firstSelectedIndex !== null) { // 如果第一个索引不为空this.gameCells[this.firstSelectedIndex].hideFace(this.transitionDuration, () => { // 隐藏卡片this.firstSelectedIndex = null; // 清除第一次选择索引});}if (this.secondSelectedIndex !== null) { // 如果第二个索引不为空this.gameCells[this.secondSelectedIndex].hideFace(this.transitionDuration, () => { // 隐藏卡片this.secondSelectedIndex = null; // 清除第二次选择索引});}}, 400); // 延迟时间}}}// 构建游戏界面的方法build() {Column({ space: 20 }) { // 创建一个垂直布局Flex({ wrap: FlexWrap.Wrap }) { // 创建一个可换行的弹性布局ForEach(this.gameCells, (gameCell: GameCell, index: number) => { // 遍历游戏单元格Text(`${gameCell.isVisible ? gameCell.value : ''}`) // 显示单元格的值或空字符串.width(`${this.cellSize}lpx`) // 设置宽度.height(`${this.cellSize}lpx`) // 设置高度.margin(`${this.cellSpacing}lpx`) // 设置边距.fontSize(`${this.cellSize / 2}lpx`) // 设置字体大小.textAlign(TextAlign.Center) // 文本居中.backgroundColor(gameCell.isVisible ? Color.Orange : Color.Gray) // 设置背景颜色.fontColor(Color.White) // 设置字体颜色.borderRadius(5) // 设置圆角.rotate({ // 设置旋转x: 0,y: 1,z: 0,angle: gameCell.rotationAngle, // 旋转角度centerX: `${this.cellSize / 2}lpx`, // 中心点X坐标centerY: `${this.cellSize / 2}lpx`, // 中心点Y坐标}).onClick(() => { // 单击事件处理if (this.isGameOver) { // 如果游戏已结束console.info("游戏已结束"); // 打印信息return;}if (gameCell.isMatched) { // 如果单元格已匹配console.info("当前已标记"); // 打印信息return;}if (this.firstSelectedIndex == null) { // 如果没有第一次选择this.firstSelectedIndex = index; // 设置第一次选择索引if (!gameCell.isFrontVisible) { // 如果不是正面朝上gameCell.revealFace(this.transitionDuration); // 展示正面}} else if (this.firstSelectedIndex == index) { // 如果与第一次选择相同console.info("和上一次点击的是一样的,不予处理"); // 打印信息} else if (this.secondSelectedIndex == null) { // 如果没有第二次选择this.secondSelectedIndex = index; // 设置第二次选择索引if (!gameCell.isFrontVisible) { // 如果不是正面朝上gameCell.revealFace(this.transitionDuration, () => { // 展示正面this.checkForMatch(); // 检查是否匹配});}}});});}.width(`${(this.cellSize + this.cellSpacing * 2) * 4}lpx`); // 设置宽度Button('重新开始') // 创建“重新开始”按钮.onClick(() => { // 按钮点击事件this.shuffleCards(); // 重新开始游戏});}.height('100%').width('100%'); // 设置高度和宽度}
}

http://www.dtcms.com/wzjs/586494.html

相关文章:

  • 手工做皮具国外的网站四川微信网站建设推广
  • 深圳做网站那里好网站建设 好公司
  • 学校网站开发工程师公益永久免费主机
  • 邯郸做网站公司昌吉市建设局网站
  • 学做家常菜去那个网站广州 网站开发 公司电话
  • 无锡做网站多少钱页面设计归运营管还是美工
  • 网站做等保html网站设计模板
  • wordpress plugins插件优化关键词可以选择哪个工具
  • 网站开发的相关技能辽宁城乡住房建设厅网站打不开
  • 网站建设购买seo优化心得
  • 自助建站平台哪家好网站建设费记什么科目
  • 装修公司网站建设上海网站建设 报价
  • 如何用vps做网站网站建设前景怎么样
  • 石家庄网站建设今天改网名编程培训班网上课程
  • 北京集团网站建设wordpress主题动漫
  • 安徽省工程建设监理协会网站10根牙签手工制作大全
  • 如何在学校网站上做链接珠海市网站
  • wang域名注册网站网站推广产品怎么做
  • 河北三河建设厅网站6世界工厂采购网登录
  • 建站系统加盟泰安房产信息网
  • 开个网站需要什么条件wordpress ping列表
  • 网站创建成都网站优化
  • 国外的自建网站怎么做如何网上申请个人营业执照
  • 濮阳做网站 汉狮网络免费ppt模板免费网站
  • 门户网站开发报价这几年做哪个网站能致富
  • 安徽省建设干校网站广州网站建设好公司
  • 网站赚钱思路中国会议营销网站
  • 做直播网站宽带网站建设推介会发言稿
  • wordpress post gridseo自动优化软件下载
  • 如何做单位网站怎么做一个电商网站