- 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)');
- figure(gcf);
- m=50;
- %===Exceeding highscore movie variables==========
- a(1:m+2)=zeros(1,m+2);b(1:m+2)=(m+1)*ones(1,m+2);
- c(1:m+2)=0:m+1;d(1:m+2)=m+2-[1:m+2];
- %===Snake game initial variables===================
- GmData=get(AssHndl,'UserData');
- i=GmData{1};
- j=GmData{2};
- ih=GmData{3};
- jh=GmData{4};
- k=GmData{5};
- timer=GmData{6};
- incrmnt=GmData{7};
- score=GmData{8};
- level=GmData{9};
- ir=GmData{10};
- jr=GmData{11};
- xdrn=GmData{12};
- ydrn=GmData{13};
- PausGm=2*GmData{14}+strcmp(get(ValNumber,'SelectionType'),'alt');
- %===for debugging purposes============================
- ChitStr=get(uic(4),'string');
- if length(ChitStr)>11
- if strcmp(ChitStr(1:8),'teleport')&str2num(ChitStr(9:12))
- ih=str2num(ChitStr(9:10));
- jh=str2num(ChitStr(11:12));end;end;
- if strcmp(ChitStr,'flipflop')
- i=fliplr(i);
- j=fliplr(j);
- ih=i(1);
- jh=j(1);end;
- if length(ChitStr)>3
- if strcmp(ChitStr(1:4),'tree')&str2num(ChitStr(5:end))
- vetka=min(length(i),str2num(ChitStr(5:end)));
- ih=i(vetka);
- jh=j(vetka);end;end;
- if length(ChitStr)>3
- if strcmp(ChitStr(1:4),'jump')&str2num(ChitStr(5:6))
- jmpdst=str2num(ChitStr(5:6));
- ih=ih+jmpdst*xdrn;
- jh=jh+jmpdst*ydrn;end;end;
- %Play area clicking toggle
- set(AssHndl,'UserData',1);
- %==Helper variables============
- ibdy=i(3:end);jbdy=j(3:end);
- %===Colors====================================
- SnakColor=[(cos(level*pi/100))^2 (sin(level*pi/130))^2 (cos(level*pi/60))^2];
- HeadColor=1-SnakColor;
- PreyColor=[0.5+timer/218 timer/109 0];
- %===Drawing initial snake`s body===========================
- PlotHndl=plot(ibdy,jbdy,'.', ...
- 'EraseMode','none', ...
- 'color',SnakColor, ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- %===Drawing initial snake`s head==========================
- HeadHndl=plot(i(1:2),j(1:2),'.', ...
- 'EraseMode','none', ...
- 'color',HeadColor, ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- %===Drawing initial "Prey" ================================
- Prey=plot(ir,jr,'.', ...
- 'EraseMode','xor', ...
- 'color',PreyColor, ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- axis([0 m+1 0 m+1]);
- %===Initial values of play variables=============
- CMP=1;
- %==="Player","Snakemaster","SCORE","HIGHSCORE" and "LEVEL" indicators===============
- set(ScoreHndl, ...
- 'Visible','on', ...
- 'String',int2str(score));
- set(LevelHndl, ...
- 'Visible','on', ...
- 'String',int2str(level));
- %===Slider, and indicators label======
- set(uic([5 14]),'Visible','on');
- %Comment window===========================================
- GoStr= ...
- [' Go! '];
- set(uic(4),'String',GoStr);
- drawnow;
- %===THE MAIN LOOP=================================
- while [ih>0 ih<=m jh>0 jh<=m nnz(CMP)==1 ~PausGm]
- %===Tail erasing variables================
- itl=i(end);jtl=j(end);
- %===Make sure the button pressed inside the "Play area"==
- currPt=get(AssHndl,'CurrentPoint');
- xPt=currPt(1,1);yPt=currPt(1,2);
- btndn=get(AssHndl,'UserData');
- if [xPt>=0 xPt<=m+1 yPt>=0 yPt<=m+1 btndn]
- %===Evaluating variables of snake direction==
- dx=sign(xPt-ih);dy=sign(yPt-jh);
- xdrn=(~xdrn)*dx+(~dy)*xdrn;
- ydrn=(~ydrn)*dy+(~dx)*ydrn;
- set(AssHndl,'UserData',0);
- end;
- %=====Next step routine==========================
- engine;
- %=====Running text tray==========================
- RunStr=[RunStr(2:end) RunStr(1)];
- set(uic(12),'Visible','on', ...
- 'String',RunStr(1:min([50 length(RunStr)])));
- %===Snake's speed loop=======================
- ratio=get(uic(14),'value');
- pause(0.20-ratio*0.18);
- % for count=1:(15000-ratio*10000)
- % r=rand;
- % end;
- end;
- %===END OF MAIN LOOP=================================
- %================Pause Game===========================
- if PausGm
- GmData={i j ih jh k timer incrmnt score level ir jr xdrn ydrn 0};
- set(AssHndl, ...
- 'UserData',GmData, ...
- 'ButtonDownFcn','snake(''play'');');
- set([PlotHndl HeadHndl Prey],'ButtonDownFcn','snake(''play'');');
- ResumStr= ...
- [' The game paused. '
- ' Press inside the play area to resume.'
- ' You can save this game for a while. '];
- set(uic(4),'String',ResumStr);
- %===If the game only begins====================================
- if PausGm>1
- InsdprStr= ...
- [' Press inside the play area when ready '];
- set(uic(4),'String',InsdprStr);
- end;
- %======================================================
- else
- if score>highscr
- %===Highscore exceeding movie================
- while m>1
- m=m-1;
- a=a(2:m+2)+1;
- b=b(2:m+2)-1;
- c=c(2:m+2);
- d=d(2:m+2);
- plot([a c b d],[c b d a],'.', ...
- 'markersize',25,'color',[0.02*m 0 1-0.02*m]);
- end;
- text(25,35,[Name '!'],'color','g', ...
- 'HorizontalAlignment','center', ...
- 'FontSize',20,'FontWeight','bold');
- text(25,28,'You have exceeded the highscore!', ...
- 'HorizontalAlignment','center');
- text(25,24,['A new highscore is ' int2str(score)], ...
- 'HorizontalAlignment','center');
- text(25,20,'Press a button to play again', ...
- 'HorizontalAlignment','center');
- %===Message in comment window=========================
- ExcStr= ...
- [' Congradulations!. '
- ' You have made the best result. '
- ' Now you are the snakemaster! '
- ' Press a button '];
- set(uic(4),'String',ExcStr);
- %===Saves a new highscore and a new snakemaster========
- fid=fopen('highscor.txt','w');
- fprintf(fid,'%6d',score);
- fid=fopen('bestplyr.txt','w');
- fprintf(fid,'%s',Name);
- else
- %==="Game Over"========================================
- set(PlotHndl,'xdata',i,'ydata',j);
- text(25,28,' G A M E O V E R ', ...
- 'HorizontalAlignment','center', ...
- 'FontWeight','bold','FontSize',20,'color','b');
- %===Message in comment window==========================
- SheetStr= ...
- [' You loose like a piece of shit! '
- ' Press a button to try again, Ass hole! '];
- set(uic(4),'String',SheetStr);
- end;
- %===prepares to start a new game=======================
- pause(0.5);
- set(AssHndl,'ButtonDownFcn','snake(''NewGame'');');
- end;
- %===Demonstration session===============================================
- elseif strcmp(action,'Demo')
- cla;
- AssHndl=gca;
- ValNumber=gcf;
- %===Prepares screen=================================
- set(AssHndl,'color','b');
- set(AssHndl,'color',[0.4 0.8 0.4]);
- uic=get(ValNumber,'UserData');
- PromptHndl=uic(4);
- ScoreHndl=uic(6);
- LevelHndl=uic(7);
-
- %===Player introducing removing======================
- set(uic([1:3 8:11 13 14]),'Visible','off');
- set(uic([17:19 21 22]),'Enable','off');%"save property"
- set(uic(5),'Visible','on');
- %===Running text=============================================
- RunStr=['**** Demonstration ******** Demonstration ****'];
- set(uic(12),'Visible','on', ...
- 'String',RunStr);
- %===Uncover the figure===============================
- set(AssHndl, ...
- 'DrawMode','fast', ...
- 'Visible','on', ...
- 'Box','on', ...
- 'Xtick',[],'Ytick',[], ...
- 'UserData',0, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- figure(gcf);
- m=50;
- %===Snake game initial variables===================
- 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;
- %===helper variables=
- ibdy=i;jbdy=j;
- ibdy(1:2)=[];jbdy(1:2)=[];
- %===Colors====================================
- SnakColor=[(cos(level*pi/100))^2 (sin(level*pi/130))^2 (cos(level*pi/60))^2];
- HeadColor=1-SnakColor;
- PreyColor=[0.5+timer/218 timer/109 0];
- %===Drawing initial snake`s body===========================
- PlotHndl=plot(ibdy,jbdy,'.', ...
- 'EraseMode','none', ...
- 'color','m', ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- %===Drawing initial snake`s head==========================
- HeadHndl=plot(i(1:2),j(1:2),'.', ...
- 'EraseMode','none', ...
- 'color','r', ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- %===Drawing initial "Prey" ================================
- Prey=plot(ir,jr,'.', ...
- 'EraseMode','xor', ...
- 'color','y', ...
- 'MarkerSize',15, ...
- 'ButtonDownFcn','set(gca,''UserData'',1)');
- axis([0 m+1 0 m+1]);
- %===Initial values of play variables=============
- CMP=1;
- %==="Player","Snakemaster","SCORE","HIGHSCORE" and "LEVEL" indicators===============
- set(ScoreHndl, ...
- 'Visible','on', ...
- 'String',int2str(score));
- set(LevelHndl, ...
- 'Visible','on', ...
- 'String',int2str(level));
- GoStr= ...
- [' Go! '];
- set(PromptHndl,'String',GoStr);
- %===THE MAIN LOOP=================================
- while [ih>0 ih<=m jh>0 jh<=m nnz(CMP)==1 ~get(AssHndl,'UserData')]
- %===Tail erasing variables================
- itl=i(k);jtl=j(k);
- %===snake demo engine========================
- dx=sign(ir-ih);dy=sign(jr-jh);
- if dx*xdrn+dy*ydrn<=0
- xdrn=(~xdrn)*dx+(~dy)*xdrn;
- ydrn=(~ydrn)*dy+(~dx)*ydrn;
- end;
- WUS=sparse(i,j,[1:k],m,m);
- WUS=[ones(2,m);WUS;ones(2,m)];
- WUS=[ones(m+4,2) WUS ones(m+4,2)];
- isrnd=[1 -1 0 0];jsrnd=[0 0 1 -1];
- srnd=[WUS(ih+3,jh+2) WUS(ih+1,jh+2) WUS(ih+2,jh+3) WUS(ih+2,jh+1)];
- iwu=ih+2+xdrn;jwu=jh+2+ydrn;
- if WUS(iwu,jwu)
- if [nnz(srnd)==2 xdrn==(ih-i(2)) ydrn==(jh-j(2))]
- pwus=WUS(iwu+(~xdrn),jwu+(~ydrn));
- mwus=WUS(iwu-(~xdrn),jwu-(~ydrn));
- drn=pwus*(pwus-WUS(iwu,jwu))+mwus*(WUS(iwu,jwu)-mwus);
- xdrn=(~xdrn)*sign(drn);
- ydrn=(~ydrn)*sign(drn);
- elseif nnz(srnd)==3
- srnd=find(~srnd);
- xdrn=isrnd(srnd);
- ydrn=jsrnd(srnd);
- else
- xdrn=ih-i(2);
- ydrn=jh-j(2);
- end;
- end;
- %===Next step routine=========
- engine;
- %====Running text=======================
- RunStr=[RunStr(2:end) RunStr(1)];
- set(uic(12),'String',RunStr);
- %===Snake`s speed============
- pause(0.025);
- end;
- %===END OF MAIN LOOP=============
- snake('NewGame');
- %===Starts a new game===================================================
- elseif strcmp(action,'NewGame')
- AssHndl=gca;
- ValNumber=gcf;
- %===Loads interface data============================
- uic=get(ValNumber,'UserData');
- %===Clears/Defaults interface=======================
- cla;
- set(uic([5:7 12 14]),'Visible','off');
- set(AssHndl,'Visible','off');
- set(uic(17:22),'Enable','on');
- set(uic(19),'Enable','off');%"save property"
- %==Introduction comment======================
- text(25,46,' Wan`t you play ', ...
- 'HorizontalAlignment','center','color','w');
- text(25,42,' one more time? ', ...
- 'HorizontalAlignment','center','color','w');
- set(uic([1:3 13]),'Visible','on');
- 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. '];
- set(uic(4),'String',PromptStr);
- %===Changes the poster=========================
- FgRs=get(0,'children');
- if any(FgRs==uic(15))
- delete(uic(15));
- end;
-
- %=====Save the game===================================
- elseif strcmp(action,'save')
- AssHndl=gca;
- ValNumber=gcf;
- %===Loads interface data============================
- uic=get(ValNumber,'UserData');
- GmData=get(AssHndl,'UserData');
- i=GmData{1};
- j=GmData{2};
- ih=GmData{3};
- jh=GmData{4};
- k=GmData{5};
- timer=GmData{6};
- incrmnt=GmData{7};
- score=GmData{8};
- level=GmData{9};
- ir=GmData{10};
- jr=GmData{11};
- xdrn=GmData{12};
- ydrn=GmData{13};
- %Player`s name==================
- Name=get(uic(8),'string');
- %Opens data file and saves=======================
- SavInfo=[timer score ir xdrn i
- incrmnt level jr ydrn j];
- fid=fopen([Name '.txt'],'w');
- fprintf(fid,'%3d %3d\n',SavInfo);
- set(uic(4),'String',' The game saved. ');
- set(AssHndl, ...
- 'UserData',GmData, ...
- 'ButtonDownFcn','snake(''play'');');
-
- %===Opens the information window========================================
- elseif strcmp(action,'info'),
- helpwin(mfilename);
- %===Quits the game===========================
- elseif strcmp(action,'close')
- AssHndl=gca;
- ValNumber=gcf;
- %===Loads interface data============================
- uic=get(ValNumber,'UserData');
- FimHndl=uic(15);
- FgRs=get(0,'children');
- if any(FgRs==FimHndl)
- delete(FimHndl);
- end;
- close(gcf);
- end;
- %=========================================================================
- %========================END OF THE PROGRAM===============================
- %=========================================================================
复制代码 |