Matlab实现别踩白块小游戏的示例代码
目录
- 游戏效果
- 游戏说明
- 完整代码
- pianoKeys.m(主函数)
- getMusic.m(用于获取音乐数据)
游戏效果
游戏说明
‘A’,‘S’,‘D’,F’按键代表四条通路(点击S开始),按错按钮或黑块接触底限均为失败。
完整代码
分两个m文件,应放在同一文件夹
pianoKeys.m(主函数)
function pianoKeys %======================%======== [v1,notes,fs]=getMusic;%读取音乐 %======================%======== fig=uifigure; fig.Position=[10 50 4*90 4*150]; fig.NumberTitle='off'; fig.MenuBar='none'; fig.Resize='off'; fig.Name='pianoKeys'; ax=uiaxes(fig); ax.Position=[-22 -15 4*90+36 4*150+40]; ax.XLim=[0 4*90]; ax.YLim=[0 4*150]; ax.XColor=[0 0 0]; ax.YColor=[0 0 0]; ax.Box='on'; ax.XTick=0:90:360; ax.YTick=[0 600]; ax.XGrid='on'; ax.GridColor=[0 0 0]; ax.GridAlpha=1; ax.Toolbar.Visible='off'; %========================================================================== blockList(1)=drawBlock(changeData(1),0,{'开始';'游戏'},v1{1}); noDeleteList=1:4; newBlockY=600; newBlockNum=5; startFlag=0; gameOver=0; for i=2:4 x=changeData(i); blockList(i)=drawBlock(x,(i-1)*150,'',v1{i}); end %========================================================================== set(fig,'KeyPressFcn',@keyPressFcn) %========================================================================== fps=10; PKtimer=timer('ExecutionMode', 'fixedRate', 'Period',1/fps, 'TimerFcn', @pianoGame); start(PKtimer) %========================================================================== function pianoGame(~,~) if startFlag if newBlockY<=600&&newBlockNum<=length(v1) tempX=changeData(newBlockNum); blockList(newBlockNum)=drawBlock(tempX,newBlockY,'',v1{newBlockNum}); noDeleteList=[noDeleteList,newBlockNum]; newBlockNum=newBlockNum+1; newBlockY=newBlockY+150; end for ii=noDeleteList blockList(ii).Position(2)=blockList(ii).Position(2)-150/8; end newBlockY=newBlockY-150/8; if (~isempty(noDeleteList))&&blockList(noDeleteList(1)).Position(2)<-10 gameOverFcn(1); gameOver=1; %tempStr=blockList(noDeleteList(1)).UserData; %sound(notes.(tempStr),fs) %delete(blockList(noDeleteList(1))) %noDeleteList(1)=[]; end end end function gameOverFcn(coe) sound([notes.do0f,notes.do1f,notes.do2f,notes.do2f],fs) switch coe case 1 blockList(noDeleteList(1)).BackgroundColor=[0.6 0 0]; blockList(noDeleteList(1)).Text={'游戏';'失败'}; startFlag=0; otherwise object=uilabel(fig); object.Text={'游戏';'失败'}; object.Position=[coe,0+3-18.75,90,150]; object.FontSize=26; object.FontWeight='bold'; object.FontColor=[1 1 1]; object.BackgroundColor=[0.6 0 0]; object.HorizontalAlignment='center'; end end function keyPressFcn(~,event) switch event.Key case 'a',xPos=0+3; case 's',xPos=90+3; case 'd',xPos=180+3; case 'f',xPos=270+3; otherwise,xPos=-1; end if (~isempty(noDeleteList))&&blockList(noDeleteList(1)).Position(1)==xPos&&gameOver==0 tempStr=blockList(noDeleteList(1)).UserData; sound(notes.(tempStr),fs) delete(blockList(noDeleteList(1))) if noDeleteList(1)==1 startFlag=1; end noDeleteList(1)=[]; elseif blockList(noDeleteList(1)).Position(1)~=xPos&&xPos~=-1&&gameOver==0&&startFlag==1 startFlag=0; gameOverFcn(xPos); gameOver=1; end end %========================================================================== function x=changeData(sort) note=v1{sort}; switch note(1) case 'd',x=0; case 'r',x=0; case 'm',x=90; case 'f',x=90; case 's',x=180; case 'l',x=180; case 't',x=270; case 'b',x=270; end end function object=drawBlock(x,y,string,note) object=uilabel(fig); object.Text=string; object.Position=[x+3,y+3,90,150]; object.FontSize=26; object.FontWeight='bold'; object.FontColor=[1 1 1]; object.BackgroundColor=[0 0 0]; object.HorizontalAlignment='center'; object.UserData=note; end end
getMusic.m(用于获取音乐数据)
function [v1,notes,fs]=getMusic % Most shining national wind//最炫民族风 on Matlab % The Modification is from "canon", not by me fs = 44100; % sample rate dt = 1/fs; T16 = 0.125; t16 = 0:dt:T16; [~,k] = size(t16); t4 = linspace(0,4*T16,4*k); t8 = linspace(0,2*T16,2*k); [~,i] = size(t4); [~,j] = size(t8); % Modification functions mod4=(t4.^4).*exp(-30*(t4.^0.5)); mod4=mod4*(1/max(mod4)); mod8=(t8.^4).*exp(-50*(t8.^0.5)); mod8=mod8*(1/max(mod8)); mod16=(t16.^4).*exp(-90*(t16.^0.5)); mod16=mod16*(1/max(mod16)); f0 = 2*146.8; % reference frequency ScaleTable = [2/3 3/4 5/6 15/16 ... 1 9/8 5/4 4/3 3/2 5/3 9/5 15/8 ... 2 9/4 5/2 8/3 3 10/3 15/4 4 ... 1/2 9/16 5/8]; % 1/4 notes notes.do0f = mod4.*cos(2*pi*ScaleTable(21)*f0*t4); notes.re0f = mod4.*cos(2*pi*ScaleTable(22)*f0*t4); notes.mi0f = mod4.*cos(2*pi*ScaleTable(23)*f0*t4); notes.fa0f = mod4.*cos(2*pi*ScaleTable(1)*f0*t4); notes.so0f = mod4.*cos(2*pi*ScaleTable(2)*f0*t4); notes.la0f = mod4.*cos(2*pi*ScaleTable(3)*f0*t4); notes.ti0f = mod4.*cos(2*pi*ScaleTable(4)*f0*t4); notes.do1f = mod4.*cos(2*pi*ScaleTable(5)*f0*t4); notes.re1f = mod4.*cos(2*pi*ScaleTable(6)*f0*t4); notes.mi1f = mod4.*cos(2*pi*ScaleTable(7)*f0*t4); notes.fa1f = mod4.*cos(2*pi*ScaleTable(8)*f0*t4); notes.so1f = mod4.*cos(2*pi*ScaleTable(9)*f0*t4); notes.la1f = mod4.*cos(2*pi*ScaleTable(10)*f0*t4); notes.tb1f = mod4.*cos(2*pi*ScaleTable(11)*f0*t4); notes.ti1f = mod4.*cos(2*pi*ScaleTable(12)*f0*t4); notes.do2f = mod4.*cos(2*pi*ScaleTable(13)*f0*t4); notes.re2f = mod4.*cos(2*pi*ScaleTable(14)*f0*t4); notes.mi2f = mod4.*cos(2*pi*ScaleTable(15)*f0*t4); notes.fa2f = mod4.*cos(2*pi*ScaleTable(16)*f0*t4); notes.so2f = mod4.*cos(2*pi*ScaleTable(17)*f0*t4); notes.la2f = mod4.*cos(2*pi*ScaleTable(18)*f0*t4); notes.ti2f = mod4.*cos(2*pi*ScaleTable(19)*f0*t4); notes.do3f = mod4.*cos(2*pi*ScaleTable(20)*f0*t4); notes.blkf = zeros(1,i); % 1/8 notes notes.do0e = mod8.*cos(2*pi*ScaleTable(21)*f0*t8); notes.re0e = mod8.*cos(2*pi*ScaleTable(22)*f0*t8); notes.mi0e = mod8.*cos(2*pi*ScaleTable(23)*f0*t8); notes.fa0e = mod8.*cos(2*pi*ScaleTable(1)*f0*t8); notes.so0e = mod8.*cos(2*pi*ScaleTable(2)*f0*t8); notes.la0e = mod8.*cos(2*pi*ScaleTable(3)*f0*t8); notes.ti0e = mod8.*cos(2*pi*ScaleTable(4)*f0*t8); notes.do1e = mod8.*cos(2*pi*ScaleTable(5)*f0*t8); notes.re1e = mod8.*cos(2*pi*ScaleTable(6)*f0*t8); notes.mi1e = mod8.*cos(2*pi*ScaleTable(7)*f0*t8); notes.fa1e = mod8.*cos(2*pi*ScaleTable(8)*f0*t8); notes.so1e = mod8.*cos(2*pi*ScaleTable(9)*f0*t8); notes.la1e = mod8.*cos(2*pi*ScaleTable(10)*f0*t8); notes.tb1e = mod8.*cos(2*pi*ScaleTable(11)*f0*t8); notes.ti1e = mod8.*cos(2*pi*ScaleTable(12)*f0*t8); notes.do2e = mod8.*cos(2*pi*ScaleTable(13)*f0*t8); notes.re2e = mod8.*cos(2*pi*ScaleTable(14)*f0*t8); notes.mi2e = mod8.*cos(2*pi*ScaleTable(15)*f0*t8); notes.fa2e = mod8.*cos(2*pi*ScaleTable(16)*f0*t8); notes.so2e = mod8.*cos(2*pi*ScaleTable(17)*f0*t8); notes.la2e = mod8.*cos(2*pi*ScaleTable(18)*f0*t8); notes.ti2e = mod8.*cos(2*pi*ScaleTable(19)*f0*t8); notes.do3e = mod8.*cos(2*pi*ScaleTable(20)*f0*t8); notes.blke = zeros(1,j); % 1/16 notes notes.do0s = mod16.*cos(2*pi*ScaleTable(21)*f0*t16); notes.re0s = mod16.*cos(2*pi*ScaleTable(22)*f0*t16); notes.mi0s = mod16.*cos(2*pi*ScaleTable(23)*f0*t16); notes.fa0s = mod16.*cos(2*pi*ScaleTable(1)*f0*t16); notes.so0s = mod16.*cos(2*pi*ScaleTable(2)*f0*t16); notes.la0s = mod16.*cos(2*pi*ScaleTable(3)*f0*t16); notes.ti0s = mod16.*cos(2*pi*ScaleTable(4)*f0*t16); notes.do1s = mod16.*cos(2*pi*ScaleTable(5)*f0*t16); notes.re1s = mod16.*cos(2*pi*ScaleTable(6)*f0*t16); notes.mi1s = mod16.*cos(2*pi*ScaleTable(7)*f0*t16); notes.fa1s = mod16.*cos(2*pi*ScaleTable(8)*f0*t16); notes.so1s = mod16.*cos(2*pi*ScaleTable(9)*f0*t16); notes.la1s = mod16.*cos(2*pi*ScaleTable(10)*f0*t16); notes.tb1s = mod16.*cos(2*pi*ScaleTable(11)*f0*t16); notes.ti1s = mod16.*cos(2*pi*ScaleTable(12)*f0*t16); notes.do2s = mod16.*cos(2*pi*ScaleTable(13)*f0*t16); notes.re2s = mod16.*cos(2*pi*ScaleTable(14)*f0*t16); notes.mi2s = mod16.*cos(2*pi*ScaleTable(15)*f0*t16); notes.fa2s = mod16.*cos(2*pi*ScaleTable(16)*f0*t16); notes.so2s = mod16.*cos(2*pi*ScaleTable(17)*f0*t16); notes.la2s = mod16.*cos(2*pi*ScaleTable(18)*f0*t16); notes.ti2s = mod16.*cos(2*pi*ScaleTable(19)*f0*t16); notes.do3s = mod16.*cos(2*pi*ScaleTable(20)*f0*t16); notes.blks = zeros(1,k); % Melody by Schau_mal part0={'mi1f' 'la0e' 'la0e' 'do1f' 'mi1f' ... 're1e' 're1s' 'mi1s' 're1e' 'do1e' 're1e' 'do1e' 'la0f' ... 'mi1f' 'la0e' 'la0e' 'do1f' 'mi1f' ... 'so1e' 're1s' 'mi1s' 're1e' 'do1e' 're1e' 'do1e' 'ti0e' 'so0e' ... 'mi1f' 'la0e' 'la0e' 'do1f' 'mi1f' ... 're1e' 're1s' 'mi1s' 're1e' 'do1e' 're1e' 'do1e' 'la0e' 'so0e' ... 'mi1f' 'la0e' 'la0e' 'do1f' 'mi1f' ... 'so1e' 'mi1e' 'blkf' 'blkf' 'blkf' }; part1={'la0f' 'la0e' 'so0e' 'la0f' 'la0e' 'do1e' ... 'do1f' 're1e' 'do1e' 'la0f' 'la0f' ... 'do1f' 'do1e' 'so0e' 'do1e' 're1e' 'mi1e' 'so1e' ... 'so1e' 'mi1e' 're1f' 'mi1f' 'mi1f' ... 'la1e' 'la1e' 'la1e' 'so1e' 'mi1e' 'mi1f' 'do1e' ... 'la0e' 'la0e' 'la0e' 'mi1e' 're1s' 'mi1s' 're1e' 're1f' ... 'mi1e' 'mi1e' 'so1e' 'mi1e' 're1e' 'mi1e' 're1e' 'do1e' ... 'la0f' 'so0f' 'la0f' 'la0f' ... }; part2={'mi1e' 'mi1e' 'so1e' 'mi1e' 'mi1e' 'so1e' 'so1e' 'la1e' ... 'do2e' 'la1e' 'so1f' 'la1s' 'do2s' 'la1e' 'la1f' ... 'la0f' 'la0e' 'so0e' 'la0f' 'do1f' ... 're1e' 'mi1s' 're1s' 'do1e' 're1e' 'mi1f' 'mi1f' ... 'la0e' 'la1e' 'la1e' 'so1e' 're1e' 'mi1s' 're1s' 'do1e' 're1e' ... 'mi1f' 'mi1f' 'blke' 'blke' 'blkf' ... 'do1e' 'la0e' 'la0e' 'do1e' 're1f' 'so0e' 'so0e' ... 'mi1e' 'so1e' 'mi1e' 're1e' 'do1f' 'do1f' ... 'la0e' 'do1e' 're1e' 'mi1e' 're1e' 'do1e' 'so0e' 'mi0e' ... 'la0f' 'la0f' 'blke' 'blke' 'blkf' ... }; part3={'la0f' 'la0e' 'so0e' 'la0f' 'do1f' ... 're1e' 'mi1s' 're1s' 'do1e' 're1e' 'mi1f' 'mi1f' ... 'la0e' 'la1e' 'la1e' 'so1e' 're1e' 'mi1s' 're1s' 'do1e' 're1e' ... 'mi1f' 'mi1f' 'blke' 'blke' 'blkf' ... 'do1e' 'la0e' 'la0e' 'do1e' 're1f' 'so0e' 'so0e' ... 'mi1e' 'so1e' 'mi1e' 're1e' 'do1f' 'do1e' 'do1e' ... 'la0e' 'do1e' 're1e' 'mi1e' 'so1e' 'mi1e' 'mi1e' 'so1e' ... 'la1f' 'la1f' 'la1f' 'la1f' ... }; part4={'la1e' 'la1s' 'la1s' 'la1e' 'la1e' 'la1e' 'la1s' 'so1s' 'mi1e' 're1e' ... 're1e' 're1s' 're1s' 'mi1e' 'mi1s' 'so1s' 'mi1e' 'mi1s' 're1s' 'do1e' 'do1s' 'la0s' ... 'la0f' 'la0e' 'so0e' 'la0f' 'la0e' 'do1e' ... 're1e' 'mi1s' 're1s' 'do1e' 're1e' 'mi1f' 'mi1f' ... 'la1e' 'so1e' 'mi1e' 're1e' 'so1e' 'mi1e' 're1e' 'do1e' ... 'do1f' 'do1f' 'la0s' 'do1s' 're1s' 'mi1s' 're1s' 'do1s' 'la0s' 'do1s'}; part5={'do2e' 'do2s' 'do2s' 'la1e' 'la1s' 'la1s' 'so1e' 'so1s' 'so1s' 'mi1e' 'mi1s' 'mi1s' ... 're1e' 'mi1s' 're1s' 'do1e' 'la0s' 'so0s' 'la0s' 'so0s' 'do1s' 're1s' 'mi1s' 'so1s' 'la1s' 're2s' ... 'do2f' 'do2f' 'blks' 'blks' 'blks' 'blks' 'do1e' 're1e' ... 'mi1f' 'mi1f' 'mi1f' 'so1e' 'mi1e' ... 'la1f' 'la1f' 'la1e' 'do1e' 'so1e' 'mi1e' ... 're1f' 're1e' 're1s' 're1s' 're1e' 're1e' 'do1e' 're1e' ... 'mi1f' 'mi1e' 'mi1s' 'mi1s' 'mi1e' 're1s' 'do1s' 'ti0e' 'do1s' 're1s' ... 'mi1f' 'mi1f' 'mi1f' 'so1e' 'mi1e' ... 'do2f' 'la1f' 'la1f' 'la1e' 'do1e' ... 're1f' 'so1f' 'so1f' 'la1f' ... 'ti1f' 'ti1f' 'ti1f' 'ti1f' ... }; part6={'blkf' 'blkf' 'mi1e' 'so1e' 'mi1e' 'so1e' ... 'mi1f' 'la0e' 'la0s' 'la0s' 'do1f' 'la0e' 'mi1s' 'la0s' ... 'do1e' 'do1s' 'do1s' 're1e' 'do1s' 're1s' 'mi1f' 'mi1f' ... 'mi1f' 'la0e' 'la0s' 'la0s' 'so1f' 're1e' 're1s' 're1s' ... 'mi1f' 'mi1f' 'mi1s' 're1s' 'do1s' 'la0s' 'mi0s' 're0s' 'mi0s' 'so0s' ... 'do1f' 'la0e' 'la0s' 'la0s' 're1f' 'so0e' 'so0s' 'so0s' ... 'mi0f' 'so0e' 'so0s' 'so0s' 'do1f' 'do1f' ... 'la0f' 'do1e' 'do1s' 'la0s' 'mi1e' 'mi1s' 'mi1s' 're1e' 're1s' 'mi1s' ... }; % Combination, v1 is complete version, v2 is simple version. v1 = [part0 part1 part1 part2 part3 part4 part0 part1 part1 part2 part3 part5 part3 part6 part3]; %v2 = [part0 part1 part1 part2 part3 part5 part3 part6 part3]; end
到此这篇关于Matlab实现别踩白块小游戏的示例代码的文章就介绍到这了,更多相关Matlab别踩白块游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)