没有合适的资源?快使用搜索试试~ 我知道了~
首页Matlab 小游戏汇总
Matlab 小游戏汇总
1星 需积分: 47 141 下载量 199 浏览量
更新于2023-05-29
评论 14
收藏 469KB PDF 举报
在网上找了一些matlab小游戏代码,我自己也对其中一两个作了少量改进。
资源详情
资源评论
资源推荐
第 1 页,共 57 页
Matlab 小游戏汇总
俄罗斯方块
程序代码
function RussiaBlock( varargin )
if nargin == 0
OldHandle = findobj( 'Type', 'figure', 'Tag', 'RussiaBlock' ) ;
if ishandle( OldHandle )
delete( OldHandle ) ;
end
FigureHandle = 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' ) ;
elseif ischar( varargin{1} )
feval( varargin{:} ) ;
end
% -------------------------------------------------------------------------
function generate_FigureContent( FigureHandle )
TabSpace = 30 ;
BlockWidth = 20 ;
BlockHeight = 20 ;
FigureWidth = BlockWidth * (12 + 1) + TabSpace * 7;
FigureHeight = 500 ;
set( FigureHandle, 'Position', [0 0 FigureWidth FigureHeight] ) ;
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 )' );
第 2 页,共 57 页
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( [1 1 0], 16, 16), [16,16,3] ),...
'ClickedCallback', 'RussiaBlock( ''PauseGame_Callback'', gcbo )' ) ;
% 创建游戏窗口
MainWindowXPos = TabSpace;
MainWindowYPos = TabSpace;
MainWindowWidth = BlockWidth * 12 ;
MainWindowHeight = BlockHeight * 22 ;
MainWindowPosition = [MainWindowXPos MainWindowYPos MainWindowWidth MainWindowHeight] ;
% 定义游戏窗口的右键菜单
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', [0 MainWindowWidth], 'YLim', [0 MainWindowHeight] ) ;
hold on;
% 创建一个窗口用于显示下一个方块的图形
NextBlockWndXPos = MainWindowXPos + MainWindowWidth + TabSpace ;
NextBlockWndHeight = 4 * TabSpace + BlockHeight ;
NextBlockWndYPos = MainWindowYPos + MainWindowHeight - NextBlockWndHeight ;
NextBlockWndWidth = TabSpace * 4 + BlockWidth ;
第 3 页,共 57 页
NextBlockWndPosition = [NextBlockWndXPos NextBlockWndYPos NextBlockWndWidth NextBlockWndHeight] ;
NextBlockAxes = axes( 'Units', 'pixels', 'Pos', NextBlockWndPosition, 'XTick', [], 'YTick',[],...
'XTickLabel', [], 'YTickLabel', [], 'XLim', [0 NextBlockWndWidth],...
'YLim', [0 NextBlockWndHeight], ...
'Box', 'on', 'Tag', 'NextBlockAxes', 'Color', [0.85 0.85 0.85] ) ;
% 创建一组控件,包括(两个文本框用于显示当前方块数和成绩,两个按钮用于暂停和退出)
ButtonTag = { 'QuitButton', 'PauseButton', 'BlockNumText', 'ScoreText' } ;
ButtonStyle = { 'pushbutton', 'togglebutton', 'text', 'text' } ;
FontColor = { [0 0 0], [1 0 0], [0 0 1], [1 0 1] } ;
ButtonColor = { [0.7 0.8 0.9], [0.3 1 0.3], [0.5 1 1], [0.5 1 1] } ;
ButtonString = { '退出', '暂停', '方块数', '积分' };
ButtonCallback = { 'close(gcf)', 'RussiaBlock( ''ButtonPauseGame_Callback'', gcbo )', '', '' } ;
ButtonNumber = length( ButtonTag ) ;
ButtonWidth = NextBlockWndWidth ;
ButtonHeight = 50 ;
ButtonXPos = NextBlockWndXPos ;
ButtonYPos = MainWindowYPos + TabSpace ;
ButtonPosition = [ButtonXPos ButtonYPos ButtonWidth ButtonHeight] ;
ButtonTabSpace = (NextBlockWndYPos - 2 * TabSpace - ButtonHeight * ButtonNumber) / ButtonNumber ;
for num = 1: ButtonNumber
TempButtonPosition = ButtonPosition ;
TempButtonPosition(2) = ButtonPosition(2) + (num - 1) * (ButtonTabSpace + ButtonHeight);
if findstr( 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 ;
end
ButtonHandle = uicontrol( 'Tag', ButtonTag{num}, 'Style', ButtonStyle{num}, 'Pos',
TempButtonPosition,...
'Foregroundcolor', FontColor{num}, 'Backgroundcolor', ButtonColor{num},...
'Fontsize', 16, 'String', ButtonString{num}, 'Cal', ButtonCallback{num} ) ;
if findstr( ButtonStyle{num}, 'text' )
set( ButtonHandle, 'Max', 2 ) ;
end
if findstr( ButtonTag{num}, 'PauseButton' )
set( ButtonHandle, 'Enable', 'inactive', 'ButtonDownFcn', ButtonCallback{num}, 'Cal', '' ) ;
end
end
MainBlockAxes = axes( 'Units', 'pixels', 'Pos', MainWindowPosition, 'XTick', [], 'YTick',[],
'XTickLabel', [],...
第 4 页,共 57 页
'YTickLabel', [], 'Box', 'on', 'Tag', 'MainBlockAxes', 'Hittest', 'off',...
'XLim', [0 MainWindowWidth], 'YLim', [0 MainWindowHeight], 'Color', 'none' ) ;
line( 'Visible', 'on', 'Tag', 'BlockHandle', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest',
'off',...
'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', nan, 'YData', nan, 'LineStyle', 'none' ) ;
line( 'Visible', 'off', 'Tag', 'TempBlock', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest',
'off',...
'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 130, 'YData', 30, 'LineStyle', 'none' ) ;
line( 'Visible', 'off', 'Tag', 'NextBlock', 'Markersize', 18, 'Parent', NextBlockAxes, 'HitTest',
'off',...
'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 30, 'YData', 30, 'LineStyle', 'none' ) ;
setappdata( FigureHandle, 'XLim', [0 MainWindowWidth] )
setappdata( FigureHandle, 'YLim', [0 MainWindowHeight] )
handles = guihandles( FigureHandle ) ;
guidata( FigureHandle, handles ) ;
% -------------------------------------------------------------------------
function init_FigureContent( FigureHandle )
handles = guidata( FigureHandle ) ;
ColorInfo = [] ;
try
ColorInfo = load('ColorInfo.mat') ;
catch
end
if isempty( ColorInfo )
ColorInfo.BlockColor = GetDefaultBlockColor ;
ColorInfo.MainAxesColor = GetDefaultMainAxesColor ;
ColorInfo.MainAxesImage.ImageData = [] ;
end
set( handles.MainAxes, 'Color', ColorInfo.MainAxesColor ) ;
if ~isempty( ColorInfo.MainAxesImage.ImageData )
ImageHandle = image( ColorInfo.MainAxesImage.ImageData, 'Parent', handles.MainAxes ) ;
set( ImageHandle, ColorInfo.MainAxesImage.Property ) ;
setappdata( FigureHandle, 'ImageData', ColorInfo.MainAxesImage.ImageData ) ;
end
set( handles.BlockHandle, 'MarkerFaceColor', ColorInfo.BlockColor ) ;
set( handles.TempBlock, 'MarkerFaceColor', ColorInfo.BlockColor ) ;
set( handles.NextBlock, 'MarkerFaceColor', ColorInfo.BlockColor ) ;
setappdata( FigureHandle, 'BlockColor', ColorInfo.BlockColor ) ;
% ------------------------------------------------------------
function StartNewGame_Callback( h, StartType )
handles = guidata( h ) ;
global PauseTime
if nargin == 1
StartType = 'NewStart' ;
setappdata( handles.RussiaBlock, 'BlockNumber', 0 ) ;
set( handles.BlockNumText, 'String', {'方块数','0'} ) ;
第 5 页,共 57 页
setappdata( handles.RussiaBlock, 'CurrentScore', 0 ) ;
set( handles.ScoreText, 'String', {'积分','0'} ) ;
set( handles.BlockHandle, 'XData', nan, 'YData', nan ) ;
set( handles.TempBlock, 'XData', nan, 'YData', nan ) ;
TextHandle = findobj( 'Parent', handles.MainBlockAxes, 'Type', 'text' ) ;
delete( TextHandle ) ;
else
end
set( handles.NextBlock, 'Visible', 'on' ) ;
set( handles.TempBlock, 'Visible', 'on' ) ;
set( handles.PauseTool, 'State', 'off' ) ;
set( handles.PauseButton, 'Value', 0 ) ;
YLim = get( handles.MainAxes, 'YLim' ) ;
while( ishandle( h ) )
TotalYData = get( handles.BlockHandle, 'YData' ) ;
if any( TotalYData >= YLim(2) )
% Game over
text( 20, 200, 'GameOver', 'Parent', handles.MainBlockAxes,...
'FontSize', 30, 'Color', 'r', 'FontAngle', 'italic' ) ;
break;
end
if length( TotalYData ) >= 4
TotalXData = get( handles.BlockHandle, 'XData' ) ;
LastBlockYData = TotalYData( end - 3: end ) ;
LastBlockYData = unique( LastBlockYData ) ;
CompleteLine = [] ;
UsefulIndex = [] ;
for num = 1: length( LastBlockYData )
[YData, Index] = find( TotalYData == LastBlockYData(num) ) ;
if length( YData ) == 12
CompleteLine = [CompleteLine, LastBlockYData(num)] ;
UsefulIndex = [UsefulIndex, Index] ;
end
end
if ~isempty( CompleteLine )
TotalXData( UsefulIndex ) = [] ;
TotalYData( UsefulIndex ) = [] ;
LineNumber = length( CompleteLine ) ;
ScoreArray = [100 300 600 1000] ;
NewScore = ScoreArray(LineNumber) ;
CurrentScore = getappdata( handles.RussiaBlock, 'CurrentScore' ) ;
TextString = get( handles.ScoreText, 'String' ) ;
TextString{2} = CurrentScore + NewScore ;
set( handles.ScoreText, 'String', TextString ) ;
setappdata( handles.RussiaBlock, 'CurrentScore', CurrentScore + NewScore ) ;
剩余56页未读,继续阅读
生生__不息
- 粉丝: 4
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论2