首页 > 其他分享 >小游戏:基于鸿蒙的24点纸牌游戏

小游戏:基于鸿蒙的24点纸牌游戏

时间:2022-12-17 11:44:07浏览次数:37  
标签:24 operindex num 鸿蒙 current second 小游戏 css first

开发设计实现文档

 


一、编写目的

 

灵感来源于二十四点纸牌游戏,将生活中的纸牌游戏在电脑网页上实现。学会以后在空闲时可以玩一玩,锻炼一下速算能力。

 

二、项目目录

 

 

 

 

 

三、页面结构

 

页面设计得比较简单,上半是数字区,放着四张纸牌。

下半是运算符号区,分别是加、减、乘、除。

右侧是得分记录和两个操作按钮。

 

 

 

 

 

<div class="container">    <div class="gamezone">        <div class="poker">            <div for="(index, item) in current" class="card {{ item.css }}" show="{{ item.show }}" disabled="{{ item.disabled }}">                <image src="{{ item.shape }}" onclick="optnum(index)"></image>                <text class="number">{{ item.text }}</text>            </div>        </div>        <div class="sign">            <image for="(index, item) in operator" src="{{ item.src }}" class="operator {{ item.css }}"                   disabled="{{ operdis }}" onclick="setsign(index)"></image>        </div>    </div>    <div class="side">        <text class="score">得分\n{{ score }}</text>        <button class="btn" onclick="replace">换一批</button>        <button class="btn" onclick="revert">重置</button>    </div></div>

 

 

 

 

 

四、游戏逻辑

随机抽牌:这里分别写了两种随机抽牌逻辑。

一是将4×13共52张牌打乱顺序后抽取前四张;

二是52张牌的排序不变,随机生成4个不重复的数,以这四个数作为数组下标抽牌。

牌组替换与还原:因为抽牌是随机的,而并非所有的组合都是有解的,所以需要有操作按钮,可以让玩家主动替换或还原当前牌组。

选中样式:选中的数字或运算符需要有特别的样式提示,以提高游戏体验,所以需要给纸牌和运算符设置样式属性。

运算合法判定:在进行运算前,需要对运算的合法性进行判定。对于零不能作为除数,某些数不能被整除等情况,需要加以限制。当出现这些情况,撤销这次操作,恢复牌组状态,并弹出提示玩家。

得分判定:根据场上最后一张牌的数值进行得分判定,等于24则加分替换新牌组,否则将牌组重置为原状。

 disorder() {        let many, ran, temp = 0;        for(many=0; many<26; 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];    }

getrandom() {        this.origin = [];        let compare = false;        let temp = 0;        while(4 > this.origin.length) {            compare = false;            temp = Math.floor(Math.random()*52);            for(let i=0; i<this.origin.length; i++) {                if(temp == this.origin[i]) {                                     compare = true;                    break;                }            }            if(false == compare) {                this.origin.push(temp);            }        }    }

replace() {        console.log("—————————更换牌组—————————");//        this.disorder();             this.getrandom();             this.initcards();        console.log("当前牌组为" + JSON.stringify(this.current));    },       revert() {        console.log("—————————还原牌组—————————");        this.initcards();        console.log("还原当前牌组为" + JSON.stringify(this.current));    }

 optnum(num_index) {             if(null == this.operindex) {            this.operdis = false;            first = num_index;            for(let i=0; i<4; i++) {                this.current[i].css = "";            }            this.current[num_index].css = "select";        }               else {            this.operdis = true;            second = num_index;            this.calculate();        }    },        setsign(op_index) {               if("" == this.operator[op_index].css) {            for(let i=0; i<4; i++) {                this.operator[i].css = "";            }            this.operator[op_index].css = "select";            this.operindex = op_index;                     this.current[first].disabled = true;        }               else {            this.operator[op_index].css = "";            this.current[first].disabled = false;            this.operindex = null;        }    }

 calculate() {            if((3 == this.operindex) && (0 == this.current[second].num)) {            prompt.showToast({                message: "0不能作为除数,请重新选择",                duration: 1500,            });                       this.current[first].disabled = false;            this.current[first].css = "";            this.operator[this.operindex].css = "";            this.operindex = null;            this.operdis = true;            first = second = 0;        }        else if((3 == this.operindex) && (0 != (this.current[first].num % this.current[second].num))) {            prompt.showToast({                message: "无法被整除,请重新选择",                duration: 1500,            });                        this.current[first].disabled = false;            this.current[first].css = "";            this.operator[this.operindex].css = "";            this.operindex = null;            this.operdis = true;            first = second = 0;        }               else {            switch (this.operindex) {                case 0:                    this.current[first].num += this.current[second].num;                    break;                case 1:                    this.current[first].num -= this.current[second].num;                    break;                case 2:                    this.current[first].num *= this.current[second].num;                    break;                case 3:                    this.current[first].num /= this.current[second].num;                    break;                default:                    console.log("运算意外出错");                    break;            }                      this.current[first].text = this.current[first].num;            this.current[first].shape = "";            this.current[first].disabled = false;            this.current[first].css = "";            this.current[second].show = false;            this.operator[this.operindex].css = "";            this.operindex = null;            this.checkover();        }    }

 checkover() {        let temp = 4;        for(let i=0; i<4; i++) {            if(false == this.current[i].show) {                temp --;            }        }        if(1 == temp) {                        if(24 == this.current[first].num) {                prompt.showToast({                    message: "答对啦,加分,换牌组",                    duration: 1500,                });                this.score += 10;                this.replace();            }            else {                prompt.showToast({                    message: "答错啦……还原牌组",                    duration: 1500,                });                this.revert();            }        }    }

 

 

 

 

 

 

标签:24,operindex,num,鸿蒙,current,second,小游戏,css,first
From: https://www.cnblogs.com/Mumu111/p/16988782.html

相关文章

  • pyqt5制作俄罗斯方块小游戏-----源码解析
    一、前言最近学习pyqt5中文教程时,最后一个例子制作了一个俄罗斯方块小游戏,由于解释的不是很清楚,所以源码有点看不懂,查找网上资料后,大概弄懂了源码的原理。二、绘制主窗口......
  • P2466 [SDOI2006]Sue的小球
    我们注意到,这道题要求小球最大价值和,并且即使权值变为负的也要算上。因此,状态转移时只要维护所有小球损失的价值“最小即可。这是一道典型的区间dp,因此我们可以设值状态......
  • 【LeeCode】2488. 统计中位数为 K 的子数组 -- 太难了
    【题目描述】给你一个长度为n的数组nums,该数组由从1到n的不同整数组成。另给你一个正整数k。统计并返回num中的中位数等于k的非空子数组的数目。注意:数组......
  • 解决使用innobackupex备份mysql产生returned OS error 124【转】
    ###简介今天在使用innobackupex全量备份数据库的时候发生了下面的错误错误详情19070515:22:18>>logscannedupto(258819807308)xtrabackup:Generatingalist......
  • python24
    Python 变量类型变量是存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间。基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。......
  • 痞子衡嵌入式:浅谈i.MXRT10xx系列MCU外接24MHz晶振的作用
    大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是i.MXRT10xx系列MCU外接24MHz晶振的作用。痞子衡之前写过一篇关于时钟引脚的文章​​《i.MXRT1xxx......
  • 【FAQ】在华为鸿蒙车机上集成华为帐号的常见问题总结
    随着新一代信息技术与汽车产业的深度融合,智能网联汽车正逐渐成为汽车产业发展的战略制高点,无论是传统车企还是新势力都瞄准了“智能座舱”这种新一代人机交互方式。面对竞......
  • 小游戏赛道如何加速流量增长?
    小游戏是指设计极简的轻量级游戏。它构造简单,但却给人带来了娱乐性和重复参与的欲望。近年来,小游戏在抖音、微信小游戏等平台拥有着疯狂裂变的可能性,出现了例如“羊了个羊......
  • 【FAQ】在华为鸿蒙车机上集成华为帐号的常见问题总结
    随着新一代信息技术与汽车产业的深度融合,智能网联汽车正逐渐成为汽车产业发展的战略制高点,无论是传统车企还是新势力都瞄准了“智能座舱”这种新一代人机交互方式。面对竞......
  • 鸿蒙小游戏2048
    DevEcoStudioProjects鸿蒙2048小游戏2048大家应该都玩过,今天我们就来实现一个可以在鸿蒙系统上运行的2048小游戏,大概长下面这样: ​​​​ 在开始写代码之前,我们来分析下,要......