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

基于JS实现的鸿蒙游戏——二十四点纸牌

目录前言概述正式开始一、创建项目二、编码1.项目结构2.实现思路3.主要代码块三、页面及功能展示1.运算正确2.运算错误3.换一批及重置本人项目仓库链接前言相信大家都有玩过纸牌游戏本项目是基于JS实现的鸿蒙小游戏二十四点纸牌这个游戏可以说是非常之经典。个人认为适合新手接触鸿蒙开发闲暇之时锻炼自己的速算能力。欢迎大家点赞收藏加关注谢谢~概述本项目将从零开始完成鸿蒙小游戏在移动设备如手机上的编译此次以手机为例在项目中我们所使用到的软件为DevEco Studio下载地址为DevEco Studio下载安装教程可以参考鸿蒙开发者联盟的DevEco Studio安装教程在项目中我们要实现的内容为经典二十四纸牌小游戏的开发。正式开始一、创建项目DevEco Studio下载安装成功后打开DevEco Studio点击左侧的Create Project选择一个Empty Ability应用点击Next将Project name即项目名改为PointsGame包名依照个人爱好修改然后选择保存路径以及SDK的版本这里我选择的SDK版本为8语言选择JS最后点击Finish。二、编码1.项目结构2.实现思路hml部分用于展示前端页面css部分用于页面样式的调整pointsgame.js用于网页与用户的交互poker.js用于定制4种花型各13张纸牌的样式这里主要介绍下游戏的主要逻辑。①随机抽牌这里分别写了两种随机抽牌逻辑。一是将4×13共52张牌打乱顺序后抽取前四张二是52张牌的排序不变随机生成4个不重复的数以这四个数作为数组下标抽牌。②牌组替换与还原因为抽牌是随机的而并非所有的组合都是有解的所以需要有操作按钮可以让玩家主动替换或还原当前牌组。③选中样式选中的数字或运算符需要有特别的样式提示以提高游戏体验所以需要给纸牌和运算符设置样式属性。④运算合法判定在进行运算前需要对运算的合法性进行判定。对于零不能作为除数某些数不能被整除等情况需要加以限制。当出现这些情况撤销这次操作恢复牌组状态并弹出提示玩家。⑤得分判定根据场上最后一张牌的数值进行得分判定等于24则加分替换新牌组否则将牌组重置为原状。3.主要代码块pointsgame.hml部分div classcontainer div classgamezone div classpoker div for(index, item) in current classcard {{ item.css }} show{{ item.show }} disabled{{ item.disabled }} image src{{ item.shape }} onclickoptnum(index)/image text classnumber{{ item.text }}/text /div /div div classsign image for(index, item) in operator src{{ item.src }} classoperator {{ item.css }} disabled{{ operdis }} onclicksetsign(index)/image /div /div div classside text classscore得分\n{{ score }}/text button classbtn onclickreplace换一批/button button classbtn onclickrevert重置/button /div /divpointsgame.css部分.container { flex-direction: row; width: 100%; height: 100%; } .gamezone { flex-direction: column; width: 85%; height: 100%; background-color: #9999CC; } .poker { flex-direction: row; justify-content: center; align-items: center; width: 100%; height: 70%; } ........... .score { width: 100%; height: 30%; margin-bottom: 25%; font-size: 28px; text-align: center; } .btn { width: 90px; height: 50px; margin: 10px 0; font-size: 20px; background-color: brown; }pointsgame.js部分let first, second 0; // 第一个数、第二个数下标 export default { data: { score: 0, // 得分 operdis: true, // 运算符不可点击 operindex: null, // 运算符下标 pokers: Poker, // 源牌组 origin: [], // 初始牌组 current: [ // 当前牌组 { num: 1, // 计算值 text: 1, // 左上角文本 shape: common/images/spade.png, // 花型 show: true, // 显示标识符 disabled: false, // 不可交互属性 css: , // 样式 }, { num: 11, text: J, shape: common/images/heart.png, show: true, disabled: false, css: , }, { num: 12, text: Q, shape: common/images/club.png, show: true, disabled: false, css: , }, { num: 13, text: K, shape: common/images/diamond.png, show: true, disabled: false, css: , } ], operator: [ // 加、减、乘、除运算符图片及选中样式 { src: common/images/plus.png, css: , }, { src: common/images/sub.png, css: , }, { src: common/images/mult.png, css: , }, { src: common/images/div.png, css: , }, ], }, onInit() { this.replace(); }, // 换牌方法一将4*13共52张牌先乱序后取前4张 disorder() { let many, ran, temp 0; for(many0; many26; many) { ran Math.floor(Math.random()*52); temp this.pokers[many]; this.pokers[many] this.pokers[ran]; this.pokers[ran] temp; } this.origin [0, 1, 2, 3]; }, ......... // 牌组赋值 initcards() { for(let i0; i4; i) { this.current[i].num this.pokers[this.origin[i]].num; this.current[i].text this.pokers[this.origin[i]].text; this.current[i].shape this.pokers[this.origin[i]].shape; this.current[i].show true; this.current[i].disabled false; this.current[i].css ; } this.operdis true; if(null ! this.operindex) { this.operator[this.operindex].css ; this.operindex null; } }, // 得分判定先判断是否只剩一张牌若是则进行结果比较答对加分换牌答错还原 checkover() { let temp 4; for(let i0; i4; i) { if(false this.current[i].show) { temp --; } } if(1 temp) { // 结果判断是否等于24 if(24 this.current[first].num) { prompt.showToast({ message: 答对啦加分换牌组, duration: 1500, }); this.score 10; this.replace(); } else { prompt.showToast({ message: 答错啦……还原牌组, duration: 1500, }); this.revert(); } } }, }poker.js部分export let Poker [ { num: 1, text: A, shape: common/images/spade.png }, { num: 2, text: 2, shape: common/images/spade.png }, ………… { num: 13, text: K, shape: common/images/diamond.png }, ] export default Poker;三、页面及功能展示1.运算正确2.运算错误3.换一批及重置本人项目仓库链接https://gitee.com/nononoo_O/PointsGamehttps://gitee.com/nononoo_O/PointsGame有兴趣的小伙伴可以关注加收藏哦~

相关文章:

基于JS实现的鸿蒙游戏——二十四点纸牌

目录 前言 概述 正式开始 一、创建项目 二、编码 1.项目结构 2.实现思路 3.主要代码块 三、页面及功能展示 1.运算正确 2.运算错误 3.换一批及重置 本人项目仓库链接 前言 相信大家都有玩过纸牌游戏,本项目是基于JS实现的鸿蒙小游戏二十四点纸牌&…...

vue截取字符串(商城系统非常常用的小知识)

1.截取指定字符串{{row.real_name.substr(2, 3)}}2.截取字符串最后一位{{row.real_name.charAt(row.real_name.length - 1)}}3.看一下效果如何...

【BBF系列协议】TR-140 TR-069支持存储服务的设备的数据模型

目录TR-140 TR-069支持存储服务的设备的数据模型执行摘要1.目的和范围目的范围2 案例2.1 BASIC MANAGED STORAGE SERVICE(基本托管存储服务)2.2 REMOTE STORAGE BACK-UP SERVICE(远程存储备份服务)2.3 Remote access of Storage S…...

【BBF系列协议】TR-135 支持TR-069的STB的数据模型

TR-135 支持TR-069的STB的数据模型 执行摘要 TR-135,启用TR-069的STB的数据模型,定义了用于通过TR-069和TR-106中定义的CWMP远程管理机顶盒(STB)设备上的数字电视(IPTV或广播)功能的数据模型。它包括用于…...

【BBF系列协议】TR-106 CWMP端点和USP代理的数据模型模板

目录TR-106 CWMP端点和USP代理的数据模型模板执行摘要1 介绍CWMPUSP规范结构2 架构2.1 数据层次结构2.2 对象版本控制2.3 配置文件2.4 DEPRECATED 和 OBSOLETED 条目3 对象定义3.1 通用符号3.2 数据类型和表示3.3 供应商特定要素A参数:单个实例对象:单个命令:单个事…...

【亲测免费】【推荐】基于Vue3的全能H5模板:vue3-h5-template

标题:【推荐】基于Vue3的全能H5模板:vue3-h5-template 【免费下载链接】vue3-h5-template My starter template for Vue3, with vite, quark design, sass(含viewport 适配方案, axios 封装) 项目地址: https://gitcode.com/gh_mirrors/vue3/vue3-h5-t…...

Scalding执行模型揭秘:从Job到Execution的演进之路

Scalding执行模型揭秘:从Job到Execution的演进之路 【免费下载链接】scalding A Scala API for Cascading 项目地址: https://gitcode.com/gh_mirrors/sc/scalding Scalding是一个基于Scala的Cascading API,专为大规模数据处理而设计。这个强大的…...

探索DockerGS:一键启动动漫游戏服务器的利器

探索DockerGS:一键启动动漫游戏服务器的利器 【免费下载链接】DockerGS DockerGC is a container that run Grasscutter (anime game) with just a single command. 项目地址: https://gitcode.com/gh_mirrors/do/DockerGS 在数字娱乐的世界中,各…...

推荐项目:IdentityServer4.AccessTokenValidation - 混合型JWT和参考令牌验证利器

推荐项目:IdentityServer4.AccessTokenValidation - 混合型JWT和参考令牌验证利器 【免费下载链接】IdentityServer4.AccessTokenValidation IdentityServer Access Token Validation for ASP.NET Core 项目地址: https://gitcode.com/gh_mirrors/id/IdentityServ…...

推荐:用Flowershow打造你的优雅知识分享网站

推荐:用Flowershow打造你的优雅知识分享网站 【免费下载链接】flowershow 💐 Publish your obsidian digital garden or any markdown site easily and elegantly. 项目地址: https://gitcode.com/gh_mirrors/fl/flowershow 1、项目介绍 &#x…...

RITM 交互式分割项目使用教程

RITM 交互式分割项目使用教程 【免费下载链接】ritm_interactive_segmentation 项目地址: https://gitcode.com/gh_mirrors/rit/ritm_interactive_segmentation 1. 项目的目录结构及介绍 RITM 交互式分割项目的目录结构如下: ritm_interactive_segmentati…...

RITM交互式分割算法实战指南

RITM交互式分割算法实战指南 【免费下载链接】ritm_interactive_segmentation 项目地址: https://gitcode.com/gh_mirrors/rit/ritm_interactive_segmentation 项目介绍 RITM(Real-Time Interactive Image Segmentation with Memory-Augmented U-Net&#…...

从论文到代码:Performer核心公式的PyTorch逐行实现

从论文到代码:Performer核心公式的PyTorch逐行实现 【免费下载链接】performer-pytorch An implementation of Performer, a linear attention-based transformer, in Pytorch 项目地址: https://gitcode.com/gh_mirrors/pe/performer-pytorch Performer是一…...

Jupyter Notify 使用教程

Jupyter Notify 使用教程 【免费下载链接】jupyter-notify A Jupyter Notebook magic for browser notifications of cell completion 项目地址: https://gitcode.com/gh_mirrors/ju/jupyter-notify 项目介绍 Jupyter Notify 是一个为 Jupyter Notebook 设计的扩展&…...

react-shimmer自定义加载效果:打造属于你的独特加载动画

react-shimmer自定义加载效果:打造属于你的独特加载动画 【免费下载链接】react-shimmer 🌠 Async loading, performant Image component for React.js 项目地址: https://gitcode.com/gh_mirrors/re/react-shimmer react-shimmer是一个为React.j…...

diffvg底层原理揭秘:可微光栅化技术如何让矢量图形支持梯度下降优化

diffvg底层原理揭秘:可微光栅化技术如何让矢量图形支持梯度下降优化 【免费下载链接】diffvg Differentiable Vector Graphics Rasterization 项目地址: https://gitcode.com/gh_mirrors/di/diffvg diffvg是一个创新的开源项目,它实现了可微矢量图…...

系统颜色选择器:macOS 的色彩控制强化版

系统颜色选择器:macOS 的色彩控制强化版 【免费下载链接】System-Color-Picker 🎨 The macOS color picker as an app with more features 项目地址: https://gitcode.com/gh_mirrors/sy/System-Color-Picker 项目介绍 🎨 系统颜色选…...

微型Lisp开源项目指南

微型Lisp开源项目指南 【免费下载链接】micro-lisp 🎄A very small Lisp programming language 😀that used to be under 200 lines of C🎄 项目地址: https://gitcode.com/gh_mirrors/mi/micro-lisp 项目介绍 🌟 微型Lis…...

RLS历史回顾:Rust IDE工具链的演进之路

RLS历史回顾:Rust IDE工具链的演进之路 【免费下载链接】rls Repository for the Rust Language Server (aka RLS) 项目地址: https://gitcode.com/gh_mirrors/rl/rls 作为Rust语言的官方IDE工具链,RLS(Rust Language Server&#xff…...

开源项目《Aviator》安装与使用指南

开源项目《Aviator》安装与使用指南 【免费下载链接】aviator Level up your Aviator game! This app employs its prediction prowess to help you maximize your profit - and its completely free! 项目地址: https://gitcode.com/gh_mirrors/avia/aviator 本指南旨在…...

陆彦廷《势均力敌的我们2》收官,细节见人品,尽显绅士与真诚

近日,《势均力敌的我们2》正式收官,天平岛上为期十天的相处旅程,不知不觉就落下了帷幕。陆彦廷在一众嘉宾中,凭着骨子里的真诚和分寸感,成为了节目里让人印象深刻的存在。陆彦廷的聪明,教养和真诚&#xff…...

SAP GR(Group Reporting)合并报表内容及功能简介(一)-主数据结构

目录 主数据&结构 合并单元 合并组: 全局层次结构 主数据&结构 合并单元 合并单元(法定子公司): SAP S/4HANA 公司一对一地链接到合并单元。 SAP S/4HANA 公司标识与合并单元标识相同。 通用日记账的合并单元对应SAP中的公司,不在通用日记账中的合并单元代表…...

MobileChromeApps开发痛点全解析:从环境配置到API适配的完美解决方案

MobileChromeApps开发痛点全解析:从环境配置到API适配的完美解决方案 【免费下载链接】mobile-chrome-apps Chrome apps on Android and iOS 项目地址: https://gitcode.com/gh_mirrors/mo/mobile-chrome-apps 引言:你还在为MobileChromeApps开发…...

Ruby-JMeter 项目推荐

Ruby-JMeter 项目推荐 【免费下载链接】ruby-jmeter A Ruby based DSL for building JMeter test plans 项目地址: https://gitcode.com/gh_mirrors/ru/ruby-jmeter 1. 项目基础介绍和主要编程语言 Ruby-JMeter 是一个基于 Ruby 的领域特定语言(DSL&#xf…...

移动Chrome应用:跨平台的力量汇聚

移动Chrome应用:跨平台的力量汇聚 【免费下载链接】mobile-chrome-apps Chrome apps on Android and iOS 项目地址: https://gitcode.com/gh_mirrors/mo/mobile-chrome-apps 项目基础介绍及主要编程语言 移动Chrome应用(MobileChromeApps/mobile…...

TrackEval并行计算配置:提升MOT评估效率的5个实用技巧

TrackEval并行计算配置:提升MOT评估效率的5个实用技巧 【免费下载链接】TrackEval HOTA (and other) evaluation metrics for Multi-Object Tracking (MOT). 项目地址: https://gitcode.com/gh_mirrors/tr/TrackEval TrackEval是一款用于多目标跟踪&#xff…...

java基于微信小程序的电影点评影评交流平台的设计与实现_0144t2v4

目录项目概述技术选型核心功能模块数据库设计开发阶段计划关键代码示例注意事项项目技术支持可定制开发之功能创新亮点源码获取详细视频演示 :文章底部获取博主联系方式!同行可合作项目概述 设计一个基于微信小程序的电影点评与影评交流平台&#xff0c…...

PHP代码复杂度分析:基于sebastian/lines-of-code的实践指南

PHP代码复杂度分析:基于sebastian/lines-of-code的实践指南 【免费下载链接】lines-of-code Library for counting the lines of code in PHP source code 项目地址: https://gitcode.com/gh_mirrors/li/lines-of-code 在现代PHP开发中,准确评估代…...

fake-rs完全指南:如何在Rust中快速生成逼真测试数据

fake-rs完全指南:如何在Rust中快速生成逼真测试数据 【免费下载链接】fake-rs A library for generating fake data in Rust. 项目地址: https://gitcode.com/gh_mirrors/fa/fake-rs 在软件开发过程中,测试数据的生成往往是一项繁琐但必不可少的任…...

react-shimmer性能优化技巧:让你的图片加载速度提升50%

react-shimmer性能优化技巧:让你的图片加载速度提升50% 【免费下载链接】react-shimmer 🌠 Async loading, performant Image component for React.js 项目地址: https://gitcode.com/gh_mirrors/re/react-shimmer react-shimmer是一个为React.js…...