第一版成品 demo:2048
GitHub:2048

前言
花了几天复刻了2048
,写之前没想到会这么复杂……
我的大体思路还是保持和扫雷一样,v-for
嵌套二维数组渲染格子,写好框架确实省了很多时间,但是这个方案有个很严重的问题,就是很难实现移动动画。
读了读别的网站的 css 代码,发现他们是先写好 16 个格子的样式,然后给每一个格子都使用transform
来进行位移,这样就很自然的可以调用transition
进行位移的平滑过渡了,而且这种效果还属于 CSS 范畴,有硬件加速,比 js 操作要好得多。
我移动格子的方式基本上局限于操作格子的blocks[x][y].value
和blocks[x][y].styles
,然后给格子添加基本动画,这样除了不能实现移动动画之外,其他的功能倒是都可以实现。
我的实现
主视图:
1
2
3
4
5
6
7
8
|
<!-- template -->
<div v-for="(itemY, indexY) in blocks" :key="indexY">
<div v-for="(itemX, indexX) in itemY" :key="indexX">
<div :style="itemX.styles" v-if="!Boolean(itemX.value == 0)">
{{ itemX.value }}
</div>
</div>
</div>
|
操作数组
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
|
function pushUp(arr) {
let steps = 0; // 记录移动元素的次数
let scores = 0; // 记录本次得分
for (let x = 0; x < 4; x++) {
// 每一列都实施相同方法
let head = 0; // 指向该列起始位置
let i = 0;
while (i < 4) {
for (i = head; i < 4; i++) {
// 获取head起第一个非零值
if (arr[x][i].value == 0) continue;
else {
// head需要指向已经push到顶部的元素
// 如果此时head指向为0,说明head指向不正确,需要重置指向
// 操作为:把当前值直接赋值给head,并且不更改head的值
if (arr[x][head].value == 0) {
// 如果head为0,则该值移到head处
arr[x][head].value = arr[x][i].value;
arr[x][head].styles = changeColor(arr[x][head].value);
// changeColor返回对应数值的样式
arr[x][i].value = 0;
arr[x][i].styles = {};
steps++;
break;
// 以新的head重新进行for循环
} else {
// head指向不为0,此时指向正确
// 第一次指向一定是本身,直接跳过即可
if (i == head) continue;
else {
// 此时找到的值需要再与head进行比较
if (arr[x][i].value != arr[x][head].value) {
// 如果当前值和head值不相等
if (i == head + 1) {
// 恰好是head下一个元素,那么就没必要移动了
head++;
break;
} else {
// 把该元素移动到head下方
arr[x][head + 1].value = arr[x][i].value;
arr[x][head + 1].styles = changeColor(arr[x][head + 1].value);
arr[x][i].value = 0;
arr[x][i].styles = {};
steps++;
head++;
break;
}
} else {
// 如果当前值和head值相等,则加入head中
arr[x][head].value = arr[x][head].value * 2;
scores += arr[x][head].value;
arr[x][head].styles = changeColor(arr[x][head].value);
arr[x][i].value = 0;
arr[x][i].styles = {};
steps++;
head++;
break;
}
}
}
}
}
}
}
if (steps != 0) arr = addNewBlock(arr, 1);
// 移动了就生成新格子
return [steps, scores]; // 返回步数和得分
}
|
分数记录
我的项目有game
和rank
两个组件,分别对应游戏本体和历史最高分,那么挂载时可以选择将两个组件同时挂载在App.vue
根组件下,但是如果这样做,我就需要实现非父子组件传值,而且我的 rank 组件是teleport to body
的,根本不需要关心在哪里挂载,所以直接写到 game 下作为一个子组件,实现父组件向子组件传值即可。
game 组件中将得分传入 rank 组件——使用props
1
2
|
<!-- game.vue -->
<rank v-if="showrank" :records="records"></rank>
|
1
2
|
// rank.vue
props: ["records"];
|
那么就可以直接在rank.vue
中调用records
了
计分板

vue 模板中对 records 进行遍历,这里显示八条,所以 records 需要赋初值,那么在内部对 scores 是否等于 0 进行判断,就能实现不显示 0 值了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<div class="grid">
<div class="classes">
<div>序号</div>
<div>分数</div>
<div>时间</div>
</div>
<div v-for="index of 8" :key="index" class="records">
<div>{{ index }}</div>
<div v-if="Boolean(records[index - 1].scores)">
{{ records[index - 1].scores }}
</div>
<div v-if="Boolean(records[index - 1].scores)">
{{ records[index - 1].date }}
</div>
</div>
</div>
|
主体部分使用grid
网格布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// <style lang="scss" scoped>
.grid {
margin: 0 auto;
width: 300px;
.classes {
display: grid;
grid-template-columns: 50px 100px 150px;
}
.records {
display: grid;
grid-template-columns: 50px 100px 150px;
padding: 7px 0;
border-bottom: 1px solid #897c6a;
}
}
|
计分板的关闭需要改变父组件中的showrank
参数,所以这里涉及到子组件向父组件传值,通过调用自定义事件
来完成
1
|
<rank v-if="showrank" :records="records" @close-window="closeRank"></rank>
|
这样在子组件中调用$emit
方法即可
1
2
3
|
closeRankWindow() {
this.$emit("close-window");
},
|
这样在子组件调用CloseRankWindow
方法时就会调用父组件中的closeRank
方法,实现子组件传值(emit 对象可以传入第二个值,即为传参)
目前亟待解决的问题:
- 重构,实现移动动画和合并动画
- 记录上一时刻(也许更多)的状态,实现
“撤回”
功能
- 搭建后端服务器(初步选用 koa),记录社群最高分。