贪吃蛇游戏说明一、游戏简介主要功能:(1)通过游戏设置的等级设置进入游戏,在游戏过程中可以选择速度快慢程度来选择游戏的难易度。
(2)可以选择有边界阻碍和无边界阻碍的。
界面功能:(3)游戏的的窗口菜单是自定义的,简单设计了几个菜单项,方便选择开始游戏,离开游戏,速度快慢,有无边界。
(4)在游戏界面的右下方有一个显示当前的得分情况,方便便用户来了解自己的战绩。
操作功能:其中游戏的主操作键是↓↑→←。
不仅可以通过菜单来进行操作,还可以通过快捷键开操作(crtrl+N --- New Game,ctrl+Q--- Exit)二、实现步骤1、设置30×40的界面和颜色,设置控制台窗口标题,然后自定义菜单2、设置一些主要的功能,游戏的开始,离开,难易度和有无墙壁(代码参考)3、设置蛇和豆子的位置大小和颜色4、在设置时考虑蛇移动的方向,通过在蛇头没有撞到自身和边界的前提下,判断蛇是否吃到食物,若吃到了食物,则需要将吃到的食物作为蛇头,否则需要在原来蛇头的下一个坐标作为蛇头,而蛇尾需要被去除,同时加分。
三、游戏截图(无墙壁)(有墙壁)四、代码(其中标记的地方是参考的)function snake(cmd)global SNAKE WALL LEVEL BOARD DIRECTION RUNNING FOOD BONUS PAUSEif ~nargincmd = 'init';endif ~(ischar(cmd)||isscalar(cmd))return;endswitch cmdcase'init'scrsz = get(0,'ScreenSize');f = figure('Name','Snake',...'Numbertitle','off',...'Menubar','none',...'Color',[.95 .95 .95],...'DoubleBuffer','on',...'Position',[(scrsz(3)-400)/2 (scrsz(4)-300)/2 400 300],...'Colormap',[.1 .71 0;.3 .4 .4;0 0 0;1 1 0],...'CloseRequestFcn',sprintf('%s(''Stop'');closereq;',mfilename),...'KeyPressFcn',sprintf('%s(double(get(gcbf,''Currentcharacter'')))',mf ilename));FileMenu = uimenu(f,'Label','&File');uimenu(FileMenu,'Label','NewGame','Accelerator','N','Callback',sprintf('%s(''NewGame'')',mfilenam e));uimenu(FileMenu,'Label','Exit','Accelerator','Q','Separator','on','Ca llback',sprintf('%s(''Stop'');closereq',mfilename));LevelMenu = uimenu(f,'Label','&Level');uimenu(LevelMenu,'Label','1','Callback',sprintf('%s(''Level'')',mfile name))uimenu(LevelMenu,'Label','2','Callback',sprintf('%s(''Level'')',mfile name))uimenu(LevelMenu,'Label','3','Callback',sprintf('%s(''Level'')',mfile name))uimenu(LevelMenu,'Label','4','Callback',sprintf('%s(''Level'')',mfile name),'checked','on')uimenu(LevelMenu,'Label','5','Callback',sprintf('%s(''Level'')',mfile name))WallMenu = uimenu(f,'Label','&Wall');uimenu(WallMenu,'Label','Nowall','Callback',sprintf('%s(''Wall'')',mfilename),'checked','on')uimenu(WallMenu,'Label','Wall','Callback',sprintf('%s(''Wall'')',mfil ename))% Create The axesaxes('Units','normalized',...'Position', [0 0 1 1],...'Visible','off',...'DrawMode','fast',...'NextPlot','replace');% Add the boardBOARD = image(getTitle,'CDataMapping','scaled');axis imageset(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','Score');SNAKE = [14,20;14,19;14,18;14,17;14,16];WALL = zeros(30,40);LEVEL = 4;case 28 % leftif SNAKE(2,2)~=mod(SNAKE(1,2)-2,40)+1DIRECTION = cmd;endcase 29 % rightif SNAKE(2,2)~=mod(SNAKE(1,2),40)+1DIRECTION = cmd;endcase 30 % upif SNAKE(2,1)~=mod(SNAKE(1,1)-2,30)+1DIRECTION = cmd;endcase 31 % downif SNAKE(2,1)~=mod(SNAKE(1,1),30)+1DIRECTION = cmd;endcase'Level'% Change of Levelset(get(get(gcbo,'Parent'),'Children'),'checked','off') set(gcbo,'checked','on')LEVEL = length(get(gcbo,'Label'));case'Wall'% Change of Wallset(get(get(gcbo,'Parent'),'Children'),'checked','off') set(gcbo,'checked','on')WALL = zeros(30,40);switch get(gcbo,'Label')case'No wall'case'Wall'WALL([1 30],:) = 1;WALL(:,[1 40]) = 1;endfeval(mfilename,'Stop')case'ShowScore'% Change of Show Scoreswitch get(gcbo,'checked')case'on'set(gcbo,'checked','off')set(findobj(gcbf,'Tag','Score'),'Visible','off') case'off'set(gcbo,'checked','on')set(findobj(gcbf,'Tag','Score'),'Visible','on') endcase'NewGame'set(findobj(gcbf,'Tag','Score'),'String','0')SNAKE = [14,20;14,19;14,18;14,17;14,16];DIRECTION = 29; % rightBONUS = 0;PAUSE = false;feval(mfilename,'Food')feval(mfilename,'Start')case'Start'% Start GameRUNNING = true;bonusCounter = 0;foodCounter = 0;while(RUNNING)if ~PAUSESNAKE = circshift(SNAKE,1);SNAKE(1,:) = SNAKE(2,:);switch DIRECTIONcase 28 % leftSNAKE(1,2) = mod(SNAKE(1,2)-2,40)+1;case 29 % rightSNAKE(1,2) = mod(SNAKE(1,2),40)+1;case 30 % upSNAKE(1,1) = mod(SNAKE(1,1)-2,30)+1;case 31 % downSNAKE(1,1) = mod(SNAKE(1,1),30)+1;end% Check if snake hits any barrierif WALL(SNAKE(1,1),SNAKE(1,2)) || ...sum(ismember(SNAKE(2:end,1),SNAKE(1,1))+... ismember(SNAKE(2:end,2),SNAKE(1,2))==2)pause(.3)delete(findobj(gcbf,'Tag','Bonus'))feval(mfilename,'Stop')set(BOARD,'CData',getGameOver)else% Check if snake eats bonusif isequal(SNAKE(1,:),BONUS)% Update scorescorehandle = findobj(gcbf,'Tag','Score'); set(scorehandle,'String',...num2str(LEVEL*ceil(bonusCounter/3)+... str2double(get(scorehandle,'String')))) bonusCounter = 1;endif BONUSbonusCounter = bonusCounter-1;if bonusCounter<=0delete(findobj(gcbf,'Tag','Bonus'))BONUS = 0;endend% Check if snake eats foodif isequal(SNAKE(1,:),FOOD)% Snake Grows!SNAKE(end+1,:) = SNAKE(end,:);% Update scorescorehandle = findobj(gcbf,'Tag','Score'); set(scorehandle,'String',...num2str(LEVEL+str2double(get(scorehandle,'String'))))% Spawn new foodfeval(mfilename,'Food')if ~BONUS % only update if no bonus existbonusCounter = bonusCounter+15;foodCounter = foodCounter+1;endif foodCounter==4 % Spawn new bonus every fourth Food feval(mfilename,'Bonus')foodCounter = 0;endendfeval(mfilename,'DrawBoard')endendpause(.3/LEVEL)endcase {112 32} % Pause GamePAUSE=~PAUSE;if PAUSE && RUNNINGset(BOARD,'CData',getPause)endcase'Stop'% Stop GameRUNNING = false;set(BOARD,'CData',getTitle)case'Food'% Put food onto game boardCData = WALL;for i=1:size(SNAKE,1)CData(SNAKE(i,1),SNAKE(i,2)) = 1;endind = find(CData'==0);ind = ind(ceil(rand*length(ind)));FOOD = [ceil(ind/40) mod(ind-1,40)+1];case'Bonus'% Put bonus onto game boarddelete(findobj(gcbf,'Tag','Bonus'))CData = WALL;for i=1:size(SNAKE,1)CData(SNAKE(i,1),SNAKE(i,2)) = 1;endCData(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'% Draw the Game BoardCData = WALL;for i=1:size(SNAKE,1)CData(SNAKE(i,1),SNAKE(i,2)) = 2;endCData(FOOD(1),FOOD(2)) = 4;set(BOARD,'CData',CData)endfunction [ico,map]=getIcon()% create simple icon matrixico = ones(13)*3;ico(:,1:4:13) = 1;ico(1:4:13,:) = 1;ico(6:8,6:8) = 2;ico(6:8,10:12) = 2;ico(10:12,10:12) = 2;map = [0 0 0;.5 .5 .6;[148 182 166]/255;];function title = getTitle()title = zeros(30,40);title([42 43 47 48 72 73 77 78 104 105 106 107 108 134 135 136 137 138 ... 164 165 166 167 168 222 223 224 225 226 227 228 252 253 254 255 256 ... 257 258 282 283 312 313 344 345 346 347 348 374 375 376 377 378 404 ... 405 406 407 408 464 465 466 494 495 496 522 523 527 528 552 553 557 ... 558 582 583 587 588 612 613 614 615 616 617 618 642 643 644 645 646 ... 647 648 672 673 674 675 676 677 678 727 728 729 730 731 732 733 734 ... 735 736 737 738 757 758 759 760 761 762 763 764 765 766 767 768 787 ... 788 789 790 791 792 793 794 795 796 797 798 824 825 826 854 855 856 ... 882 883 887 888 912 913 917 918 972 973 974 975 976 977 978 1002 ... 1003 1004 1005 1006 1007 1008 1032 1033 1037 1038 1062 1063 1067 ... 1068 1092 1093 1097 1098 1122 1123 1124 1125 1126 1152 1153 1154 ... 1155 1156]) = 3;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;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;。