使用matlab实现2048游戏

目录

实现效果

主要功能
1.用窗口界面实现2048游戏界面显示;
2. 在界面设置上下左右移动的按键;
3. 实现2048游戏功能;
4. 记录操作次数以及得分情况;
5. 通过矩阵实现。

效果展示

初始界面

开始游戏

游戏情况
游戏情况2
游戏结束
重新开始
获胜
其他样式

主要程序

变量定义

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值