您好,登錄后才能下訂單哦!
這篇文章主要講解了“基于Matlab如何實現抖音小游戲蘋果蛇”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“基于Matlab如何實現抖音小游戲蘋果蛇”吧!
還是提一句這個小游戲開始玩的心路歷程:
第1-5關:?這不就是有重力的貪吃蛇,有任何難度嘛??
第6關:嗯。。。這個小技巧還是有點意思的
第30關:???這繞來繞去的是人出的題目??
第90關:… …瘋了瘋了已經瘋了
廢話不多說看一下MATLAB版的完整代碼:
為了方便修改,關卡地圖就擺在m文件里,總共寫了八關,但是只有代碼沒有素材包還是運行不了,因此請從文末獲取完整壓縮包.
function appleSnake % @author : slandarer % 公眾號 : slandarer隨筆 % 知乎 : hikari % 加載圖像數據 MT=load('material.mat'); MT=MT.MT; SIZE.BASE=size(MT.base.CData,1); SIZE.EXIT=size(MT.exit.CData,1); SIZE.STONE=size(MT.stone.CData,1); SIZE.APPLE=size(MT.apple.CData,1); SIZE.SNAKE=size(MT.snake1.CData,1); SIZE.RESTART=size(MT.restart.CData,1); % 初始化地圖及關卡 [fig,ax]=init(); LEVEL=1; MAP=getMap(LEVEL); loadMap(); refreshSnake(); text(10,1000,'當前關卡:','FontSize',18,'FontWeight','bold') LEVEL_HDL=text(270,1000,num2str(LEVEL),'FontSize',18,'FontWeight','bold'); % ========================================================================= set(fig,'KeyPressFcn',@key); function key(~,event) dirvec=[0,0]; switch event.Key case 'uparrow' dirvec=[-1,0]; [~,colSet]=find(MAP<0); if all(colSet==colSet(1)) dirvec=[0,0]; end case 'downarrow',dirvec=[1,0]; case 'rightarrow',dirvec=[0,1]; case 'leftarrow',dirvec=[0,-1]; end if sum(dirvec)~=0 [hi,hj]=find(MAP==-1); switch MAP(hi+dirvec(1),hj+dirvec(2)) case 0 MAP(MAP<0)=MAP(MAP<0)-1; MAP(MAP==min(MAP,[],[1,2]))=0; MAP(hi+dirvec(1),hj+dirvec(2))=-1; case 1 case 2 MAP(MAP<0)=MAP(MAP<0)-1; MAP(MAP==min(MAP,[],[1,2]))=0; MAP(hi+dirvec(1),hj+dirvec(2))=-1; refreshSnake() win(); return; case 3 MAP(MAP<0)=MAP(MAP<0)-1; APPLE_HDL=findobj('Tag','APPLE','UserData',[hi+dirvec(1),hj+dirvec(2)]); MAP(hi+dirvec(1),hj+dirvec(2))=-1;delete(APPLE_HDL); case 4 if MAP(hi+2*dirvec(1),hj+2*dirvec(2))==0 MAP(MAP<0)=MAP(MAP<0)-1; MAP(MAP==min(MAP,[],[1,2]))=0; MAP(hi+dirvec(1),hj+dirvec(2))=-1; MAP(hi+2*dirvec(1),hj+2*dirvec(2))=4; STONE_HDL=findobj('Tag','STONE','UserData',[hi+dirvec(1),hj+dirvec(2)]); STONE_HDL.XData=STONE_HDL.XData+70*dirvec(2); STONE_HDL.YData=STONE_HDL.YData-70*dirvec(1); STONE_HDL.UserData=[hi+2*dirvec(1),hj+2*dirvec(2)]; tCol=MAP(:,hj+2*dirvec(2));tCol(60)=1; nRow=find(tCol~=0&((1:60)'>hi+2*dirvec(1)),1,'first')-1; STONE_HDL.YData=STONE_HDL.YData-70*(nRow-hi+dirvec(1)); STONE_HDL.UserData=[nRow,hj+2*dirvec(2)]; MAP(hi+2*dirvec(1),hj+2*dirvec(2))=0; MAP(nRow,hj+2*dirvec(2))=4; end end refreshSnake();pause(.15) freeFall(); refreshSnake() end end function freeFall() [rowSet,colSet]=find(MAP<0); diffmin=inf; for t=1:length(rowSet) tCol=MAP(:,colSet(t)); tCol(60)=1; tRow=find(tCol>0&((1:60)'>rowSet(t)),1,'first'); diffmin=min(diffmin,tRow-rowSet(t)); end diffmin=diffmin-1; if diffmin>15 loss(diffmin) elseif diffmin>0 tMAP=MAP; for t=1:length(rowSet) tMAP(rowSet(t),colSet(t))=0; end for t=1:length(rowSet) tMAP(rowSet(t)+diffmin,colSet(t))=MAP(rowSet(t),colSet(t)); end MAP=tMAP; end end function restart(~,~) MAP=getMap(LEVEL); if ~isempty(MAP) loadMap(); refreshSnake(); LEVEL_HDL.String=num2str(LEVEL); end end function win(~,~) LEVEL=LEVEL+1; MAP=getMap(LEVEL); if ~isempty(MAP) loadMap(); refreshSnake(); LEVEL_HDL.String=num2str(LEVEL); else msgbox('暫無更多關卡') end end function loss(D) if D>15 for d=1:20 [rowSet,colSet]=find(MAP<0); tMAP=MAP; for t=1:length(rowSet) tMAP(rowSet(t),colSet(t))=0; end for t=1:length(rowSet) tMAP(rowSet(t)+1,colSet(t))=MAP(rowSet(t),colSet(t)); end MAP=tMAP; refreshSnake(); pause(.1); end end end % ========================================================================= function [fig,ax]=init(~,~) % figure窗口創建及屬性設置 fig=figure(); fig.NumberTitle='off'; fig.Position=[250,120,500,500]; fig.MenuBar='none'; fig.Name='apple snake by slandarer'; % axes坐標區域創建及屬性設置 ax=gca;hold on ax.Position=[0 0 1 1]; ax.XTick=[]; ax.YTick=[]; ax.XColor='none'; ax.YColor='none'; ax.XLim=[0,1050]; ax.YLim=[0,1050]; % 繪制背景 image(ax,ax.XLim,ax.YLim,flipud(MT.background)) image(ax,[-SIZE.RESTART/2,SIZE.RESTART/2]+60,... [-SIZE.RESTART/2,SIZE.RESTART/2]+900,... flipud(MT.restart.CData),... 'AlphaData',flipud(MT.restart.AlpData),... 'ButtonDownFcn',@restart) end function map=getMap(level) % 地圖大小15x15 % 空氣 : 0 % 土塊 : 1 % 蛇頭 : -1,蛇身數值依次遞減 % 終點 : 2 % 蘋果 : 3 % 石塊 : 4 Map{1}=[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 -2 -1 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 1 0 0 0 0 0 0 0 0 0 0 0 -2 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -3 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; Map{9}=[]; map=Map{level}; end function loadMap(~,~) delete(findobj('Tag','BASE')) delete(findobj('Tag','EXIT')) delete(findobj('Tag','APPLE')) delete(findobj('Tag','STONE')) % 繪制土塊 for i=15:-1:1 for j=15:-1:1 if MAP(i,j)==1 image(ax,70*(j-1)+35+[-SIZE.BASE/2,SIZE.BASE/2],... 70*(16-i)+35+[-SIZE.BASE/2,SIZE.BASE/2],... flipud(MT.base.CData),... 'alphaData',flipud(MT.base.AlpData),... 'tag','BASE'); end end end % 繪制出口 [ti,tj]=find(MAP==2); image(ax,70*(tj-1)+35+[-SIZE.EXIT/2,SIZE.EXIT/2],... 70*(16-ti)+35+[-SIZE.EXIT/2,SIZE.EXIT/2],... flipud(MT.exit.CData),... 'alphaData',flipud(MT.exit.AlpData),... 'tag','EXIT'); % 繪制蘋果 [ti,tj]=find(MAP==3); if ~isempty(ti) image(ax,70*(tj-1)+35+[-SIZE.APPLE/2,SIZE.APPLE/2],... 70*(16-ti)+35+[-SIZE.APPLE/2,SIZE.APPLE/2],... flipud(MT.apple.CData),... 'alphaData',flipud(MT.apple.AlpData),... 'tag','APPLE','UserData',[ti,tj]); end % 繪制石塊 [ti,tj]=find(MAP==4); if ~isempty(ti) image(ax,70*(tj-1)+35+[-SIZE.STONE/2,SIZE.STONE/2],... 70*(16-ti)+35+[-SIZE.STONE/2,SIZE.STONE/2],... flipud(MT.stone.CData),... 'alphaData',flipud(MT.stone.AlpData),... 'tag','STONE','UserData',[ti,tj]); end end function refreshSnake(~,~) delete(findobj('Tag','SNAKE')) % 畫蛇頭 [ti,tj]=find(MAP==-1); [ti_n,tj_n]=find(MAP==-2); tSnakeC=MT.snake1.CData; tSnakeAlp=MT.snake1.AlpData; if tj_n>tj tSnakeC=fliplr(tSnakeC); tSnakeAlp=fliplr(tSnakeAlp); end if ti_n>ti if MAP(ti,tj-1)==1 tSnakeC(:,:,1)=flipud(tSnakeC(end:-1:1,:,1)'); tSnakeC(:,:,2)=flipud(tSnakeC(end:-1:1,:,2)'); tSnakeC(:,:,3)=flipud(tSnakeC(end:-1:1,:,3)'); tSnakeAlp=flipud(tSnakeAlp'); else tSnakeC(:,:,1)=flipud(tSnakeC(:,:,1)'); tSnakeC(:,:,2)=flipud(tSnakeC(:,:,2)'); tSnakeC(:,:,3)=flipud(tSnakeC(:,:,3)'); tSnakeAlp=flipud(tSnakeAlp'); end end if ti_n<ti if MAP(ti,tj-1)==1 tSnakeC(:,:,1)=tSnakeC(end:-1:1,:,1)'; tSnakeC(:,:,2)=tSnakeC(end:-1:1,:,2)'; tSnakeC(:,:,3)=tSnakeC(end:-1:1,:,3)'; tSnakeAlp=tSnakeAlp'; else tSnakeC(:,:,1)=tSnakeC(:,:,1)'; tSnakeC(:,:,2)=tSnakeC(:,:,2)'; tSnakeC(:,:,3)=tSnakeC(:,:,3)'; tSnakeAlp=tSnakeAlp'; end end image(ax,70*(tj-1)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... 70*(16-ti)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... flipud(tSnakeC),... 'alphaData',flipud(tSnakeAlp),... 'tag','SNAKE'); [ti,tj]=find(MAP==min(MAP,[],[1,2])); [ti_l,tj_l]=find(MAP==min(MAP,[],[1,2])+1); tSnakeC=MT.snake4.CData; tSnakeAlp=MT.snake4.AlpData; switch true case tj_l>tj case tj_l<tj tSnakeC=fliplr(tSnakeC); tSnakeAlp=fliplr(tSnakeAlp); case ti_l<ti tSnakeC(:,:,1)=flipud(tSnakeC(:,:,1)'); tSnakeC(:,:,2)=flipud(tSnakeC(:,:,2)'); tSnakeC(:,:,3)=flipud(tSnakeC(:,:,3)'); tSnakeAlp=flipud(tSnakeAlp'); case ti_l>ti tSnakeC(:,:,1)=tSnakeC(:,:,1)'; tSnakeC(:,:,2)=tSnakeC(:,:,2)'; tSnakeC(:,:,3)=tSnakeC(:,:,3)'; tSnakeAlp=tSnakeAlp'; end % 畫蛇尾 image(ax,70*(tj-1)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... 70*(16-ti)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... flipud(tSnakeC),... 'alphaData',flipud(tSnakeAlp),... 'tag','SNAKE'); % 畫蛇身體 for i=-2:-1:(min(MAP,[],[1,2])+1) [ti,tj]=find(MAP==i); [ti_l,tj_l]=find(MAP==i+1); [ti_n,tj_n]=find(MAP==i-1); switch true case ti_l==ti_n tSnakeC=MT.snake2.CData; tSnakeAlp=MT.snake2.AlpData; case tj_l==tj_n tSnakeC=MT.snake2.CData; tSnakeAlp=MT.snake2.AlpData; tSnakeC(:,:,1)=tSnakeC(:,:,1)'; tSnakeC(:,:,2)=tSnakeC(:,:,2)'; tSnakeC(:,:,3)=tSnakeC(:,:,3)'; tSnakeAlp=tSnakeAlp'; case ti_l<ti_n&&tj_l>tj_n&&ti>ti_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=rot90(tSnakeC,2); tSnakeAlp=rot90(tSnakeAlp,2); case ti_l<ti_n&&tj_l>tj_n&&tj<tj_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; case ti_l<ti_n&&tj_l<tj_n&&ti>ti_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=flipud(tSnakeC); tSnakeAlp=flipud(tSnakeAlp); case ti_l<ti_n&&tj_l<tj_n&&tj>tj_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=fliplr(tSnakeC); tSnakeAlp=fliplr(tSnakeAlp); case ti_l>ti_n&&tj_l>tj_n&&ti<ti_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=fliplr(tSnakeC); tSnakeAlp=fliplr(tSnakeAlp); case ti_l>ti_n&&tj_l>tj_n&&tj<tj_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=flipud(tSnakeC); tSnakeAlp=flipud(tSnakeAlp); case ti_l>ti_n&&tj_l<tj_n&&ti<ti_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; case ti_l>ti_n&&tj_l<tj_n&&tj>tj_l tSnakeC=MT.snake3.CData; tSnakeAlp=MT.snake3.AlpData; tSnakeC=rot90(tSnakeC,2); tSnakeAlp=rot90(tSnakeAlp,2); end image(ax,70*(tj-1)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... 70*(16-ti)+35+[-SIZE.SNAKE/2,SIZE.SNAKE/2],... flipud(tSnakeC),... 'alphaData',flipud(tSnakeAlp),... 'tag','SNAKE'); end end end
感謝各位的閱讀,以上就是“基于Matlab如何實現抖音小游戲蘋果蛇”的內容了,經過本文的學習后,相信大家對基于Matlab如何實現抖音小游戲蘋果蛇這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。