📄 snake.m
字号:
function snake(action)
%=======THE GREATEST GAME OF SNAKE==================
% Well known game from cellular telephones
% now also available on MATLAB.
%
% Pretty nice interface,
% mouse control,
% funny coments are displayed during the play.
%
% *ROOLS*
% All rules are familiar besides one :
% more time you waste to catch the "prey" - less points you get.
% Wasted too much time ? You don't get points at all.
% The comment window will notify you whenever you should hurry
% and whenever the maximal time is expired.
% In addition, the "prey" will change color - from yellow(you get 10 pts)
% to dark brown(you get 0 pts).
%
% *START A NEW GAME*
% Choose "New game" in the menubar.
% Then type your name (or anything else) in desired place.
%
% *MOUSE CONTROL*
% Just start to play - you`ll quickly get the point :
% leftclicking turns the snake towards the pointer,
% rightclicking pouses the game.
% Tip : rightclick whenever you miss the "prey",
% or if you want to "roll up" enormously long snake.
%
% *SAVE AND LOAD THE GAME*
% You can save the current game :
% just rightclick to pause the game and
% choose "Save game" in the menubar.
% Later you`ll be able to continue any saved game :
% choose "Continue game" in the menubar
% and type a name of the saved game in desired.
% Tip : before saving, be sure the snake "looks nice".
%
% *BEST RESULTS*
% If the player exceeds the highscore, his result and his name
% are stored in memory.
%
% *RUNNING TEXT*
% Under the "play area" you`ll see the text tray running.
% It displays the following : player`s name, best player`s
% (SnakeMaster`s) name and best result.
%
% *COMMENTS*
% On the bottom of the window you`ll see an area displaying
% comments guiding you in using the interface
% and some announcements during the play, for instance :
% when the time, desired to get any points for catching the "prey",
% is running out, the word "Hurry" appears.
%
% *DEMO*
% Choose "Demonstration" in the menubar to see a demoplay.
%
% *CREDITS*
% The "snake" game - version 1.03
% Created by Valery Garmider
% Updated 27/03/03
% Copyright (c) by Valery Garmider, Inc.
%
%===================================================
if nargin<1
action='initialize';
end;
%=================Initialize=======================
if strcmp(action,'initialize')
%=============================
Rsltn=get(0,'ScreenSize');
Ratio=1.65;
width=max(Rsltn)/4;
height=Ratio*width;
Pstn=[(Rsltn(3)-width)/2 (Rsltn(4)-height)/2 width height];
ValNumber=figure( ...
'name','The greatest game of Snake', ...
'NumberTitle','off', ...
'Visible','off', ...
'BackingStore','off', ...
'menubar','none', ...
'Color','k', ...
'Position',Pstn);
FrmHeight=0.25;
AssHeight=1.00/Ratio;
AssPstn=[0.00 FrmHeight 1.00 AssHeight];
AssHndl=axes( ...
'Units','normalized', ...
'Position',AssPstn, ...
'Visible','off', ...
'DrawMode','fast', ...
'NextPlot','add', ...
'Color',[0.4 0.8 0.4], ...
'UserData',1);
%==Introduction comment======================
text(0,0.85,' Want to play? ', ...
'HorizontalAlignment','center','color','w');
text(0,0.70,' Let`s play! ', ...
'HorizontalAlignment','center','color','w');
axis([-1 1 -1 1]);
%Create the menu
label = str2mat(...
'&Snake Tools', ...
'>&New game', ...
'>&Continue game', ...
'>&Save game', ...
'>&Demonstration', ...
'>&About Snake', ...
'>>---------', ...
'>&Quit Snake');
call = str2mat(...
'', ...
'snake(''start'');', ...
'set(gca,''UserData'',1);snake(''load'');', ...
'snake(''save'');', ...
'snake(''Demo'');', ...
'snake(''info'');', ...
'', ...
'snake(''close'')');
Menu=makemenu(ValNumber,label,call);
set(Menu(4),'Enable','off');
%===The comment window======================
FrmPstn=[0.00 FrmHeight+AssHeight 1.00, ...
1.00-FrmHeight-AssHeight];
uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',FrmPstn, ...
'BackgroundColor',[0.5 0.5 0.5]);
uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',[0.00 0.00 1.00 FrmHeight], ...
'BackgroundColor',[0.5 0.5 0.5]);
PromptStr= ...
['Press the "NEW" button to begin a new game,'
'the "LOAD" button to continue a saved game,'
'the "INFO" button for information, '
'the "EXIT" button to shut this folly down. '];
PromptHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.2 0.2 0.2], ...
'ForeGroundColor',[1.0 0.5 1.0], ...
'Position',[0.01 0.01 0.98 0.14], ...
'String',PromptStr);
%==="SCORE" and "LEVEL" indicators===========
ScrPstn=[0.06 FrmHeight+AssHeight+0.01 0.14 0.04];
ScoreHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.3 0.2 0.2], ...
'ForeGroundColor',[0.9 0.9 0.9], ...
'Position',ScrPstn, ...
'Visible','off');
LevelHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.3 0.2 0.2], ...
'ForeGroundColor',[0.9 0.9 0.9], ...
'Position',[0.94-ScrPstn(3) ScrPstn(2:4)], ...
'Visible','off');
%===Name typing area========================
FrmHndl=uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',[0.30 0.36 0.40 0.40], ...
'BackgroundColor',[0.5 0.5 0.5], ...
'Visible','off');
IntrdcHndl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.5 0.5 0.5], ...
'ForegroundColor',[0.8 0.8 0.8], ...
'Position',[0.31 0.65 0.38 0.05], ...
'Visible','off');
NameHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[1 1 1], ...
'Position',[0.32 0.55 0.36 0.04], ...
'Visible','off');
%===The OK button===========================
OKHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[0.43 0.40 0.14 0.08], ...
'Interruptible','on', ...
'String','OK', ...
'Callback','snake(''play'');', ...
'Visible','off');
%===The Speed slider========================
IndPstn=[0.01 FrmHeight+AssHeight+0.06 0.98 0.04];
IndString=['score speed level'];
IndHndl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.5 0.5 0.5], ...
'ForegroundColor',[0.8 0.8 0.8], ...
'Position',IndPstn, ...
'String',IndString, ...
'Visible','off');
SpdLngth=0.2;
SpdPstn=[0.5-SpdLngth FrmHeight+AssHeight+0.01 SpdLngth*2 0.04];
SpeedHndl=uicontrol( ...
'Style','slider', ...
'Units','normalized', ...
'Position',SpdPstn, ...
'Interruptible','on', ...
'Visible','off', ...
'value',0.5);
%===Buttons information=====================
BtnWidth=0.23;
BtnHeight=0.06;
BtnSize=[BtnWidth BtnHeight];
BtnSpace=(1.00-BtnWidth)/2;
Dstnc=BtnHeight+0.03;
%===The START button========================
StartHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[BtnSpace FrmHeight+Dstnc*4 BtnSize], ...
'Interruptible','on', ...
'String','new', ...
'Callback','snake(''start'');');
%===The INFO button========================
InfoHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[BtnSpace FrmHeight+Dstnc*2 BtnSize], ...
'Interruptible','on', ...
'String','info', ...
'Callback','snake(''info'');');
%===The CLOSE button========================
CloseHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[BtnSpace FrmHeight+Dstnc BtnSize], ...
'Interruptible','on', ...
'String','exit', ...
'Callback','snake(''close'');');
%===The LOAD button========================
LOADHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[BtnSpace FrmHeight+Dstnc*3 BtnSize], ...
'Interruptible','on', ...
'String','load', ...
'Value',1, ...
'Callback','set(gca,''UserData'',1);snake(''load'');');
%===Player`s name===========================
PlrHndl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',[0.5 0.5 0.5], ...
'ForegroundColor',[0.8 0.8 0.0], ...
'Position',[0.01 0.19 0.98 0.04], ...
'String','...check...', ...
'Visible','off');
%===Saves interface data====================
uic=[StartHndl;InfoHndl;CloseHndl;PromptHndl;IndHndl;ScoreHndl; ...
LevelHndl;NameHndl;OKHndl;FrmHndl;IntrdcHndl;PlrHndl;LOADHndl;SpeedHndl;0;Menu];
set(ValNumber,'Visible','on','UserData',uic);
%===Start a new game screen=========================
elseif strcmp(action,'start')
cla;
AssHndl=gca;
ValNumber=gcf;
%===Loads interface data============================
uic=get(ValNumber,'UserData');
cla;
set(uic([5:7 12 14]),'Visible','off');
set(AssHndl,'Visible','off');
set(uic(19),'Enable','off');%"save property"
set(uic([1:3 13]),'Visible','off');
set(uic(9),'Callback','snake(''play'');');
set(uic(11),'string','Type your name:');
%===Player introducing===============================
set(uic(8:11),'Visible','on');
set(uic(4),'string','Introduce yourself please.');
%===Game Data Initialize=====
m=50;
k=4;
ih=m/2;jh=m/2;
i=[ih ih-1 ih-2 ih-3];j=[jh jh jh jh];
score=0;
level=0;
ir=floor(m*rand)+1;
jr=floor(m*rand)+1;
xdrn=1;
ydrn=0;
timer=109;
incrmnt=6;
GmData={i j ih jh k timer incrmnt score level ir jr xdrn ydrn 1};
set(AssHndl,'UserData',GmData);
%===Load a saved game screen=========================
elseif strcmp(action,'load')
cla;
AssHndl=gca;
ValNumber=gcf;
%===Loads interface data============================
uic=get(ValNumber,'UserData');
cla;
set(uic([5:7 12 14]),'Visible','off');
set(AssHndl,'Visible','off');
set(uic(19),'Enable','off');%"save property"
set(uic([1:3 13]),'Visible','off');
set(uic(9),'Callback','snake(''load'');');
set(uic(11),'string','Load a saved game:');
%===Player introducing===============================
set(uic(8:11),'Visible','on');
Name=get(uic(8),'string');
if get(AssHndl,'UserData')
set(uic(4),'string','Type a name of a saved game');
set(AssHndl,'UserData',0);
%Check`s if saved game exists=======================
elseif ~exist([Name '.txt'])
WrngStr= ...
['Can`t find such a saved game '
'Type a correct name! '];
set(uic(4),'String',WrngStr);
else
%===Loades game data==================
fid=fopen([Name '.txt'],'r');
SnkInf=fscanf(fid,'%3d %3d\n',[2,inf]);
i=SnkInf(1,:);
j=SnkInf(2,:);
i(1:4)=[];j(1:4)=[];
ih=i(1);jh=j(1);
k=length(i);
timer=SnkInf(1,1);
incrmnt=SnkInf(2,1);
score=SnkInf(1,2);
level=SnkInf(2,2);
ir=SnkInf(1,3);
jr=SnkInf(2,3);
xdrn=SnkInf(1,4);
ydrn=SnkInf(2,4);
set(uic(13),'value',1);
GmData={i j ih jh k timer incrmnt score level ir jr xdrn ydrn 1};
set(AssHndl,'UserData',GmData);
snake('play');
end;
%===============Start===============================
elseif strcmp(action,'play')
cla;
AssHndl=gca;
ValNumber=gcf;
%===Loads interface data============================
uic=get(ValNumber,'UserData');
%score and level indicators=========================
ScoreHndl=uic(6);
LevelHndl=uic(7);
%===Player introducing removing======================
set(uic(8:11),'Visible','off');
set(uic(19),'Enable','on');%"save property"
%===Extracts value of highscore and best player`s name
if [exist('highscor.txt') exist('bestplyr.txt')]
fid=fopen('highscor.txt','r');
highscr=fscanf(fid,'%6d');
fid=fopen('bestplyr.txt','r');
bestplyr=fscanf(fid,'%s');
else
highscr=0;
bestplyr='Nobody';
end;
%Player`s name==================
Name=get(uic(8),'string');
%Running text=======================================
RunStr=[' **** Player: ' Name ' **** Best result: ', ...
int2str(highscr) ' **** SnakeMaster: ' bestplyr];
set(uic(12),'Visible','on', ...
'String',RunStr(1:min([50 length(RunStr)])));
%===Uncover the figure===============================
set(AssHndl, ...
'DrawMode','fast', ...
'Visible','on', ...
'Box','on', ...
'Xtick',[],'Ytick',[], ...
'ButtonDownFcn','set(gca,''UserData'',1)');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -