详解matlab之简易2048制作
详解matlab之简易2048制作
一):制作之前
小游戏2048出来也算很久了,基本上大家都知道,通俗易懂,玩法也很简单,前几天兴趣来了,通过matlab小做了一下,并做成了gui界面,总的来说基本功能是实现了,还是挺有意思的。
好吧,下面来说说具体实现过程。虽然不是专业做游戏的,但是想想,做一个游戏或者编一个程序,首先得有框架,或者大体实现思路,第一步该干啥,下一步,等等等等,最后实现了。这之中先不管具体到某一步能不能实现,总的架构得有。之后才是具体到某一步,分析合理性以及解决办法。比如像这个游戏,你就得考虑一下几点吧:
(1)关于初始化:
初始化简单,生成最初的数字2吧,一般是生成2个2,那么生成的位置呢?肯定是随机的,也就是16个位置随机挑两个位置放2。
(2)运行时出现2的位置问题
那么在每次执行一次上下左右以后,整个游戏方格中必须出现一个新的2吧,那么这个2出现在哪里?首先得是空余的位置,然后是在这些空余位置里随机放吧。
(3)移动方向问题
自然这个问题是这个游戏的关键。这个游戏每次我们得在“上下左右”四个方向上选择一次运行吧,选择以后,就是关于相同数值的合并问题了,比如说某一次选择的是上这个方向,那么对于4*4的矩阵上,每一列上的四个位置我们得看看有没有数字吧?有几个数字?然后这几个数字能不能合并?合并完以后再把合并完的数字移动位置(依次移动到每一列的最上面排着),移动完以后,剩下的位置得清空为0。这样对于每一列操作一次就把“上”对应的操作做完了,那么“下左右”同理这么实现就可以了。
(4)关于数字合并
合并数字就是当碰到相同数字的时候的相加起来作为一个新的数字,原来两个数字就不见了。有问题的一点就是某次操作时某一列或者行上的数字有几个,要是一个就不用合并了,直接移动到这一行或者列的最当头就是了,如果多余一个数字,再看数字能不能合并?那么怎么合并,比如有一次往右边移动,那就是四行数字往右边走,假设某一行是[2 2 4 0],那么变成[0 0 4 4],如果是[0 2 2 2],那么怎么合?是第2和3合,还是第3和4合?如果是往右移动,那就是3和4位置的合,往左移动,就是2和3位置的合。理解了合的顺序关系那就简单了。
(5)关于游戏怎么结束
结束的方式似乎很简单,当4*4的位置排满数字,且上下左右都不能合并的时候就算结束了。
(6)其他还有什么,就看着需要办吧
二):实现过程
(1)关于空位置补充一个2
这个好弄,在运行之间把当前的4*4矩阵输入进来,检测一下为0的位置,然后在这些位置中挑一个位置置为2,然后返回这个矩阵就ok了,如下:
function M = add_single_2(M)
index = find(M == 0);%找为0的位置
num = randperm(length(index),1);%随机挑一个位置
M(index(num)) = 2;
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
(2)关于移动后的操作
首先移动,我们的有个方法来检测是往哪个方向移动,比如用检测字符“w s a d”作为上下左右吧,每次检测到一个一以后,再对于这个移动方法,分别对每列(也可能是每行)进行数据合并,移动等等。那么每次移动后,我们得索引那些有数值的点,然后才能进行合并操作。代码如下:
function M = move(M,way)
switch way
%上去
case 'w'
[x,y] = find(M~=0);%索引有数字的位置点
M = up(M,x,y,1);%输入位置点和第1列数,合并
M = up(M,x,y,2);%输入位置点和第2列数,合并
M = up(M,x,y,3);%输入位置点和第3列数,合并
M = up(M,x,y,4);%输入位置点和第4列数,合并
case 's'
[x,y] = find(M~=0);%索引有数字的位置点
M = down(M,x,y,1);%输入位置点和第1列数,合并
M = down(M,x,y,2);%输入位置点和第2列数,合并
M = down(M,x,y,3);%输入位置点和第3列数,合并
M = down(M,x,y,4);%输入位置点和第4列数,合并
case 'a'
[x,y] = find(M~=0);%索引有数字的位置点
M = left(M,x,y,1);%输入位置点和第1行数,合并
M = left(M,x,y,2);%输入位置点和第2行数,合并
M = left(M,x,y,3);%输入位置点和第3行数,合并
M = left(M,x,y,4);%输入位置点和第4行数,合并
case 'd'
[x,y] = find(M~=0);%索引有数字的位置点
M = right(M,x,y,1);%输入位置点和第1行数,合并
M = right(M,x,y,2);%输入位置点和第2行数,合并
M = right(M,x,y,3);%输入位置点和第3行数,合并
M = right(M,x,y,4);%输入位置点和第4行数,合并
otherwise
warning('输入方向键错误,方向描述:上:w 下:s 左:a 右:d');
M = M;
End
- 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
- 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
三):关于具体某个方向的操作
现在有了矩阵M,也知道了不为0的数据点的行x与列y,以及我们要对哪一行或者那一列进行操作,下面合并就简单了,所需要考虑的问题就是那一行或者列到底有几个数(无非0,1,2,3,4)?这几个数能不能合并?谁更谁合并的问题。这里比较一般的方法,我们可以把所有的可能性都列出来,进行switch选择不就可以了,具体如下:
3.1)上操作:
function M = up(M,x,y,num)
index1 = find(y==num);%找第一列的位置
switch length(index1)
case 1
M(1,num) = M(x(index1),y(index1));
M(2:end,num) = 0;
case 2
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
if temp1==temp2
temp = temp1 + temp2;
M(1,num) = temp;
M(2:end,num) = 0;
else
M(1,num) = temp1;
M(2,num) = temp2;
M(3:end,num) = 0;
end
case 3
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp3 = M(x(index1(3)),y(index1(3)));
if temp1==temp2
temp = temp1 + temp2;
M(1,num) = temp;
M(2,num) = temp3;
M(3:end,num) = 0;
else if temp2==temp3
temp = temp2 + temp3;
M(1,num) = temp1;
M(2,num) = temp;
M(3:end,num) = 0;
else
M(1,num) = temp1;
M(2,num) = temp2;
M(3,num) = temp3;
M(4:end,num) = 0;
end
end
case 4
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp3 = M(x(index1(3)),y(index1(3)));
temp4 = M(x(index1(4)),y(index1(4)));
if temp1 == temp2
if temp3 == temp4
temp = temp1 + temp2;
tempp = temp3 + temp4;
M(1,num) = temp;
M(2,num) = tempp;
M(3:end,num) = 0;
else
temp = temp1 + temp2;
M(1,num) = temp;
M(2,num) = temp3;
M(3,num) = temp4;
M(4:end,num) = 0;
end
else if temp2 == temp3
temp = temp2 + temp3;
M(1,num) = temp1;
M(2,num) = temp;
M(3,num) = temp4;
M(4:end,num) = 0;
else if temp3 == temp4
temp = temp3 + temp4;
M(1,num) = temp1;
M(2,num) = temp2;
M(3,num) = temp;
M(4:end,num) = 0;
else
M(1:end,num) = M(1:end,num);
end
end
end
end
- 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
- 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
3.2):下操作:
function M = down(M,x,y,num)
index1 = find(y==num);%找第一列的位置
switch length(index1)
case 1
M(4,num) = M(x(index1),y(index1));
M(3:-1:1,num) = 0;
case 2
temp2 = M(x(index1(1)),y(index1(1)));
temp1 = M(x(index1(2)),y(index1(2)));
if temp1==temp2
temp = temp1 + temp2;
M(4,num) = temp;
M(3:-1:1,num) = 0;
else
M(4,num) = temp1;
M(3,num) = temp2;
M(2:-1:1,num) = 0;
end
case 3
temp3 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp1 = M(x(index1(3)),y(index1(3)));
if temp1==temp2
temp = temp1 + temp2;
M(4,num) = temp;
M(3,num) = temp3;
M(2:-1:1,num) = 0;
else if temp2==temp3
temp = temp2 + temp3;
M(4,num) = temp1;
M(3,num) = temp;
M(2:-1:1,num) = 0;
else
M(4,num) = temp1;
M(3,num) = temp2;
M(2,num) = temp3;
M(1:-1:1,num) = 0;
end
end
case 4
temp4 = M(x(index1(1)),y(index1(1)));
temp3 = M(x(index1(2)),y(index1(2)));
temp2 = M(x(index1(3)),y(index1(3)));
temp1 = M(x(index1(4)),y(index1(4)));
if temp1 == temp2
if temp3 == temp4
temp = temp1 + temp2;
tempp = temp3 + temp4;
M(4,num) = temp;
M(3,num) = tempp;
M(2:-1:1,num) = 0;
else
temp = temp1 + temp2;
M(4,num) = temp;
M(3,num) = temp3;
M(2,num) = temp4;
M(1:-1:1,num) = 0;
end
else if temp2 == temp3
temp = temp2 + temp3;
M(4,num) = temp1;
M(3,num) = temp;
M(2,num) = temp4;
M(1:-1:1,num) = 0;
else if temp3 == temp4
temp = temp3 + temp4;
M(4,num) = temp1;
M(3,num) = temp2;
M(2,num) = temp;
M(1:-1:1,num) = 0;
else
M(1:end,num) = M(1:end,num);
end
end
end
end
- 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
- 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
3.3)左操作:
function M = left(M,x,y,num)
index1 = find(x==num);%找第一列的位置
switch length(index1)
case 1
M(num,1) = M(x(index1),y(index1));
M(num,2:end) = 0;
case 2
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
if temp1==temp2
temp = temp1 + temp2;
M(num,1) = temp;
M(num,2:end) = 0;
else
M(num,1) = temp1;
M(num,2) = temp2;
M(num,3:end) = 0;
end
case 3
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp3 = M(x(index1(3)),y(index1(3)));
if temp1==temp2
temp = temp1 + temp2;
M(num,1) = temp;
M(num,2) = temp3;
M(num,3:end) = 0;
else if temp2==temp3
temp = temp2 + temp3;
M(num,1) = temp1;
M(num,2) = temp;
M(num,3:end) = 0;
else
M(num,1) = temp1;
M(num,2) = temp2;
M(num,3) = temp3;
M(num,4:end) = 0;
end
end
case 4
temp1 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp3 = M(x(index1(3)),y(index1(3)));
temp4 = M(x(index1(4)),y(index1(4)));
if temp1 == temp2
if temp3 == temp4
temp = temp1 + temp2;
tempp = temp3 + temp4;
M(num,1) = temp;
M(num,2) = tempp;
M(num,3:end) = 0;
else
temp = temp1 + temp2;
M(num,1) = temp;
M(num,2) = temp3;
M(num,3) = temp4;
M(num,4:end) = 0;
end
else if temp2 == temp3
temp = temp2 + temp3;
M(num,1) = temp1;
M(num,2) = temp;
M(num,3) = temp4;
M(num,4:end) = 0;
else if temp3 == temp4
temp = temp3 + temp4;
M(num,1) = temp1;
M(num,2) = temp2;
M(num,3) = temp;
M(num,4:end) = 0;
else
M(num,1:end) = M(num,1:end);
end
end
end
end
- 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
- 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
3.4)右操作:
function M = right(M,x,y,num)
index1 = find(x==num);%找第一列的位置
switch length(index1)
case 1
M(num,4) = M(x(index1),y(index1));
M(num,3:-1:1) = 0;
case 2
temp2 = M(x(index1(1)),y(index1(1)));
temp1 = M(x(index1(2)),y(index1(2)));
if temp1==temp2
temp = temp1 + temp2;
M(num,4) = temp;
M(num,3:-1:1) = 0;
else
M(num,4) = temp1;
M(num,3) = temp2;
M(num,2:-1:1) = 0;
end
case 3
temp3 = M(x(index1(1)),y(index1(1)));
temp2 = M(x(index1(2)),y(index1(2)));
temp1 = M(x(index1(3)),y(index1(3)));
if temp1==temp2
temp = temp1 + temp2;
M(num,4) = temp;
M(num,3) = temp3;
M(num,2:-1:1) = 0;
else if temp2==temp3
temp = temp2 + temp3;
M(num,4) = temp1;
M(num,3) = temp;
M(num,2:-1:1) = 0;
else
M(num,4) = temp1;
M(num,3) = temp2;
M(num,2) = temp3;
M(num,1:-1:1) = 0;
end
end
case 4
temp4 = M(x(index1(1)),y(index1(1)));
temp3 = M(x(index1(2)),y(index1(2)));
temp2 = M(x(index1(3)),y(index1(3)));
temp1 = M(x(index1(4)),y(index1(4)));
if temp1 == temp2
if temp3 == temp4
temp = temp1 + temp2;
tempp = temp3 + temp4;
M(num,4) = temp;
M(num,3) = tempp;
M(num,2:-1:1) = 0;
else
temp = temp1 + temp2;
M(num,4) = temp;
M(num,3) = temp3;
M(num,2) = temp4;
M(num,1:-1:1) = 0;
end
else if temp2 == temp3
temp = temp2 + temp3;
M(num,4) = temp1;
M(num,3) = temp;
M(num,2) = temp4;
M(num,1:-1:1) = 0;
else if temp3 == temp4
temp = temp3 + temp4;
M(num,4) = temp1;
M(num,3) = temp2;
M(num,2) = temp;
M(num,1:-1:1) = 0;
else
M(num,1:end) = M(num,1:end);
end
end
end
end
- 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
- 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
四):命令窗口模拟显示
为了测试这种方式的好坏,我们先在命令窗口模拟现实一下,而后再转到GUI上。编写主函数如下(这个里面并不包含结束条件):
clc
clear
% 初始化M矩阵
M = zeros(4,4);
num = randperm(16,2);
M(num) = 2;
M
while nnz(M)~=16 % M中不为0的个数
way = input('输入选择方向:','s');
M1 = move(M,way);
if sum(sum(abs(M-M1))) ~= 0 %如果前后有变化,才添加2
M1 = add_single_2(M1);
end
M = M1;
M
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
这样运行主函数(前几步):
M =
0 0 2 0
0 2 0 0
0 0 0 0
0 0 0 0
输入选择方向:a
M =
2 0 0 0
2 2 0 0
0 0 0 0
0 0 0 0
输入选择方向:w
M =
4 2 0 2
0 0 0 0
0 0 0 0
0 0 0 0
输入选择方向:a
M =
4 4 0 0
0 2 0 0
0 0 0 0
0 0 0 0
输入选择方向:d
M =
0 0 0 8
0 0 0 2
0 0 0 0
0 2 0 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
- 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
总的来看还是可以,运行正确。
五):关于matlab的GUI实现
为了使得游戏更具有可玩性,考虑做出GUI界面,这样更贴近与游戏。
Matlab制作GUI还是比较容易,它自带一个GUI生产器,关于界面的布局什么的都可以自己产生,不需要程序来实现,需要的仅仅是当你按下某个按钮以后所触发的事件函数。
Matlab打开GUI的方式,直接新建一个图形用户见面就可以打开了。具体使用就不说了,像很多各种控件,输入框等等都可以随便使用,拖入到一个新建的GUI下就可以。做个简单的画面如下所示:
左边那个框都是各类可用的控件。
之后就是编写事件触发函数,而这些函数都是可以用到我们前面编写的那些函数的。
这里需要几个呢?无非“开始”,“上下左右”这五个触发函数,剩下的是显示区域,只需要在运行时set一下对应的某个框中的值就可以了,当然每个控件都有一些其他的属性的,比如字体大小,控件名称等等,这些都需要调调才好用。
编辑函数,点上面的那个函数编辑就可以,直接进入这个GUI对应的函数代码了。这里只给出部分触发函数的代码如下:
比如“开始”控件回调函数:
function start_Callback(hObject, eventdata, handles)
% 初始化M矩阵
global M
global step_num
step_num = 0;
M = zeros(4,4);
num = randperm(16,2);
M(num) = 2;
index = find(M~=0);
M1 = cell(1,16);
for i = 1:length(index)
M1{index(i)} = M(index(i));
end
set(handles.up,'enable','on');
set(handles.down,'enable','on');
set(handles.left,'enable','on');
set(handles.right,'enable','on');
set(handles.w1,'String',num2str(M1{1}));
set(handles.w2,'String',num2str(M1{2}));
set(handles.w3,'String',num2str(M1{3}));
set(handles.w4,'String',num2str(M1{4}));
set(handles.w5,'String',num2str(M1{5}));
set(handles.w6,'String',num2str(M1{6}));
set(handles.w7,'String',num2str(M1{7}));
set(handles.w8,'String',num2str(M1{8}));
set(handles.w9,'String',num2str(M1{9}));
set(handles.w10,'String',num2str(M1{10}));
set(handles.w11,'String',num2str(M1{11}));
set(handles.w12,'String',num2str(M1{12}));
set(handles.w13,'String',num2str(M1{13}));
set(handles.w14,'String',num2str(M1{14}));
set(handles.w15,'String',num2str(M1{15}));
set(handles.w16,'String',num2str(M1{16}));
set(handles.step,'String',num2str(step_num));
- 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
- 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
再比如“上”按钮回调函数:
function up_Callback(hObject, eventdata, handles)
global M
global step_num
if nnz(M)~=17 % M中不为0的个数
temp = 1;
MM1 = move(M,'w');
if sum(sum(abs(M-MM1))) ~= 0 %如果前后有变化,才添加2
MM1 = add_single_2(MM1);
step_num = step_num+1;
temp = 0;
end
M = MM1;
index = find(M~=0);
M1 = cell(1,16);
for i = 1:length(index)
M1{index(i)} = M(index(i));
end
set(handles.w1,'String',num2str(M1{1}));
set(handles.w2,'String',num2str(M1{2}));
set(handles.w3,'String',num2str(M1{3}));
set(handles.w4,'String',num2str(M1{4}));
set(handles.w5,'String',num2str(M1{5}));
set(handles.w6,'String',num2str(M1{6}));
set(handles.w7,'String',num2str(M1{7}));
set(handles.w8,'String',num2str(M1{8}));
set(handles.w9,'String',num2str(M1{9}));
set(handles.w10,'String',num2str(M1{10}));
set(handles.w11,'String',num2str(M1{11}));
set(handles.w12,'String',num2str(M1{12}));
set(handles.w13,'String',num2str(M1{13}));
set(handles.w14,'String',num2str(M1{14}));
set(handles.w15,'String',num2str(M1{15}));
set(handles.w16,'String',num2str(M1{16}));
set(handles.step,'String',num2str(step_num));
end
if nnz(M)==16 && temp
M1 = cell(1,16);
for i = 1:16
M1{i} = 'over';
end
set(handles.w1,'String',(M1{1}));
set(handles.w2,'String',(M1{2}));
set(handles.w3,'String',(M1{3}));
set(handles.w4,'String',(M1{4}));
set(handles.w5,'String',(M1{5}));
set(handles.w6,'String',(M1{6}));
set(handles.w7,'String',(M1{7}));
set(handles.w8,'String',(M1{8}));
set(handles.w9,'String',(M1{9}));
set(handles.w10,'String',(M1{10}));
set(handles.w11,'String',(M1{11}));
set(handles.w12,'String',(M1{12}));
set(handles.w13,'String',(M1{13}));
set(handles.w14,'String',(M1{14}));
set(handles.w15,'String',(M1{15}));
set(handles.w16,'String',(M1{16}));
set(handles.up,'enable','off');
set(handles.down,'enable','off');
set(handles.left,'enable','off');
set(handles.right,'enable','off');
end
- 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
- 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
完整代码就不贴了,太多,后面有下载地址,可以下载着去试试。
设计完成后就可以点击运行开玩了,如下:
当然了,你还可以加需求多其他的控件什么的,并且玩的过程中会发现,网络上的游戏,合并的过程慢悠悠的,我们这个飞快,也就是说缺少个动画过程,当然是简陋,不过还是可以玩的。
posted on 2017-04-12 10:21 legendsun 阅读(2030) 评论(0) 编辑 收藏 举报