目录
引言
大家好,江鸟中原鸿蒙展翅,今天桃花浊酒给大家详细讲解一下小游戏二十四点纸牌的开发过程。相信大家都有玩过纸牌游戏,二十四点纸牌这个游戏可以说是非常之经典。个人认为适合新手接触鸿蒙开发。
环境搭建
软件要求:DevEco studio 3.1.0
HarmonyOS SDK版本:API version 8
硬件要求:设备类型:华为手机或运行在DevEco Studio上的华为手机设备模拟器。
程序设计(代码演示附带运行截图)
1.项目结构

2.实现思路
hml部分用于展示前端页面,css部分用于页面样式的调整,pointsgame.js用于网页与用户的交互,poker.js用于定制4种花型各13张纸牌的样式,这里主要介绍下游戏的主要逻辑。
①随机抽牌:这里分别写了两种随机抽牌逻辑。一是将4×13共52张牌打乱顺序后抽取前四张;二是52张牌的排序不变,随机生成4个不重复的数,以这四个数作为数组下标抽牌。
②牌组替换与还原:因为抽牌是随机的,而并非所有的组合都是有解的,所以需要有操作按钮,可以让玩家主动替换或还原当前牌组。
③选中样式:选中的数字或运算符需要有特别的样式提示,以提高游戏体验,所以需要给纸牌和运算符设置样式属性。
④运算合法判定:在进行运算前,需要对运算的合法性进行判定。对于零不能作为除数,某些数不能被整除等情况,需要加以限制。当出现这些情况,撤销这次操作,恢复牌组状态,并弹出提示玩家。
⑤得分判定:根据场上最后一张牌的数值进行得分判定,等于24则加分替换新牌组,否则将牌组重置为原状。
3.主要代码块
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();
},
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];
},
.........
initcards() {
for(let i=0; i<4; 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 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();
}
}
},
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
4.项目总结
通过上半学期的学习,鸿蒙课程得以圆满落幕。在老师的三个月指导下,加之自学课外知识,参考优秀作品,选定了此课题。代码量适中,虽遇困难,但借助文献修改完善。老师提醒三个月的学习仅为入门,深入仍需努力。我将继续刻苦学习编程。