Splitting into multiple files

8 次查看(过去 30 天)
Victor Rojas
Victor Rojas 2016-3-11
I am trying to split a large piece of code into multiple files in a way that the main file calls the others and makes it work
function snakelast(cmd) %SNAKE
global Drexel SNAKE_BODY PAUSE MAZE SPEED_OF_LEVEL DRAW_WINDOW SNAKE_DIRECTION SNAKE_SPEED FOOD EAT_SOUND BONUS if ~nargin cmd = 'init'; end if ~(ischar(cmd)||isscalar(cmd)) return; end
switch cmd case 'init'
% MAKE BOARD
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));
% MENU COMPONENTS
% File
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));
% SPEED MENU
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))
% MAZE MENU
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))
%Options
OptionMenu = uimenu(f,'Label','&Options');
uimenu(OptionMenu,'Label','Show Stat','Callback',sprintf('%s(''ShowScore'')',mfilename),'Separator','on','Checked','on')
% MAKE TABLE
axes('Units','normalized',...
'Position', [0 0 1 1],...
'Visible','off',...
'DrawMode','fast',...
'NextPlot','replace');
% ADD THE WINDOW
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 % SNAKE MOVE LEFT
if SNAKE_BODY(2,2)~=mod(SNAKE_BODY(1,2)-2,40)+1
SNAKE_DIRECTION = cmd;
end
case 29 % SNAKE MOVE RIGHT
if SNAKE_BODY(2,2)~=mod(SNAKE_BODY(1,2),40)+1
SNAKE_DIRECTION = cmd;
end
case 30 % SNAKE MOVE UP
if SNAKE_BODY(2,1)~=mod(SNAKE_BODY(1,1)-2,30)+1
SNAKE_DIRECTION = cmd;
end
case 31 % SNAKE MOVE DOWN
if SNAKE_BODY(2,1)~=mod(SNAKE_BODY(1,1),30)+1
SNAKE_DIRECTION = cmd;
end
%DEFINE SPEED
case 'Drexel University'
Drexel = ('||||||||||||||||||');
case 'Level' % CHANGE SPEED
set(get(get(gcbo,'Parent'),'Children'),'checked','off')
set(gcbo,'checked','on')
SPEED_OF_LEVEL = length(get(gcbo,'Label'));
case 'Mazes' % CHANGE MAZE
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' % SCORE CALCULATOR
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' % SHORTCUT FOR NEW GAME
set(findobj(gcbf,'Tag','Stat'),'String','0')
SNAKE_BODY = [14,20;14,19;14,18;14,17;14,16];
SNAKE_DIRECTION = 29; % right
BONUS = 0;
PAUSE = false;
feval(mfilename,'Food')
feval(mfilename,'Start')
case 'Start' % STARTS GAME
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 % left
SNAKE_BODY(1,2) = mod(SNAKE_BODY(1,2)-2,40)+1;
case 29 % right
SNAKE_BODY(1,2) = mod(SNAKE_BODY(1,2),40)+1;
case 30 % up
SNAKE_BODY(1,1) = mod(SNAKE_BODY(1,1)-2,30)+1;
case 31 % down
SNAKE_BODY(1,1) = mod(SNAKE_BODY(1,1),30)+1;
end
% BARRIER CHECK
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
% BONUS EATEN CHECK
if isequal(SNAKE_BODY(1,:),BONUS)
% Update score
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
% SNAKE FOOD EATEN CHECK
if isequal(SNAKE_BODY(1,:),FOOD)
% ADJESTS SNAKE GROWTH
SNAKE_BODY(end+1,:) = SNAKE_BODY(end,:);
% SCORE UPDATOR
scorehandle = findobj(gcbf,'Tag','Stat');
set(scorehandle,'String',...
num2str(SPEED_OF_LEVEL+str2double(get(scorehandle,'String'))))
% SPAWN NEW FOOD ON FIELD
feval(mfilename,'Food')
if ~BONUS %
bonusCounter = bonusCounter+35;
foodCounter = foodCounter+1;
end
% SPAWN FOOD ON THE 4TH EAT
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 GAME
PAUSE=~PAUSE;
if PAUSE && SNAKE_SPEED
set(DRAW_WINDOW,'CData',getPause)
end
case 'Stop' % END GAME
SNAKE_SPEED = false;
set(DRAW_WINDOW,'CData',getTitle)
case 'Food' % FOOD GENERATE ON BOARD
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' % BOUNUS GENERATE ON BOARD
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' % GENERATE GAME BOARD
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)
end % SNAKE_BODY function title = getTitle() title = zeros(30,40); title([ 3 32 33 61 62 63 64 65 66 67 92 93 123 ]) = 3;
%Gameover function gameover = getGameOver() gameover = zeros(30,40); gameover([95 96 97 98 99 100 101 102 103 104 109 110 111 112 113 114 ... 125 126 127 128 129 130 131 132 133 134 139 140 141 142 143 144 155 ... 156 163 164 167 168 175 176 185 186 193 194 197 198 205 206 215 216 ... 219 220 221 222 223 224 229 230 231 232 233 234 245 246 249 250 251 ... 252 253 254 259 260 261 262 263 264 335 336 337 338 339 340 341 342 ... 343 344 347 348 349 350 351 352 353 354 365 366 367 368 369 370 371 ... 372 373 374 377 378 379 380 381 382 383 384 395 396 399 400 415 416 ... 425 426 429 430 445 446 455 456 457 458 459 460 461 462 463 464 467 ... 468 469 470 471 472 473 474 485 486 487 488 489 490 491 492 493 494 ... 497 498 499 500 501 502 503 504 575 576 577 578 579 580 581 582 583 ... 584 587 588 589 590 591 592 593 594 595 596 605 606 607 608 609 610 ... 611 612 613 614 617 618 619 620 621 622 623 624 625 626 635 636 647 ... 648 651 652 655 656 665 666 677 678 681 682 685 686 695 696 697 698 ... 699 700 701 702 703 704 707 708 711 712 715 716 725 726 727 728 729 ... 730 731 732 733 734 737 738 741 742 745 746 755 756 785 786 817 818 ... 819 820 821 822 823 824 827 828 829 830 831 832 833 834 835 836 847 ... 848 849 850 851 852 853 854 857 858 859 860 861 862 863 864 865 866 ... 887 888 891 892 917 918 921 922 935 936 937 938 939 940 941 942 943 ... 944 949 950 953 954 955 956 965 966 967 968 969 970 971 972 973 974 ... 979 980 983 984 985 986 995 996 999 1000 1003 1004 1025 1026 1029 ... 1030 1033 1034 1055 1056 1059 1060 1063 1064 1067 1068 1069 1070 ... 1071 1072 1075 1076 1085 1086 1093 1094 1097 1098 1099 1100 1101 ... 1102 1105 1106]) = 1;
%Pause function pause = getPause() pause = zeros(30,40); pause([41 42 43 44 45 46 47 48 49 50 71 72 73 74 75 76 77 78 79 80 101 ... 102 103 104 105 106 107 108 109 110 131 132 136 137 161 162 166 167 ... 193 194 195 223 224 225 283 284 285 313 314 315 341 342 346 347 371 ... 372 376 377 401 402 406 407 431 432 433 434 435 436 437 461 462 463 ... 464 465 466 467 491 492 493 494 495 496 497 551 552 553 554 555 556 ... 581 582 583 584 585 586 616 617 646 647 671 672 673 674 675 676 677 ... 701 702 703 704 705 706 707 731 732 733 734 735 736 737 791 792 796 ... 797 821 822 826 827 853 854 855 856 857 883 884 885 886 887 913 914 ... 915 916 917 971 972 973 974 975 976 977 1001 1002 1003 1004 1005 ... 1006 1007 1031 1032 1036 1037 1061 1062 1066 1067 1091 1092 1096 ... 1097 1121 1122 1123 1124 1125 1151 1152 1153 1154 1155]) = 1;

回答(1 个)

Matthew Eicholtz
Matthew Eicholtz 2016-3-12
编辑:Matthew Eicholtz 2016-3-12
Try editing your question for readability. It is difficult to parse your sample code.
That being said, in general, you can create a separate file for helper functions that are called by your main function. For example, you could create a function called "changemaze.m" containing:
function changemaze()
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
end
and then call "changemaze" in the main file somewhere...
case 'Mazes' % CHANGE MAZE
changemaze();
feval(mfilename,'Stop')
case 'ShowScore' % SCORE CALCULATOR
...
When you create these separate files, make sure to specify any inputs or outputs that may be required by the main function. From what I can tell, you shouldn't need any inputs/outputs for the example I described because you declare a bunch of global variables (something I generally advise against unless absolutely necessary).

类别

Help CenterFile Exchange 中查找有关 Labyrinth problems 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by