用matlab编写的一个贪食蛇游戏
该文件需要存为engine.m%=====This script is used with "snake.m" as a part of the program
%=====By Valery Garmider=========================================
%=====Updated 27/03/03===========================================
%===Evaluates next values of snake's head coordinates==
ih=ih+xdrn;
jh=jh+ydrn;
%===The snake catches a prey==============
if
%===Evaluates next prey coordinates==
SNK=sparse(i,j,1,m,m);
=find(~SNK);
pr=floor((m^2-k)*rand)+1;
ir=nsi(pr);
jr=nsj(pr);
set(Prey,'xdata',ir,'ydata',jr,'Color','y');
%===Updates comment window===========
if timer>9
set(uic(4),'String',' Catched it! Go! ');
else
set(uic(4),'String','At last! Come on! Move your ass!');
end;
%===Updates play variables=========
score=score+floor(timer/10);
level=level+1;
timer=109;
incrmnt=incrmnt-6;
%===Define colors=======================
SnakColor=[(cos(level*pi/100))^2(sin(level*pi/130))^2(cos(level*pi/60))^2];
HeadColor=1-SnakColor;
%===Updates score and level indicators==
set(ScoreHndl,'String',int2str(score));
set(LevelHndl,'String',int2str(level));
end;
%===Updates snake image=====================
if incrmnt<6
incrmnt=incrmnt+1;
k=k+1;
i=;
j=;
else
i=;
j=;
set(PlotHndl,'xdata',itl,'ydata',jtl,'color',);
end;
set(HeadHndl,'xdata',ih,'ydata',jh,'color',HeadColor);
set(HeadHndl,'xdata',i(3),'ydata',j(3),'color',SnakColor);
set(PlotHndl,'xdata',i(3),'ydata',j(3),'color',SnakColor);
PreyColor=;
set(Prey,'Color',PreyColor);
%==="A stupid snake catches his own tail" indicator=====
CMP=(i==ih)&(j==jh);
%===Pause the game======================================
PausGm=strcmp(get(ValNumber,'SelectionType'),'alt');
%===Decreasing the score timer===============
if timer>9
timer=timer-1;
end;
drawnow;
%===Some comments============================
%if xPt<0|xPt>m+1|yPt<0|yPt>m+1
% InsdprStr= ...
% [' Press inside the play area! '];
% set(uic(4),'String',InsdprStr);
if
set(uic(4),'String',' Hurry!');
elseif timer==9
LateStr= ...
[' Too late! '
' Next time move your ass quicker! '];
set(uic(4),'String',LateStr);
end;
[ 本帖最后由 suffer 于 2006-11-15 15:28 编辑 ] 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 anypoints 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=;
AssHndl=axes( ...
'Units','normalized', ...
'Position',AssPstn, ...
'Visible','off', ...
'DrawMode','fast', ...
'NextPlot','add', ...
'Color',, ...
'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',);
uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',, ...
'BackgroundColor',);
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',, ...
'ForeGroundColor',, ...
'Position',, ...
'String',PromptStr);
%==="SCORE" and "LEVEL" indicators===========
ScrPstn=;
ScoreHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',, ...
'ForeGroundColor',, ...
'Position',ScrPstn, ...
'Visible','off');
LevelHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',, ...
'ForeGroundColor',, ...
'Position',, ...
'Visible','off');
%===Name typing area========================
FrmHndl=uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',, ...
'BackgroundColor',, ...
'Visible','off');
IntrdcHndl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',, ...
'ForegroundColor',, ...
'Position',, ...
'Visible','off');
NameHndl=uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',, ...
'Position',, ...
'Visible','off');
%===The OK button===========================
OKHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',, ...
'Interruptible','on', ...
'String','OK', ...
'Callback','snake(''play'');', ...
'Visible','off');
%===The Speed slider========================
IndPstn=;
IndString=['score speed level'];
IndHndl=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Max',10, ...
'BackgroundColor',, ...
'ForegroundColor',, ...
'Position',IndPstn, ...
'String',IndString, ...
'Visible','off');
SpdLngth=0.2;
SpdPstn=;
SpeedHndl=uicontrol( ...
'Style','slider', ...
'Units','normalized', ...
'Position',SpdPstn, ...
'Interruptible','on', ...
'Visible','off', ...
'value',0.5);
%===Buttons information=====================
BtnWidth=0.23;
BtnHeight=0.06;
BtnSize=;
BtnSpace=(1.00-BtnWidth)/2;
Dstnc=BtnHeight+0.03;
%===The START button========================
StartHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',, ...
'Interruptible','on', ...
'String','new', ...
'Callback','snake(''start'');');
%===The INFO button========================
InfoHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',, ...
'Interruptible','on', ...
'String','info', ...
'Callback','snake(''info'');');
%===The CLOSE button========================
CloseHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',, ...
'Interruptible','on', ...
'String','exit', ...
'Callback','snake(''close'');');
%===The LOAD button========================
LOADHndl=uicontrol( ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',, ...
'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',, ...
'ForegroundColor',, ...
'Position',, ...
'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(),'Visible','off');
set(AssHndl,'Visible','off');
set(uic(19),'Enable','off');%"save property"
set(uic(),'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=;j=;
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 levelir 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(),'Visible','off');
set(AssHndl,'Visible','off');
set(uic(19),'Enable','off');%"save property"
set(uic(),'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()
WrngStr= ...
['Can`t find such a saved game '
'Type a correct name! '];
set(uic(4),'String',WrngStr);
else
%===Loades game data==================
fid=fopen(,'r');
SnkInf=fscanf(fid,'%3d %3d\n',);
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 levelir 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
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()));
%===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-;
%===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=;
%===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();
%===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(),'Visible','on');
%Comment window===========================================
GoStr= ...
[' Go! '];
set(uic(4),'String',GoStr);
drawnow;
%===THE MAIN LOOP=================================
while
%===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
%===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=;
set(uic(12),'Visible','on', ...
'String',RunStr(1:min()));
%===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 levelir jr xdrn ydrn 0};
set(AssHndl, ...
'UserData',GmData, ...
'ButtonDownFcn','snake(''play'');');
set(,'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(,,'.', ...
'markersize',25,'color',);
end;
text(25,35,,'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 EO 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',);
uic=get(ValNumber,'UserData');
PromptHndl=uic(4);
ScoreHndl=uic(6);
LevelHndl=uic(7);
%===Player introducing removing======================
set(uic(),'Visible','off');
set(uic(),'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=;j=;
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=;
%===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();
%===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
%===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,,m,m);
WUS=;
WUS=;
isrnd=;jsrnd=;
srnd=;
iwu=ih+2+xdrn;jwu=jh+2+ydrn;
if WUS(iwu,jwu)
if
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=;
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(),'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(),'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(,'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===============================
%========================================================================= 是不是很战内存啊? 这么强吗!顶一下 强人啊,佩服! 原帖由 lxq 于 2006-11-13 12:52 发表
是不是很战内存啊?
还可以了 没话说 学习中
好像还存在一些问题
原帖由 xjtu211 于 2006-11-15 12:59 发表程序没有什么问题,不过发布的时候少申明了一点
第一个文件必须存engine.m 第二个代码存成snake.m行吗?
然后运行snake.m
是这样的吗?
能具体解释一下吗?
我很菜 9楼的说法,好象不好用啊.
因为游戏过程中score不见变化啊
请问lz,能解释一下么? 原帖由 fenyunwd 于 2006-11-28 00:04 发表
9楼的说法,好象不好用啊.
因为游戏过程中score不见变化啊
请问lz,能解释一下么?
没有出现你所说的问题
没吃掉一个,score就会增加 原帖由 HolySaint 于 2006-11-25 12:34 发表
第二个代码存成snake.m行吗?
然后运行snake.m
是这样的吗?
能具体解释一下吗?
我很菜
是这样的
页:
[1]