📄 snake.m
字号:
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===============================
%=========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -