用matlab编写的俄罗斯方块小游戏Word下载.docx
《用matlab编写的俄罗斯方块小游戏Word下载.docx》由会员分享,可在线阅读,更多相关《用matlab编写的俄罗斯方块小游戏Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
![用matlab编写的俄罗斯方块小游戏Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/27/37f2612e-260d-40f9-8af4-c838bac1d654/37f2612e-260d-40f9-8af4-c838bac1d6541.gif)
HelpFcn'
helpdlg('
帮不了你--!
'
不好意思'
)'
CloseRequestFcn'
CloseFigure_Callback'
generate_FigureContent(FigureHandle);
init_FigureContent(FigureHandle);
set(FigureHandle,'
Visible'
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,'
开始新游戏'
Accelerator'
N'
Callback'
StartNewGame_Callback'
);
SaveMenu=uimenu(BeginMenu,'
保存'
S'
Enable'
Separator'
Cal'
SaveGame_Callback'
);
LoadMenu=uimenu(BeginMenu,'
读取'
L'
LoadGame_Callback'
QuitMenu=uimenu(BeginMenu,'
退出'
Q'
close(gcf)'
OperationMenu=uimenu(FigureHandle,'
功能'
BoardConfigMenu=uimenu(OperationMenu,'
label'
键盘设置'
BoardConfig_Callback'
FigureConfigMenu=uimenu(OperationMenu,'
界面设置'
FigureConfig_Callback'
HighScoreMenu=uimenu(OperationMenu,'
最高记录'
HighScore_Callback'
GameLevelMenu=uimenu(OperationMenu,'
游戏难度'
GameLevel_Callback'
HelpMenu=uimenu(FigureHandle,'
帮助'
AboutMenu=uimenu(HelpMenu,'
关于此软件'
关于此软件…………'
HelpDlgMenu=uimenu(HelpMenu,'
游戏帮助'
%创建工具条,图标可以用imread从图片读取,但图片不要太大
BeginTool=uipushtool('
ToolTipString'
CData'
rand(16,16,3),'
BeginTool'
ClickedCallback'
PauseTool=uitoggletool('
暂停'
PauseTool'
reshape(repmat([110],16,16),[16,16,3]),...
PauseGame_Callback'
%创建游戏窗口
MainWindowXPos=TabSpace;
MainWindowYPos=TabSpace;
MainWindowWidth=BlockWidth*12;
MainWindowHeight=BlockHeight*22;
MainWindowPosition=[MainWindowXPosMainWindowYPosMainWindowWidthMainWindowHeight];
%定义游戏窗口的右键菜单
AxesContextMenu=uicontextmenu('
uicontextmenu'
uimenu(AxesContextMenu,'
设置窗口颜色'
WindowColor_Callback'
)
设置背景图片'
WindowPicture_Callback'
设置方块颜色'
BlockColor_Callback'
恢复默认'
Default_Callback'
MainAxes=axes('
Units'
pixels'
Pos'
MainWindowPosition,'
XTick'
[],'
YTick'
[],'
XTickLabel'
[],...
YTickLabel'
Box'
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('
NextBlockWndPosition,'
[],...
[0NextBlockWndWidth],...
[0NextBlockWndHeight],...
NextBlockAxes'
Color'
[0.850.850.85]);
%创建一组控件,包括(两个文本框用于显示当前方块数和成绩,两个按钮用于暂停和退出)
ButtonTag={'
QuitButton'
PauseButton'
BlockNumText'
ScoreText'
};
ButtonStyle={'
pushbutton'
togglebutton'
text'
FontColor={[000],[100],[001],[101]};
ButtonColor={[0.70.80.9],[0.310.3],[0.511],[0.511]};
ButtonString={'
方块数'
积分'
};
ButtonCallback={'
ButtonPauseGame_Callback'
ButtonNumber=length(ButtonTag);
ButtonWidth=NextBlockWndWidth;
ButtonHeight=50;
ButtonXPos=NextBlockWndXPos;
ButtonYPos=MainWindowYPos+TabSpace;
ButtonPosition=[ButtonXPosButtonYPosButtonWidthButtonHeight];
ButtonTabSpace=(NextBlockWndYPos-2*TabSpace-ButtonHeight*ButtonNumber)/ButtonNumber;
fornum=1:
ButtonNumber
TempButtonPosition=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;
TempButtonPosition(4)=TempButtonPosition(4)-5*2;
else
TempButtonPosition
(1)=TempButtonPosition
(1)-10;
TempButtonPosition
(2)=TempButtonPosition
(2)-5;
TempButtonPosition(3)=TempButtonPosition(3)+10*2;
TempButtonPosition(4)=TempButtonPosition(4)+5*2;
ButtonHandle=uicontrol('
ButtonTag{num},'
Style'
ButtonStyle{num},'
TempButtonPosition,...
Foregroundcolor'
FontColor{num},'
Backgroundcolor'
ButtonColor{num},...
Fontsize'
16,'
String'
ButtonString{num},'
ButtonCallback{num});
set(ButtonHandle,'
Max'
2);
iffindstr(ButtonTag{num},'
inactive'
ButtonDownFcn'
ButtonCallback{num},'
MainBlockAxes=axes('
MainBlockAxes'
Hittest'
[0MainWindowHeight],'
line('
BlockHandle'
Markersize'
18,'
Parent'
MainBlockAxes,'
HitTest'
Marker'
s'
MarkerEdgeColor'
k'
XData'
nan,'
YData'
LineStyle'
TempBlock'
130,'
30,'
NextBlock'
NextBlockAxes,'
setappdata(FigureHandle,'
[0MainWindowWidth])
[0MainWindowHeight])
handles=guihandles(FigureHandle);
guidata(FigureHandle,handles);
functioninit_FigureContent(FigureHandle)
handles=guidata(FigureHandle);
ColorInfo=[];
try
ColorInfo=load('
ColorInfo.mat'
);
catch
ifisempty(ColorInfo)
ColorInfo.BlockColor=GetDefaultBlockColor;
ColorInfo.MainAxesColor=GetDefaultMainAxesColor;
ColorInfo.MainAxesImage.ImageData=[];
set(handles.MainAxes,'
ColorInfo.MainAxesColor);
if~isempty(ColorInfo.MainAxesImage.ImageData)
ImageHandle=image(ColorInfo.MainAxesImage.ImageData,'
handles.MainAxes);
set(ImageHandle,ColorInfo.MainAxesImage.Property);
setappdata(FigureHandle,'
ImageData'
ColorInfo.MainAxesImage.ImageData);
set(handles.BlockHandle,'
MarkerFaceColor'
ColorInfo.BlockColor);
set(handles.TempBlock,'
set(handles.NextBlock,'
BlockColor'
%------------------------------------------------------------
functionStartNewGame_Callback(h,StartType)
handles=guidata(h);
globalPauseTime
ifnargin==1
StartType='
NewStart'
;
setappdata(handles.RussiaBlock,'
BlockNumber'
0);
set(handles.BlockNumText,'
{'
0'
CurrentScore'
set(handles.ScoreText,'
set(handles.BlockHandle,'
nan);
set(handles.TempBlock,'
TextHandle=findobj('
handles.MainBlockAxes,'
delete(TextHandle);
else
set(handles.PauseTool,'
State'
set(handles.PauseButton,'
Value'
YLim=get(handles.MainAxes,'
while(ishandle(h))
TotalYData=get(handles.BlockHandle,'
ifany(TotalYData>
=YLim
(2))
%Gameover
text(20,200,'
GameOver'
handles.MainBlockAxes,...
FontSize'
r'
FontAngle'
italic'
bre