【游戏】基于matlab GUI贪吃蛇游戏(难度、地图、音乐)【含Matlab源码 1160期】

举报
海神之光 发表于 2022/05/29 01:40:53 2022/05/29
【摘要】 一、获取代码方式 获取代码方式1: 完整代码已上传我的资源:【游戏】基于matlab GUI贪吃蛇游戏(难度、地图、音乐)【含Matlab源码 1160期】 获取代码方式2: 通过订阅紫极神光博客付费...

一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【游戏】基于matlab GUI贪吃蛇游戏(难度、地图、音乐)【含Matlab源码 1160期】

获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效)

二、部分源代码

function snake(cmd)
%SNAKE  Graphical User Interface for playing "Nokia Classic Game" Snake.
%
%   The Game:
%   Make the snake grow longer by directing it to the food.
%   Extra bonus points are given from hearts, eat them as fast as possible
%   as amount of bonus points decreases with time.
%   Higher level gives more points for the food but will also make it
%   tougher to steer a long snake as speed increaces.
%   Five different mazes as well as play w/o maze are possible.
%
%   Game Controls:
%   Steer snake using arrow keys. Pause game with 'p' or space.
%
%   Example:
%       snake    % Start Main Snake Interface

%   

global SNAKE MAZE LEVEL BOARD DIRECTION RUNNING FOOD SOUND BONUS PAUSE
if ~nargin
    cmd = 'init';
end
if ~(ischar(cmd)||isscalar(cmd))
    return;
end

switch cmd
    case 'init'
        scrsz = get(0,'ScreenSize');
        % Initialize figure window
        f = figure('Name','Snake',...
            'Numbertitle','off',...
            'Menubar','none',...
            'Color',[.95 .95 .95],...
            'DoubleBuffer','on',...
            'Position',[(scrsz(3)-400)/2 (scrsz(4)-300)/2 400 300],...
            'Colormap',[.58 .71 .65;.4 .4 .4;0 0 0;1 1 0],...
            'CloseRequestFcn',sprintf('%s(''Stop'');closereq;',mfilename),...
            'KeyPressFcn',sprintf('%s(double(get(gcbf,''Currentcharacter'')))',mfilename));
        % Create the Menu
        FileMenu = uimenu(f,'Label','&File');
        uimenu(FileMenu,'Label','New Game','Accelerator','N','Callback',sprintf('%s(''NewGame'')',mfilename));
        uimenu(FileMenu,'Label','Exit','Accelerator','Q','Separator','on','Callback',sprintf('%s(''Stop'');closereq',mfilename));
        LevelMenu = uimenu(f,'Label','&Level');
        uimenu(LevelMenu,'Label','|','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','|||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','||||','Callback',sprintf('%s(''Level'')',mfilename),'checked','on')
        uimenu(LevelMenu,'Label','|||||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','||||||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','|||||||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','||||||||','Callback',sprintf('%s(''Level'')',mfilename))
        uimenu(LevelMenu,'Label','|||||||||','Callback',sprintf('%s(''Level'')',mfilename))
        MazesMenu = uimenu(f,'Label','&Mazes');
        uimenu(MazesMenu,'Label','No maze','Callback',sprintf('%s(''Mazes'')',mfilename),'checked','on')
        uimenu(MazesMenu,'Label','Box','Callback',sprintf('%s(''Mazes'')',mfilename))
        uimenu(MazesMenu,'Label','Tunnel','Callback',sprintf('%s(''Mazes'')',mfilename))
        uimenu(MazesMenu,'Label','Spiral','Callback',sprintf('%s(''Mazes'')',mfilename))
        uimenu(MazesMenu,'Label','Blockade','Callback',sprintf('%s(''Mazes'')',mfilename))
        uimenu(MazesMenu,'Label','Twisted','Callback',sprintf('%s(''Mazes'')',mfilename))
        HelpMenu = uimenu(f,'Label','&Help');
        uimenu(HelpMenu,'Label','Help','Callback',sprintf('helpwin %s',mfilename))
        uimenu(HelpMenu,'Label','Show Score','Callback',sprintf('%s(''ShowScore'')',mfilename),'Separator','on','Checked','on')
        uimenu(HelpMenu,'Label','Play Sounds','Callback',sprintf('%s(''PlaySound'')',mfilename),'Checked','on')
        uimenu(HelpMenu,'Label','About','Callback',sprintf('%s(''About'')',mfilename),'Separator','on');
        % Create The axes
        axes('Units','normalized',...
            'Position', [0 0 1 1],...
            'Visible','off',...
            'DrawMode','fast',...
            'NextPlot','replace');
        % Add the board
        BOARD = image(getTitle,'CDataMapping','scaled');
        axis image
        set(gca,...
            'XTick',NaN,...
            'YTick',NaN)
        text(40,30,'0',...
            'FontUnits','normalized', ...
            'FontSize',0.03, ...
            'FontName','FixedWidth',...
            'FontWeight','bold',...
            'Color',[1 1 1],...
            'VerticalAlignment','baseline', ...
            'HorizontalAlignment','right',...
            'Tag','Score');
        SNAKE = [14,20;14,19;14,18;14,17;14,16];
        MAZE = zeros(30,40);
        LEVEL = 4;
        SOUND = true;
    case 28 % left
        if SNAKE(2,2)~=mod(SNAKE(1,2)-2,40)+1
            DIRECTION = cmd;
        end
    case 29 % right
        if SNAKE(2,2)~=mod(SNAKE(1,2),40)+1
            DIRECTION = cmd;
        end
    case 30 % up
        if SNAKE(2,1)~=mod(SNAKE(1,1)-2,30)+1
            DIRECTION = cmd;
        end
    case 31 % down
        if SNAKE(2,1)~=mod(SNAKE(1,1),30)+1
            DIRECTION = cmd;
        end
    case 'Level' % Change of Level
        set(get(get(gcbo,'Parent'),'Children'),'checked','off')
        set(gcbo,'checked','on')
        LEVEL = length(get(gcbo,'Label'));
    case 'Mazes' % Change of Maze
        set(get(get(gcbo,'Parent'),'Children'),'checked','off')
        set(gcbo,'checked','on')
        MAZE = zeros(30,40);
        switch get(gcbo,'Label')
            case 'No maze'
            case 'Box'
                MAZE([1 30],:) = 1;
                MAZE(:,[1 40]) = 1;
            case 'Tunnel'
                MAZE([1:4 26:30],[1 40]) = 1;
                MAZE([1 30],[1:4 36:40]) = 1;
                MAZE([10 20],15:25) = 1;
            case 'Spiral'
                MAZE(1:15,15) = 1;
                MAZE(15:30,25) = 1;
                MAZE(25,1:15) = 1;
                MAZE(5,25:40) = 1;
            case 'Blockade'
                MAZE([1:10 end-10:end],[1 end]) = 1;
                MAZE([1 end],:) = 1;
                MAZE([8 22],15:25) = 1;
                MAZE(5:10,[10 30]) = 1;
                MAZE(20:25,[10 30]) = 1;
            case 'Twisted'
                MAZE([1 2 3 9 16 31 39 46 61 69 76 91 99 106 121 129    ...
                    136 159 166 189 196 219 226 249 256 279 286 301 309 ...
                    316 331 339 346 361 369 376 391 399 406 421 429 436 ...
                    451 459 466 481 489 496 511 512 513 514 515 516 517 ...
                    518 519 526 541 556 571 586 601 616 631 646 661 676 ...
                    677 678 679 680 681 682 683 684 685 686 687 688 689 ...
                    690 691 699 706 721 729 736 751 759 766 781 789 796 ...
                    811 819 826 841 849 856 871 879 886 901 909 916 931 ...
                    939 946 969 976 999 1006 1029 1036 1059 1066 1089 ...
                    1096 1119 1126 1149 1156 1179 1186]) = 1;
        end
        feval(mfilename,'Stop')
    case 'ShowScore' % Change of Show Score
        switch get(gcbo,'checked')
            case 'on'
                set(gcbo,'checked','off')
                set(findobj(gcbf,'Tag','Score'),'Visible','off')
            case 'off'
                set(gcbo,'checked','on')
                set(findobj(gcbf,'Tag','Score'),'Visible','on')
        end
    case 'PlaySound' % Change of Play Sounds
        switch get(gcbo,'checked')
            case 'on'
                set(gcbo,'checked','off')
                SOUND = false;
            case 'off'
                set(gcbo,'checked','on')
                SOUND = true;
        end
    case 'NewGame' % New Game N or Ctrl-N
        set(findobj(gcbf,'Tag','Score'),'String','0')
        SNAKE = [14,20;14,19;14,18;14,17;14,16];
        DIRECTION = 29; % right
        BONUS = 0;
        PAUSE = false;
        feval(mfilename,'Food')
        feval(mfilename,'Start')
    case 'Start' % Start Game
        RUNNING = true;
        bonusCounter = 0;
        foodCounter = 0;
        while(RUNNING)
            if ~PAUSE
                SNAKE = circshift(SNAKE,1);
               
                switch DIRECTION
                    case 28 % left
                        SNAKE(1,2) = mod(SNAKE(1,2)-2,40)+1;
                    case 29 % right
                        SNAKE(1,2) = mod(SNAKE(1,2),40)+1;
                    case 30 % up
                        SNAKE(1,1) = mod(SNAKE(1,1)-2,30)+1;
                    case 31 % down
                        SNAKE(1,1) = mod(SNAKE(1,1),30)+1;
                end
                % Check if snake hits any barrier
                if MAZE(SNAKE(1,1),SNAKE(1,2)) || ...
                        sum(ismember(SNAKE(2:end,1),SNAKE(1,1))+...
                        ismember(SNAKE(2:end,2),SNAKE(1,2))==2)
                    %If play sound
                    if SOUND
                        soundsc(sin(1:100),1000)
                    end
                    pause(.3)
                    delete(findobj(gcbf,'Tag','Bonus'))
                    feval(mfilename,'Stop')
                    set(BOARD,'CData',getGameOver)
                else
                    % Check if snake eats bonus
                    if isequal(SNAKE(1,:),BONUS)
                        % Update score
                        scorehandle = findobj(gcbf,'Tag','Score');
                        set(scorehandle,'String',...
                            num2str(LEVEL*ceil(bonusCounter/3)+...
                            str2double(get(scorehandle,'String'))))
                        %If play sound
                        if SOUND
                            soundsc(sin(1:100),5000)
                        end
                        bonusCounter = 1;
                    end
                    if BONUS
                        bonusCounter = bonusCounter-1;
                        if bonusCounter<=0
                            delete(findobj(gcbf,'Tag','Bonus'))
                           
                        end
                    end
                    % Check if snake eats food
                    if isequal(SNAKE(1,:),FOOD)
                        % Snake Grows!
                        SNAKE(end+1,:) = SNAKE(end,:);
                        % Update score
                        scorehandle = findobj(gcbf,'Tag','Score');
                        set(scorehandle,'String',...
                            num2str(LEVEL+str2double(get(scorehandle,'String'))))
                        %If play sound
                        if SOUND
                            soundsc(sin(1:100),10000)
                        end
                        % Spawn new food
                        feval(mfilename,'Food')
                        if ~BONUS % only update if no bonus exist
                            bonusCounter = bonusCounter+15;
                            foodCounter = foodCounter+1;
                        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
  • 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
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/119220766

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。