www.gusucode.com > 贪吃蛇源码程序 > 贪吃蛇源码程序/code/she.m

    function varargout = she(varargin)
% SHE MATLAB code for she.fig
%      SHE, by itself, creates a new SHE or raises the existing
%      singleton*.
%
%      H = SHE returns the handle to a new SHE or the handle to
%      the existing singleton*.
%
%      SHE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHE.M with the given input arguments.
%
%      SHE('Property','Value',...) creates a new SHE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before she_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to she_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 she

% Last Modified by GUIDE v2.5 28-Sep-2014 00:24:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @she_OpeningFcn, ...
    'gui_OutputFcn',  @she_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 she is made visible.
    function she_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 she (see VARARGIN)
        
        % Choose default command line output for she
        handles.output = hObject;
        % Update handles structure
        guidata(hObject, handles);
        movegui center
        
        %initial
        %global old_direction;
        %global new_direction;
        axis equal
        set(handles.axes1,'xlim',[0,25],'ylim',[0,25],'box','on','Xtick',[],'Ytick',[]);
         load max_score
          set(handles.edit2,'string',max_score);
        % UIWAIT makes she wait for user response (see UIRESUME)
        % uiwait(handles.figure1);
        
        
        % --- Outputs from this function are returned to the command line.
        function varargout = she_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)
                global flag;
                global dir_change;
                dir_change=0;
                 load max_score
                  set(handles.edit2,'string',max_score);
                score=0;
                % 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 direction%direction为一个方向矩阵,存储蛇每一个节点的方向
                snake_point(1,:)=[15,8];%flag_point为一个矩阵,存储蛇每一个节点的坐标
                add_snake(15,8);%h_snake为一个句柄矩阵,存储蛇每一个节点的句柄
                goal_point=[floor(25*rand),floor(25*rand)];
                while all(goal_point==snake_point(1,:))
                    goal_point=[floor(25*rand),floor(25*rand)];
                end
                add_snake(goal_point(1),goal_point(2),'k');
                snake_tail=snake_point(end,:);
                %初始化方向
                %上下左右分别对应着1,2,3,4,初始方向为随机的一个数
                direction=randi([1,4],1);
                t=0.1;
                while 1
               if score>50
                   t=0.08;
               end
               if score>100
                   t=0.05;
               end
               if score>150
                   t=0.02;
               end
               if score>220
                   t=0.01;
               end
                    pause(t);
                    cla
                    if all(snake_point(1,:)==goal_point)
                        snake_point=[snake_point;snake_tail];
                        draw(snake_point);
                        direction=[direction,direction(end)];
                        goal_point=[floor(25*rand),floor(25*rand)];
                        add_snake(goal_point(1),goal_point(2),'k');
                        score=score+10;
                        set(handles.edit1,'string',score);
                    end
                    cla
                    snake_tail=snake_point(end,:);
                    direction(2:end)=direction(1:end-1);
                    if  dir_change
                        direction(1)=dir_change;
                    end
                    snake_point=point_update(snake_point);
                    draw(snake_point);
                    add_snake(goal_point(1),goal_point(2),'k');
                    if if_cross(snake_point)
                       if score>max_score
                           max_score=score;
                        msgbox('恭喜你打破记录');
                        save('max_score.mat','max_score');
                       else
                           msgbox('少年,还得努力啊!');
                       end
                         clear global
                        return;
                    end
                    
                end
                function condition=if_cross(snake_point)
                    condition=0;
                    min_x=min(snake_point(:,1));
                    max_x=max(snake_point(:,1));
                    min_y=min(snake_point(:,2));
                    max_y=max(snake_point(:,2));
                    if min_x<0 || max_x>24 || min_y<0 || max_y>24
                        condition=1;
                        return
                    end
                    len=size(snake_point,1);
                    for i=1:len
                        for j=i+1:len
                            if snake_point(i,1)==snake_point(j,1) && snake_point(i,2)==snake_point(j,2)
                                condition=1;
                                return
                            end
                        end
                    end
                    
                    
                    function snake_point=point_update(snake_point)
                        global direction
                        for i=1:length(direction)
                            switch direction(i)
                                case 1
                                    snake_point(i,2)=snake_point(i,2)+1;
                                case 2
                                    snake_point(i,2)=snake_point(i,2)-1;
                                case 3
                                    snake_point(i,1)=snake_point(i,1)-1;
                                case 4
                                    snake_point(i,1)=snake_point(i,1)+1;
                            end
                        end
                        
                        % --- Executes on key press with focus on figure1 and none of its controls.
                        function add_snake(x,y,varargin)
                            if nargin==2
                                color='r';
                            else
                                color=varargin{1};
                            end
                            rectangle('Position',[x,y,1,1],'facecolor',color);
                            
                            function draw(snake_point)
                                line=size(snake_point,1);
                                for i=1:line
                                    add_snake(snake_point(i,1),snake_point(i,2));
                                end
                                
                                % --- Executes on key press with focus on figure1 or any of its controls.
                                function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
                                    % hObject    handle to figure1 (see GCBO)
                                    % eventdata  structure with the following fields (see FIGURE)
                                    %	Key: name of the key that was pressed, in lower case
                                    %	Character: character interpretation of the key(s) that was pressed
                                    %	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
                                    % handles    structure with handles and user data (see GUIDATA)
                                    global dir_change;
                                    if strcmp(eventdata.Key,'uparrow')
                                        dir_change=1;
                                    end
                                    if strcmp(eventdata.Key,'downarrow')
                                        dir_change=2;
                                    end
                                    if strcmp(eventdata.Key,'leftarrow')
                                        dir_change=3;
                                    end
                                    if strcmp(eventdata.Key,'rightarrow')
                                        dir_change=4;
                                    end
                                    



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)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- 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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end