f = figure('Name','Snake',...
'Numbertitle','off',...
'Menubar','none',...
'Color',[.95 .95 .95],...
'Colormap',[.51 .255 .51;.4 .4 .4;0 0 0;1 1 0],...
'CloseRequestFcn',sprintf('%s(''Stop'');closereq;',mfilename),...
'KeyPressFcn',sprintf('%s(double(get(gcbf,''Currentcharacter'')))',mfilename));
FileMenu = uimenu(f,'Label','&File');
uimenu(FileMenu,'Label','New Game','Accelerator','N','Callback',sprintf('%s(''NewGame'')',mfilename));
uimenu(FileMenu,'Label','Exit','Accelerator','Q','Separator','on','Callback',sprintf('%s(''Stop'');closereq',mfilename));
LevelMenu = uimenu(f,'Label','&Difficulty');
uimenu(LevelMenu,'Label','Temple','Callback',sprintf('%s(''Level'')',mfilename),'checked','on')
uimenu(LevelMenu,'Label','UPenn...','Callback',sprintf('%s(''Level'')',mfilename))
uimenu(LevelMenu,'Label','Drexel University','Callback',sprintf('%s(''Level'')',mfilename))
MazesMenu = uimenu(f,'Label','&Mazes');
uimenu(MazesMenu,'Label','No maze','Callback',sprintf('%s(''Mazes'')',mfilename),'checked','on')
uimenu(MazesMenu,'Label','Box','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Tunnel','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Spiral','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Blockade','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Twisted','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Heart','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Drexel','Callback',sprintf('%s(''Mazes'')',mfilename))
uimenu(MazesMenu,'Label','Checkers','Callback',sprintf('%s(''Mazes'')',mfilename))
OptionMenu = uimenu(f,'Label','&Options');
uimenu(OptionMenu,'Label','Show Stat','Callback',sprintf('%s(''ShowScore'')',mfilename),'Separator','on','Checked','on')
axes('Units','normalized',...
'Position', [0 0 1 1],...
'Visible','off',...
'DrawMode','fast',...
'NextPlot','replace');
DRAW_WINDOW = image(getTitle,'CDataMapping','scaled');
axis image
set(gca,...
'XTick',NaN,...
'YTick',NaN)
text(40,30,'0',...
'FontUnits','normalized', ...
'FontSize',0.03, ...
'FontName','FixedWidth',...
'FontWeight','bold',...
'Color',[1 1 1],...
'VerticalAlignment','baseline', ...
'HorizontalAlignment','right',...
'Tag','Stat');
SNAKE_BODY = [14,20;14,19;14,18;14,17;14,16];
MAZE = zeros(30,40);
SPEED_OF_LEVEL = 4;
EAT_SOUND = true;
case 28
if SNAKE_BODY(2,2)~=mod(SNAKE_BODY(1,2)-2,40)+1
SNAKE_DIRECTION = cmd;
end
case 29
if SNAKE_BODY(2,2)~=mod(SNAKE_BODY(1,2),40)+1
SNAKE_DIRECTION = cmd;
end
case 30
if SNAKE_BODY(2,1)~=mod(SNAKE_BODY(1,1)-2,30)+1
SNAKE_DIRECTION = cmd;
end
case 31
if SNAKE_BODY(2,1)~=mod(SNAKE_BODY(1,1),30)+1
SNAKE_DIRECTION = cmd;
end
case 'Drexel University'
Drexel = ('||||||||||||||||||');
case 'Level'
set(get(get(gcbo,'Parent'),'Children'),'checked','off')
set(gcbo,'checked','on')
SPEED_OF_LEVEL = length(get(gcbo,'Label'));
case 'Mazes'
set(get(get(gcbo,'Parent'),'Children'),'checked','off')
set(gcbo,'checked','on')
MAZE = zeros(30,40);
switch get(gcbo,'Label')
case 'No maze'
case 'Box'
MAZE([1 30],:) = 1;
MAZE(:,[1 40]) = 1;
case 'Tunnel'
MAZE([1:4 26:30],[1 40]) = 1;
MAZE([1 30],[1:4 36:40]) = 1;
MAZE([10 20],15:25) = 1;
case 'Spiral'
MAZE(1:15,15) = 1;
MAZE(15:30,25) = 1;
MAZE(25,1:15) = 1;
MAZE(5,25:40) = 1;
case 'Blockade'
MAZE([1:10 end-10:end],[1 end]) = 1;
MAZE([1 end],:) = 1;
MAZE([8 22],15:25) = 1;
MAZE(5:10,[10 30]) = 1;
MAZE(20:25,[10 30]) = 1;
case 'Twisted'
MAZE([1 2 3 9 16 31 39 46 61 69 76 91 99 106 121 129 ...
136 159 166 189 196 219 226 249 256 279 286 301 309 ...
316 331 339 346 361 369 376 391 399 406 421 429 436 ...
451 459 466 481 489 496 511 512 513 514 515 516 517 ...
518 519 526 541 556 571 586 601 616 631 646 661 676 ...
677 678 679 680 681 682 683 684 685 686 687 688 689 ...
690 691 699 706 721 729 736 751 759 766 781 789 796 ...
811 819 826 841 849 856 871 879 886 901 909 916 931 ...
939 946 969 976 999 1006 1029 1036 1059 1066 1089 ...
1096 1119 1126 1149 1156 1179 1186]) = 1;
case 'BARRIER'
MAZE ([518 519 526 541 556 571 586 601 616 631 646 661]) =1;
MAZE(20:25,[10 30]) = 1;
MAZE(676:961) = 1;
case 'Checkers'
MAZE([ 217 218 221 222 225 226 229 230 233 234 247 ...
248 251 252 255 256 259 260 263 264 337 338 341 342 ...
345 346 349 350 353 354 367 368 371 372 375 376 379 ...
380 383 384 457 458 461 462 469 470 473 474 487 488 ...
491 492 499 500 503 504 577 578 581 582 589 590 593 ...
594 607 608 611 612 619 620 623 624 697 698 701 702 ...
709 710 713 714 727 728 731 732 739 740 743 744 817 ...
818 821 822 825 826 829 830 833 834 847 848 851 852 ...
855 856 859 860 863 864 937 938 941 942 945 946 949 ...
950 953 954 967 968 971 972 975 976 979 980 983 984 ...
]) = 1;
case 'Heart'
MAZE([336 337 338 339 340 365 371 395 402 425 433 455 464 ...
485 495 515 526 546 557 577 588 608 619 638 649 667 678 ...
696 707 725 755 785 815 845 852 875 881 906 907 908 909 910]) = 1;
case 'Drexel'
MAZE([188 202 218 219 220 221 222 223 225 226 227 228 229 230 231 232 ...
248 278 308 338 368 398 428 429 430 431 432 433 435 436 437 438 ...
439 440 441 442 412 382 352 322 292 262 698 699 700 701 702 703 ...
705 706 707 708 709 710 711 712 742 772 802 832 862 892 922 921 920 ...
919 918 917 916 915 913 912 911 910 909 908 938 668
]) = 1;
end
feval(mfilename,'Stop')
case 'ShowScore'
switch get(gcbo,'checked')
case 'on'
set(gcbo,'checked','off')
set(findobj(gcbf,'Tag','Stat'),'Visible','off')
case 'off'
set(gcbo,'checked','on')
set(findobj(gcbf,'Tag','Stat'),'Visible','on')
end
case 'NewGame'
set(findobj(gcbf,'Tag','Stat'),'String','0')
SNAKE_BODY = [14,20;14,19;14,18;14,17;14,16];
SNAKE_DIRECTION = 29;
BONUS = 0;
PAUSE = false;
feval(mfilename,'Food')
feval(mfilename,'Start')
case 'Start'
SNAKE_SPEED = true;
bonusCounter = 0;
foodCounter = 0;
while(SNAKE_SPEED)
if ~PAUSE
SNAKE_BODY = circshift(SNAKE_BODY,1);
SNAKE_BODY(1,:) = SNAKE_BODY(2,:);
switch SNAKE_DIRECTION
case 28
SNAKE_BODY(1,2) = mod(SNAKE_BODY(1,2)-2,40)+1;
case 29
SNAKE_BODY(1,2) = mod(SNAKE_BODY(1,2),40)+1;
case 30
SNAKE_BODY(1,1) = mod(SNAKE_BODY(1,1)-2,30)+1;
case 31
SNAKE_BODY(1,1) = mod(SNAKE_BODY(1,1),30)+1;
end
if MAZE(SNAKE_BODY(1,1),SNAKE_BODY(1,2)) || ...
sum(ismember(SNAKE_BODY(2:end,1),SNAKE_BODY(1,1))+...
ismember(SNAKE_BODY(2:end,2),SNAKE_BODY(1,2))==2)
pause(.3)
delete(findobj(gcbf,'Tag','Bonus'))
feval(mfilename,'Stop')
set(DRAW_WINDOW,'CData',getGameOver)
else
if isequal(SNAKE_BODY(1,:),BONUS)
scorehandle = findobj(gcbf,'Tag','Stat');
set(scorehandle,'String',...
num2str(SPEED_OF_LEVEL*ceil(bonusCounter/3)+...
str2double(get(scorehandle,'String'))))
bonusCounter = 1;
end
if BONUS
bonusCounter = bonusCounter-1;
if bonusCounter<=0
delete(findobj(gcbf,'Tag','Bonus'))
BONUS = 0;
end
end
if isequal(SNAKE_BODY(1,:),FOOD)
SNAKE_BODY(end+1,:) = SNAKE_BODY(end,:);
scorehandle = findobj(gcbf,'Tag','Stat');
set(scorehandle,'String',...
num2str(SPEED_OF_LEVEL+str2double(get(scorehandle,'String'))))
feval(mfilename,'Food')
if ~BONUS
bonusCounter = bonusCounter+35;
foodCounter = foodCounter+1;
end
if foodCounter==4
feval(mfilename,'Bonus')
foodCounter = 0;
end
end
feval(mfilename,'DrawBoard')
end
end
pause(.3/SPEED_OF_LEVEL)
end
case {112 32}
PAUSE=~PAUSE;
if PAUSE && SNAKE_SPEED
set(DRAW_WINDOW,'CData',getPause)
end
case 'Stop'
SNAKE_SPEED = false;
set(DRAW_WINDOW,'CData',getTitle)
case 'Food'
CData = MAZE;
for i=1:size(SNAKE_BODY,1)
CData(SNAKE_BODY(i,1),SNAKE_BODY(i,2)) = 1;
end
ind = find(CData'==0);
ind = ind(ceil(rand*length(ind)));
FOOD = [ceil(ind/40) mod(ind-1,40)+1];
case 'Bonus'
delete(findobj(gcbf,'Tag','Bonus'))
CData = MAZE;
for i=1:size(SNAKE_BODY,1)
CData(SNAKE_BODY(i,1),SNAKE_BODY(i,2)) = 1;
end
CData(FOOD(1,1),FOOD(1,2)) = 1;
ind = find(CData'==0);
ind = ind(ceil(rand*length(ind)));
BONUS = [ceil(ind/40) mod(ind-1,40)+1];
text(BONUS(2),BONUS(1),'\heartsuit',...
'Color',[1 0 0],...
'FontUnits','normalized',...
'FontSize',.065,...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle',...
'Tag','Bonus')
case 'DrawBoard'
CData = MAZE;
for i=1:size(SNAKE_BODY,1)
CData(SNAKE_BODY(i,1),SNAKE_BODY(i,2)) = 2;
end
CData(FOOD(1),FOOD(2)) = 4;
set(DRAW_WINDOW,'CData',CData)