本项目使用MATLAB实现了一个简单的贪吃蛇小游戏。
注:本项目为转载,原作者无法考证,如有侵权请联系我删除。
主程序如下:
function varargout = eat_snake(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @eat_snake_OpeningFcn, ...
'gui_OutputFcn', @eat_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 eat_snake is made visible.
function eat_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 eat_snake (see VARARGIN)
% Choose default command line output for eat_snake
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = eat_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;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global weizhi g lane speed
restart;
lane=handles.edit1;
speed=str2num(get(handles.edit3,'string'));
fps=speed;
weizhi=handles.axes1;
g=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @game);
start(g)
function game(~,~)
panding;
draw;
function draw(~,~)
global top body food weizhi lane score
zbz(top,body,food,weizhi)
set(lane,'string',score)
function panding(~,~)
global top food tybe score speed
if isequal(top,food)==1
score=score+2*speed;
while isequal(top,food)==1
food=round(20*rand(1,2),0);
end
tybe=1;
else
tybe=0;
end
move(tybe);
function move(tybe)
global top body long way g weizhi
if member(body,top+way)==1
stop(g);
choose=questdlg('游戏失败','警告!','重新开始','关闭游戏','重新开始');
switch choose
case '关闭游戏'
delete(g)
close(eat_snake)
return;
case '重新开始'
restart;
start(g)
return;
end
end
if top(1)>20||top(1)<=0||top(2)>20||top(2)<=0
stop(g)
choose=questdlg('游戏失败','警告!','重新开始','关闭游戏','重新开始');
switch choose
case '关闭游戏'
delete(g)
close(eat_snake)
return;
case '重新开始'
restart;
start(g)
return;
end
end
HL=size(body);
long=HL(1);
if tybe==0
last=[];
else
last=body(long,:);
end
for n=(long):-1:2
body(n,:)=body(n-1,:);
end
body(1,:)=top(1,:);
top=top+way;
top;
body=[body;last];
function restart(~,~)
global top body food way score
top=[5,5];
body=[4,5;3,5;2,5];
food=[5,8];
way=[1,0];
score=0;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global way
way=[0 1];
move(0);
draw;
pause(0.01);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global way
way=[0 -1];
move(0);
draw;
pause(0.01);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global way
way=[1,0];
move(0);
draw;
pause(0.01);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global way
way=[-1,0];
move(0);
draw;
pause(0.01);
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global g
stop(g);
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global g
start(g);
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1)
cla reset;
axis equal
axis(0.5+[0,20,0,20])
set(handles.axes1,'xtick',0:1:20,'ytick',0:1:20,'xcolor','r','ycolor','r')
set(handles.axes1,'color','y')
hold on
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
axis equal
axis(0.5+[0,20,0,20])
set(gca,'xtick',0:1:20,'ytick',0:1:20,'xcolor','r','ycolor','r')
set(gca,'color','y')
hold on
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
运行效果如下: