A list,

Matlab GUI snake game

Ii. Source code

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 anew 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, handles)'flag'.1);
            return;
        end
        if  (TempX == HeadX) & (MoveY(end)==HeadY)% for MoveX= [MoveX,HeadX];
            MoveY = [MoveY,HeadY];
            set(handles.MoveBlock,'XData',MoveX,'YData',MoveY);
            setappdata(handles.figure1,'headreq'.1);
            
            modifyscore(handles);
            return;
        else% Move left 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, handles)'flag'.1);
            return;
        end
        if  (TempX == HeadX) & (MoveY(end)==HeadY)% for MoveX= [MoveX,HeadX];
            MoveY = [MoveY,HeadY];
            set(handles.MoveBlock,'XData',MoveX,'YData',MoveY);
            setappdata(handles.figure1,'headreq'.1);
            
            modifyscore(handles);
            return;
Copy the code

Third, the operation result

Fourth, note

Version: 2014 a