HarmonyOS实战 —— 第一弹
贪吃蛇
前言
本人之前是Java后端开发,对于前端开发一知半解。但是对于官方资料中的这个贪吃蛇项目十分感兴趣。打算做一遍流程。虽然资料中也含有开发流程,但是不全,而且没有注释。所以决定自己写完之后,把每个步骤总结出来并写上注释。属于二次创作吧!
开发完成的程序界面如下所示。抄一遍代码运行,也不会超过2h。适合刚学习鸿蒙的开发者~如果你喜欢,不妨收藏一下(≧∇≦)ノ

正文
一、创建项目
1. 选择JS模板
2. 定义自己的项目名和包名,然后点击finsh
3. 创建完毕,生成的目录结构如下
由于项目比较简单,所以就只需要在红框中的位置编写代码。这是由项目默认生成的噢!

二、编写代码
1. 导入图片资源
在我提供的压缩资料里找到图片文件夹,他它们直接copy到项目目录中

2. 编写html页面
先把标签、内容和资源定义好,编写完后运行查看结果
运行后发现样式布局混乱,不过没关系,运行主要是确保样式和资源有没有加载。都加载好之后,再调整样式
(测试的话,要去最上方导航栏,点击 Tools>HVD Manager>登陆开发者账号>选择P40后面的那个三角形)
3. 编写css代码
调整样式,在需要调整的样式后面加对应的类名class="",通过这些类名调用css文件的数据
确认好类名之后,就在index.css文件中根据类名写css
写好上述内容后,再次运行一下。发现有点样子了,只需处理一下按钮即可
4. 优化按钮
想要的效果是方向键如同键盘方向的布局,所以只需对下面三个按键进行处理。可以用一个div标签把它们包裹起来,再定义一个新属性
css部分的新添加的代码
运行测试一下,发现达到了我们想要的效果

三、编写JS代码
我们发现现在只有样式,光点击按钮没有反馈,而且也没有小蛇和食物……
所以我们接下来编写JS代码就是要解决这些事情,但是切忌边写边想。应该先设计再写代码!
1. 设计思想
鼠标点击事件是有对应的方法
通过方法传不同的参数来区别不同的方向
随机生成
判断食物生成的位置如果出现在蛇身上,则重新生成
- 蛇身的初始化 (由于案例比较简单,所以没有设定随机生成初始位置)
给定长度并设定一个空数组
通过for循环,把x和y的坐标push进数组,作为蛇身每格的位置
移动是靠每帧重绘位置
吃到水果就头部立刻加长
没吃到水果就去掉尾部,把头部方向指向的下一个位置记录到数组头部,等下次刷新帧
碰壁
相对方向移动
形成环路
2. 方法调用流程图
虚线代表 if 判断,如果为符合判断条件才会调用该方法

3. 编写代码
在index.html文件中绑定对应的事件(这也是html文件的全部内容)
index.js文件的全部内容
export default {
data: {
title: "",
snakeSize: 30,
w: 600,
h: 600,
score: 0,
snake : [],
ctx: null,
food: null,
direction: '',
gameOver: false,
tail: {
x: 0,
y: 0
},
interval : null
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
const canvas = this.$refs.canvasref;
this.ctx = canvas.getContext("2d");
this.direction = 'down';
this.drawSnake()
this.createFood()
this.paint()
},
drawArea() {
var ctx = this.ctx
ctx.fillStyle = '#61c7e6';
ctx.fillRect(0, 0, this.w, this.h);
ctx.strokeStyle = '#00000';
ctx.lineWidth = 5;
ctx.strokeRect(0, 0, this.w, this.h);
this.ctx = ctx
},
drawSnake() {
var len = 7;
var snake = [];
for (var i = len - 1; i >= 0; i--) {
snake.push({
x: 0,
y: i
});
}
this.snake = snake;
},
bodySnake(x, y) {
var ctx = this.ctx;
ctx.fillStyle = '#e28743';
ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
ctx.strokeStyle = '#063970';
ctx.strokeRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
this.ctx = ctx;
},
cookie(x, y) {
var ctx = this.ctx;
ctx.fillStyle = '#e2d743';
ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
this.ctx = ctx;
},
createFood() {
this.food = {
x: Math.floor((Math.random() * 20) + 1),
y: Math.floor((Math.random() * 20) + 1)
}
for (var i = 0; i > this.snake.length; i++) {
var snakeX = this.snake[i].x;
var snakeY = this.snake[i].y;
if (this.food.x === snakeX && this.food.y === snakeY || this.food.y === snakeY && this.food.x === snakeX) {
this.food.x = Math.floor((Math.random() * 20) + 1);
this.food.y = Math.floor((Math.random() * 20) + 1);
}
}
},
checkCollision(x, y, array) {
for(var i = 0; i < array.length; i++) {
if(array[i].x === x && array[i].y === y)
return true;
}
return false;
},
onStartGame(direct){
this.gameOver = false
if (direct == 1) {
this.direction = 'up'
} else if (direct == 2) {
this.direction = 'left'
} else if (direct == 3) {
this.direction = 'down'
} else if (direct == 4) {
this.direction = 'right'
}
this.paint()
if (this.interval == null) {
this.interval = setInterval(this.paint, 250);
}
},
paint() {
this.drawArea()
var snakeX = this.snake[0].x;
var snakeY = this.snake[0].y;
if (this.direction == 'right') {
snakeX++;
}
else if (this.direction == 'left') {
snakeX--;
}
else if (this.direction == 'up') {
snakeY--;
} else if (this.direction == 'down') {
snakeY++;
}
if (snakeX == -1 || snakeX == this.w / this.snakeSize || snakeY == -1 || snakeY == this.h / this.snakeSize || this.checkCollision(snakeX, snakeY, this.snake)) {
clearInterval(this.interval);
this.interval = null
this.restart()
return;
}
if(snakeX == this.food.x && snakeY == this.food.y) {
this.tail = {x: snakeX, y: snakeY};
this.score = this.score+5;
this.createFood();
} else {
this.tail = this.snake.pop();
this.tail.x = snakeX;
this.tail.y = snakeY;
}
this.snake.unshift(this.tail);
for(var i = 0; i < this.snake.length; i++) {
this.bodySnake(this.snake[i].x, this.snake[i].y);
}
this.cookie(this.food.x, this.food.y);
},
restart() {
this.drawArea()
this.drawSnake()
this.createFood()
this.gameOver = true
this.score = 0
},
}
- 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.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
运行测试ok
总结
写贴方式有点虎头蛇尾,在比较重要的JS代码部分没有细致说清步骤。不过这也是没办法的,因为这里面太多嵌套调用了,只有文字无法说清,唯有视频讲解才能把逻辑理清。所以考虑到这样的缺点,我也做出了程序调用流程图来方便大家理解。就好像很难用语言去描述递归的调用流程一样,因为这是套娃……
在我看来这个小项目还是有比较多改进的地方
比如:
- 蛇的位置随机生成;
- 优化按下相对按键的操作:即蛇向右走的时候,按左方向是不会影响蛇的方向,就不会被程序判定失败了;
- 定制专门的重启游戏按键
- 等等
引用
PS:友情提示
# 图片模糊怎么办?
# 下面有两个文件
【鸿蒙——贪吃蛇项目源码.rar】是项目源码
【4-1.生态案例:【开发者说】重塑经典,如何.pdf】是官方大礼包的原文件
这个文章分享的很全面,学习了!
谢谢支持
好文章
谢谢😁
有没有源码啊,
有的,但是帖子的发布资源接口在升级中,我上传不了源码和对应的文档……
如果你急需的话,可以点击下面的链接查看~资源在上层目录的common中
https://gitee.com/azw13455688/harmony-daily/tree/master/code/snakegame/entry/src/main/js/default/pages/index
写的真详细啊,真不错!
赞赞赞!
谢谢( •̀ ω •́ )✧
hhh,写自己学校的实验报告都没那么认真
感谢分享,很详细
共同努力o(* ̄▽ ̄*)ブ
又开始写我的贪吃蛇游戏了,不久前还用labview写了一个。
作为理论学习中穿插的一个小demo吧~学习没有反馈很快会失去热情hhh
超赞,看到那流程图就觉得厉害了。
还是不够专业,大佬都是用软件工程的知识 + UML图画的,我这反而不规范
有自我要求,666
兄弟,我下载了源码跑了一下,在远程机上会出现布局不合理的地方。内部逻辑没问题,就是参数不知道为什么放在现行的DevEco上跑出来就会这样。
最后我调整了一下参数,运行效果是这样,蛇动起来、吃东西、食物刷新、碰壁死亡、分数显示都没问题。不过,,我调完以后发现在运行的时候画布都是一闪一闪的,,眼睛疼。

就算是第二个显示效果也不是正确的。正确的显示效果是我文末的那个,我也不知道为啥。等我到时候再跑跑试试。看看是不是代码的问题