目录
实现效果
主要功能
1.用窗口界面实现2048游戏界面显示;
2. 在界面设置上下左右移动的按键;
3. 实现2048游戏功能;
4. 记录操作次数以及得分情况;
5. 通过矩阵实现。
效果展示
主要程序
变量定义
function GUI_2048_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
global number_matrix; % 设定4*4的矩阵
global step; % 按下按键的次数
global fraction; % 得分
global save_num; %矩阵中的最大值
number_matrix = zeros(4);
step = 0;
fraction = 0;
save_num = 0;
主要代码
% 按下向上的按键
function pushbutton1_Callback(hObject, eventdata, handles)
global number_matrix;
global step;
step = step+1;
global fraction;
% 向上移动
push_up();
%¸给每个静态文本赋值
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
% 判断游戏是否结束
if length(find(number_matrix))==16
if if_game_over()
open('DEAD_2048.fig');
end
end
% 判断增加数字的个数
if length(find(number_matrix))<16
if length(find(number_matrix))<=14
if rem(step,2)==0
rand_times = randi([1,2]);% 随机判定增加一个或者两个
switch rand_times
case 1
while 1
rand_num_loc = randi([1,4],2,1);% 随机位置生成
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();% 赋值
break;
end
end
case 2
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
end
end
elseif length(find(number_matrix))==15
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
else
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
% 判断是否获胜
if_game_win();
set(handles.text1,'String',fraction);% 得分改变
% 按下向左的按键
function pushbutton2_Callback(hObject, eventdata, handles)
global step;
step=step+1;
global number_matrix;
global fraction;
% 向左移动
push_left();
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if numb