用matlab编写的俄罗斯方块小游戏

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

用matlab编写的俄罗斯方块小游戏functionRussiaBlock(varargin)ifnargin==0OldHandle=findobj('Type','figure','Tag','RussiaBlock');ifishandle(OldHandle)delete(OldHandle);endFigureHandle=figure('Name','俄罗斯方块MATLAB版','Tag','RussiaBlock','NumberTitle','off',...'Menubar','none','DoubleBuffer','on','Resize','off','visible','on',...'KeyPressFcn','RussiaBlock(''KeyPress_Callback'',gcbo)',...'HelpFcn','helpdlg(''帮不了你--!'',''不好意思'')',...'CloseRequestFcn','RussiaBlock(''CloseFigure_Callback'',gcbo)');generate_FigureContent(FigureHandle);init_FigureContent(FigureHandle);set(FigureHandle,'Visible','on');elseifischar(varargin{1})feval(varargin{:});end%-------------------------------------------------------------------------functiongenerate_FigureContent(FigureHandle)TabSpace=30;BlockWidth=20;BlockHeight=20;FigureWidth=BlockWidth*(12+1)+TabSpace*7;FigureHeight=500;set(FigureHandle,'Position',[00FigureWidthFigureHeight]);movegui(FigureHandle,'center');%创建菜单BeginMenu=uimenu(FigureHandle,'Label','开始');StartMenu=uimenu(BeginMenu,'Label','开始新游戏','Accelerator','N',...'Callback','RussiaBlock(''StartNewGame_Callback'',gcbo)');SaveMenu=uimenu(BeginMenu,'Label','保存','Accelerator','S','Enable','off',...'Separator','on','Cal','RussiaBlock(''SaveGame_Callback'',gcbo)');LoadMenu=uimenu(BeginMenu,'Label','读取','Accelerator','L','Enable','off',...'Cal','RussiaBlock(''LoadGame_Callback'',gcbo)');QuitMenu=uimenu(BeginMenu,'Label','退出','Accelerator','Q','Separator','on','Cal','close(gcf)');OperationMenu=uimenu(FigureHandle,'Label','功能');BoardConfigMenu=uimenu(OperationMenu,'label','键盘设置','Enable','off',...'Cal','RussiaBlock(''BoardConfig_Callback'',gcbo)');FigureConfigMenu=uimenu(OperationMenu,'label','界面设置','Enable','off',...'Cal','RussiaBlock(''FigureConfig_Callback'',gcbo)');HighScoreMenu=uimenu(OperationMenu,'label','最高记录','Separator','on',...'Cal','RussiaBlock(''HighScore_Callback'',gcbo)','Enable','off');GameLevelMenu=uimenu(OperationMenu,'Label','游戏难度',...'Cal','RussiaBlock(''GameLevel_Callback'',gcbo)');HelpMenu=uimenu(FigureHandle,'Label','帮助');AboutMenu=uimenu(HelpMenu,'Label','关于此软件','Cal','helpdlg(''俄罗斯方块MATLAB版'',''关于此软件…………'')');HelpDlgMenu=uimenu(HelpMenu,'Label','游戏帮助','Separator','on','Cal','helpdlg(''帮不了你--!'',''不好意思'')');%创建工具条,图标可以用imread从图片读取,但图片不要太大BeginTool=uipushtool('ToolTipString','开始','CData',rand(16,16,3),'Tag','BeginTool',...'ClickedCallback','RussiaBlock(''StartNewGame_Callback'',gcbo)');PauseTool=uitoggletool('ToolTipString','暂停','Tag','PauseTool','Tag','PauseTool',...'CData',reshape(repmat([110],16,16),[16,16,3]),...'ClickedCallback','RussiaBlock(''PauseGame_Callback'',gcbo)');%创建游戏窗口MainWindowXPos=TabSpace;MainWindowYPos=TabSpace;MainWindowWidth=BlockWidth*12;MainWindowHeight=BlockHeight*22;MainWindowPosition=[MainWindowXPosMainWindowYPosMainWindowWidthMainWindowHeight];%定义游戏窗口的右键菜单AxesContextMenu=uicontextmenu('Tag','uicontextmenu');uimenu(AxesContextMenu,'Label','设置窗口颜色','Cal','RussiaBlock(''WindowColor_Callback'',gcbo)')uimenu(AxesContextMenu,'Label','设置背景图片','Cal','RussiaBlock(''WindowPicture_Callback'',gcbo)')uimenu(AxesContextMenu,'Label','设置方块颜色','Cal','RussiaBlock(''BlockColor_Callback'',gcbo)')uimenu(AxesContextMenu,'Label','恢复默认','Cal','RussiaBlock(''Default_Callback'',gcbo)')MainAxes=axes('Units','pixels','Pos',MainWindowPosition,'XTick',[],'YTick',[],'XTickLabel',[],...'YTickLabel',[],'Box','on','Tag','MainAxes','UicontextMenu',AxesContextMenu,...'XLim',[0MainWindowWidth],'YLim',[0MainWindowHeight]);holdon;%创建一个窗口用于显示下一个方块的图形NextBlockWndXPos=MainWindowXPos+MainWindowWidth+TabSpace;NextBlockWndHeight=4*TabSpace+BlockHeight;NextBlockWndYPos=MainWindowYPos+MainWindowHeight-NextBlockWndHeight;NextBlockWndWidth=TabSpace*4+BlockWidth;NextBlockWndPosition=[NextBlockWndXPosNextBlockWndYPosNextBlockWndWidthNextBlockWndHeight];NextBlockAxes=axes('Units','pixels','Pos',NextBlockWndPosition,'XTick',[],'YTick',[],...'XTickLabel',[],'YTickLabel',[],'XLim',[0NextBlockWndWidth],...'YLim',[0NextBlockWndHeight],...'Box','on','Tag','NextBlockAxes','Color',[0.850.850.85]);%创建一组控件,包括(两个文本框用于显示当前方块数和成绩,两个按钮用于暂停和退出)ButtonTag={'QuitButton','PauseButton','BlockNumText','ScoreText'};ButtonStyle={'pushbutton','togglebutton','text','text'};FontColor={[000],[100],[001],[101]};ButtonColor={[0.70.80.9],[0.310.3],[0.511],[0.511]};ButtonString={'退出','暂停','方块数','积分'};ButtonCallback={'close(gcf)','RussiaBlock(''ButtonPauseGame_Callback'',gcbo)','',''};ButtonNumber=length(ButtonTag);ButtonWidth=NextBlockWndWidth;ButtonHeight=50;ButtonXPos=NextBlockWndXPos;ButtonYPos=MainWindowYPos+TabSpace;ButtonPosition=[ButtonXPosButtonYPosButtonWidthButtonHeight];ButtonTabSpace=(NextBlockWndYPos-2*TabSpace-ButtonHeight*ButtonNumber)/ButtonNumber;fornum=1:ButtonNumberTempButtonPosition=ButtonPosition;TempButtonPosition(2)=ButtonPosition(2)+(num-1)*(ButtonTabSpace+ButtonHeight);iffindstr(ButtonStyle{num},'button')TempButtonPosition(1)=TempButtonPosition(1)+10;TempButtonPosition(2)=TempButtonPosition(2)+5;TempButtonPosition(3)=TempButtonPosition(3)-10*2;TempButtonP

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功