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
% 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
mainfc;
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');
%%
mainfc;
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 ) ;
setappdata(handles.figure1,'flag',0);
setappdata(handles.figure1,'direction','rightarrow');
setappdata(handles.figure1,'gamelevel',1);
set(handles.MoveBlock,'XData',[170 190 210],'YData',[210 210 210]);
set(handles.score,'string','0');
while ishandle(hObject)
if getappdata(handles.figure1,'flag');
text( 100, 240, 'GameOver', 'Parent', handles.axes1,...
'FontSize', 30, 'Color', 'r', 'FontAngle', 'italic' ) ;
text( 100, 200, strcat('得分:',get(handles.score,'string')), 'Parent', handles.axes1,...
'FontSize', 30, 'Color', 'r', 'FontAngle', 'italic' ) ;
break;
end
if getappdata(handles.figure1,'headreq');
[Xpos,Ypos] = randblock(handles);
set(handles.HeadBlock,'XData',Xpos,'YData',Ypos);
setappdata(handles.figure1,'headreq',0);
end
while ( ishandle(hObject) & ~getappdata(handles.figure1,'flag') & ~getappdata(handles.figure1,'headreq') )
snake_move(handles);
level = getappdata(handles.figure1,'gamelevel');
pausetime = 1 - 0.099 * level;
pause(pausetime);
end
end
% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject handle to File (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on key press over figure1 with no controls selected.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CurrentKey = get( handles.figure1, 'CurrentKey' ) ;
olddire = getappdata(handles.figure1,'direction');
switch olddire
case 'leftarrow'
if( strcmp(CurrentKey,'rightarrow'))
return;
end
case 'rightarrow'
if( strcmp(CurrentKey,'leftarrow'))
return;
end
case 'uparrow'
if( strcmp(CurrentKey,'downarrow'))
return;
end
case 'downarrow'
if( strcmp(CurrentKey,'uparrow'))
return;
end
otherwise
return;
end
switch CurrentKey
case {'leftarrow','rightarrow','uparrow','downarrow'}
if ~getappdata(handles.figure1,'flag')
setappdata(handles.figure1,'direction',CurrentKey);
end
otherwise
return;
end;
% pause(0.4);
% --------------------------------------------------------------------
function Exit_Callback(hObject, eventdata, handles)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
% --------------------------------------------------------------------
function About_Callback(hObject, eventdata, handles)
% hObject handle to About (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox('matlab snake version 1.0 by palsword');
% --------------------------------------------------------------------
function Help_Callback(hObject, eventdata, handles)
% hObject handle to Help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox('上下左右键控制贪吃蛇的移动方向!');
% --- 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)
Start_Callback(hObject, eventdata, handles);
慕容复之巅
- 粉丝: 572
- 资源: 60
最新资源
- 基于SpringBoot的遥感影像共享系统源码(java毕业设计完整源码).zip
- (175624016)C# ModbusRtu或者TCP协议上位机源码,包括存储,数据到SQL SERVER数据库,趋势曲线图,数据报表,实时和历史报
- 棒板电极流注放电, COMSOL,氩气形成的贯穿流注 氩气放电等离子体仿真
- 基于web家庭财务管理系统.doc
- Unity入门教程:创建可控制移动并支持碰撞变色的小球
- (175710838)C#含有ModbusRtu通讯库,通讯示例 硬件设备测试例程
- 基于springboot的青少年心理健康教育网站的设计与实现源码(java毕业设计完整源码+LW).zip
- (175829054)python在线电影推荐系统源码数据库演示.zip
- 基于web的家校通管理系统的设计及实现 .doc
- 基于SpringBoot的高校电动车租赁服务业务系统的设计与实现源码(java毕业设计完整源码).zip
- 异步电动机变频调速系统设计 全文10653个字 详情请看文章 两个仿真+文章+文献+技术
- (175968616)基于ASP网络办公OA系统设计(论文+源代码+开题报告+答辩PPT)
- 基于javaweb驾校理论考试模拟系统论文.doc
- (176039852)粒子群算法解决定点选址问题matlab
- (176099846)上位机与PLC 通讯源码DEMO 上位机与三菱PLC,西门子PLC通讯 同时一起通讯,单独控制,三菱采用官方MX 通讯,支持三
- 基于spring和vue的食品厂管理系统源码(java毕业设计完整源码).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈