游戏基于matlab GUI贪吃蛇游戏含Matlab源码 1146期

Posted 紫极神光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游戏基于matlab GUI贪吃蛇游戏含Matlab源码 1146期相关的知识,希望对你有一定的参考价值。

一、简介

基于matlab GUI贪吃蛇游戏

二、源代码

function varargout = Snake(varargin)
% SNAKE M-file for Snake.fig
%      SNAKE, by itself, creates a new SNAKE or raises the existing
%      singleton*.
%
%      H = SNAKE returns the handle to a new SNAKE or the handle to
%      the existing singleton*.
%
%      SNAKE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SNAKE.M with the given input arguments.
%
%      SNAKE('Property','Value',...) creates a new SNAKE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Snake_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Snake_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Snake

% Last Modified by GUIDE v2.5 29-Jun-2021 13:43:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Snake_OpeningFcn, ...
                   'gui_OutputFcn',  @Snake_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Snake is made visible.
function Snake_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Snake (see VARARGIN)

% Choose default command line output for Snake

%%
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'down')
II=imread('background.jpg');
image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');
%%

line( 'Visible', 'on','Tag', 'MoveBlock', 'Markersize', 18, 'HitTest', 'off','Parent',handles.axes1,...
    'Marker', 's', 'MarkerEdgeColor', 'k', 'XData',nan, 'YData',nan , 'LineStyle', 'none','MarkerFaceColor',[0 0 1] ) ;
line( 'Visible', 'on','Tag', 'HeadBlock', 'Markersize', 18, 'HitTest', 'off','Parent',handles.axes1,...
    'Marker', 's', 'MarkerEdgeColor', 'k', 'XData',nan, 'YData',nan , 'LineStyle', 'none','MarkerFaceColor',[0 0 1] ) ;
% Update handles structure
handles = guihandles(hObject);
handles.output = hObject;
setappdata(handles.figure1,'gamelevel',1);
setappdata(handles.figure1,'direction','rightarrow');
setappdata(handles.figure1,'flag',1);
setappdata(handles.figure1,'headreq',1);
guidata(hObject, handles);

pos = get(handles.axes1,'position');
pos = [0 0 pos(3) pos(4)];

rectangle('Position',pos,'EdgeColor','r', 'LineWidth',2 )
% UIWAIT makes Snake wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Snake_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --------------------------------------------------------------------
function Start_Callback(hObject, eventdata, handles)
% hObject    handle to Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
TextHandle = findobj( 'Parent', handles.axes1, 'Type', 'text' ) ;       
delete( TextHandle ) ;
function snake_move(handles);
axespos = get(handles.axes1,'Position');%

XLim = axespos(3);
YLim = axespos(4);

direction = getappdata(handles.figure1,'direction');;
MoveX = get(handles.MoveBlock,'XData');
MoveY = get(handles.MoveBlock,'YData');
HeadX = get(handles.HeadBlock,'XData');
HeadY = get(handles.HeadBlock,'YData');

switch direction
    case 'leftarrow'
         TempX = MoveX(end) - 20;
        if (TempX < 0 | ismember([TempX,MoveY(end)],[MoveX',MoveY'],'rows'))    %撞墙
            setappdata(handles.figure1,'flag',1);
            return;
        end
        if  (TempX == HeadX) & (MoveY(end)==HeadY) %吃
            MoveX = [MoveX,HeadX];
            MoveY = [MoveY,HeadY];
            set(handles.MoveBlock,'XData',MoveX,'YData',MoveY);
            setappdata(handles.figure1,'headreq',1);
            
            modifyscore(handles);
            return;
        else %向左移动
            MoveX(1:end-1) = MoveX(2:end);
            MoveY(1:end-1) = MoveY(2:end);
            MoveX(end) = MoveX(end) - 20;
            set(handles.MoveBlock,'XData',MoveX,'YData',MoveY);
%             setappdata(handles.figure1,'direction','leftarrow');
        end
        
    case 'rightarrow'
        TempX = MoveX(end) + 20;
        if (TempX > XLim | ismember([TempX,MoveY(end)],[MoveX',MoveY'],'rows'))   %撞墙
            setappdata(handles.figure1,'flag',1);
            return;
        end
        if  (TempX == HeadX) & (MoveY(end)==HeadY) %吃
            MoveX = [MoveX,HeadX];
            MoveY = [MoveY,HeadY];
            set(handles.MoveBlock,'XData',MoveX,'YData',MoveY);
            setappdata(handles.figure1,'headreq',1);
            
            modifyscore(handles);
            return;

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

以上是关于游戏基于matlab GUI贪吃蛇游戏含Matlab源码 1146期的主要内容,如果未能解决你的问题,请参考以下文章

GUI简单实战——贪吃蛇

GUI编程---贪吃蛇小游戏开发

贪吃蛇“大作战”

小游戏开发———贪吃蛇

JAVA GUI贪吃蛇小游戏「内含代码」

基于JAVA的贪吃蛇小游戏