用matlab编写的俄罗斯方块小游戏_第1页
用matlab编写的俄罗斯方块小游戏_第2页
用matlab编写的俄罗斯方块小游戏_第3页
用matlab编写的俄罗斯方块小游戏_第4页
用matlab编写的俄罗斯方块小游戏_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用matlab编写的俄罗斯方块小游戏· function RussiaBlock( varargin )if nargin = 0      OldHandle = findobj( 'Type', 'figure', 'Tag', 'RussiaBlock' ) ;  if ishandle( OldHandle )  delete( OldHandle ) ;  end      FigureHandle =

2、 figure( 'Name', '俄罗斯方块MATLAB版', 'Tag', 'RussiaBlock', 'NumberTitle', 'off',.  'Menubar', 'none', 'DoubleBuffer', 'on', 'Resize', 'off', 'visible', 'on',.  'KeyPressFcn',

3、 'RussiaBlock( ''KeyPress_Callback'', gcbo )',.  'HelpFcn', 'helpdlg(''帮不了你- -!'',''不好意思'')',.  'CloseRequestFcn', 'RussiaBlock( ''CloseFigure_Callback'', gcbo )' ) ;    &

4、#160;   generate_FigureContent( FigureHandle ) ;      init_FigureContent( FigureHandle ) ;      set( FigureHandle, 'Visible', 'on' ) ;    elseif ischar( varargin1 )      feval( varargin: ) ;  &#

5、160; 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' ) ;% 创建菜单B

6、eginMenu = uimenu( FigureHandle, 'Label', '开始' ) ;StartMenu = uimenu( BeginMenu, 'Label', '开始新游戏', 'Accelerator', 'N',.  'Callback', 'RussiaBlock( ''StartNewGame_Callback'', gcbo )');SaveMenu = uimenu( BeginMenu, &#

7、39;Label', '保存', 'Accelerator', 'S', 'Enable', 'off',.  'Separator', 'on', 'Cal', 'RussiaBlock( ''SaveGame_Callback'', gcbo )' );LoadMenu = uimenu( BeginMenu, 'Label', '读取', 'Accelera

8、tor', 'L', 'Enable', 'off',.  'Cal', 'RussiaBlock( ''LoadGame_Callback'', gcbo )' );QuitMenu = uimenu( BeginMenu, 'Label', '退出', 'Accelerator', 'Q', 'Separator', 'on', 'Cal', '

9、;close(gcf)');OperationMenu = uimenu( FigureHandle, 'Label', '功能' );BoardConfigMenu = uimenu( OperationMenu, 'label', '键盘设置', 'Enable', 'off',.  'Cal', 'RussiaBlock( ''BoardConfig_Callback'', gcbo )' );FigureCon

10、figMenu = uimenu( OperationMenu, 'label', '界面设置', 'Enable', 'off',.  'Cal', 'RussiaBlock( ''FigureConfig_Callback'', gcbo )' );HighScoreMenu = uimenu( OperationMenu, 'label', '最高记录', 'Separator', 'on'

11、;,.  'Cal', 'RussiaBlock( ''HighScore_Callback'', gcbo )', 'Enable', 'off' );GameLevelMenu = uimenu( OperationMenu, 'Label', '游戏难度',.  'Cal','RussiaBlock( ''GameLevel_Callback'', gcbo )' );HelpM

12、enu = uimenu( FigureHandle, 'Label', '帮助' );AboutMenu = uimenu( HelpMenu, 'Label', '关于此软件', 'Cal', 'helpdlg(''俄罗斯方块MATLAB版'',''关于此软件'')');HelpDlgMenu = uimenu( HelpMenu, 'Label', '游戏帮助', 'Separator

13、9;, 'on', 'Cal', 'helpdlg(''帮不了你- -!'',''不好意思'')' );% 创建工具条,图标可以用imread从图片读取,但图片不要太大BeginTool = uipushtool( 'ToolTipString', '开始', 'CData', rand(16,16,3), 'Tag', 'BeginTool',.  'ClickedCallback&#

14、39;, 'RussiaBlock( ''StartNewGame_Callback'', gcbo )' ) ;PauseTool = uitoggletool( 'ToolTipString', '暂停', 'Tag', 'PauseTool', 'Tag', 'PauseTool',.  'CData', reshape( repmat( 1 1 0, 16, 16), 16,16,3 ),.  'Cl

15、ickedCallback', 'RussiaBlock( ''PauseGame_Callback'', gcbo )' ) ;% 创建游戏窗口MainWindowXPos = TabSpace;MainWindowYPos = TabSpace;MainWindowWidth = BlockWidth * 12 ;  MainWindowHeight = BlockHeight * 22 ;MainWindowPosition = MainWindowXPos MainWindowYPos MainWindowWidth M

16、ainWindowHeight ;% 定义游戏窗口的右键菜单AxesContextMenu = uicontextmenu( 'Tag', 'uicontextmenu' ) ;uimenu( AxesContextMenu, 'Label', '设置窗口颜色', 'Cal', 'RussiaBlock( ''WindowColor_Callback'', gcbo )' )uimenu( AxesContextMenu, 'Label', '

17、设置背景图片', 'Cal', 'RussiaBlock( ''WindowPicture_Callback'', gcbo )' )uimenu( AxesContextMenu, 'Label', '设置方块颜色', 'Cal', 'RussiaBlock( ''BlockColor_Callback'', gcbo )' )uimenu( AxesContextMenu, 'Label', '恢复默

18、认', 'Cal', 'RussiaBlock( ''Default_Callback'', gcbo )' )MainAxes = axes( 'Units', 'pixels', 'Pos', MainWindowPosition, 'XTick', , 'YTick', 'XTickLabel', ,.  'YTickLabel', , 'Box', 'on', &

19、#39;Tag', 'MainAxes', 'UicontextMenu', AxesContextMenu,.  'XLim', 0 MainWindowWidth, 'YLim', 0 MainWindowHeight ) ;hold on;% 创建一个窗口用于显示下一个方块的图形NextBlockWndXPos = MainWindowXPos + MainWindowWidth + TabSpace ;NextBlockWndHeight = 4 * TabSpace + BlockHeight ;Nex

20、tBlockWndYPos = MainWindowYPos + MainWindowHeight - NextBlockWndHeight ;NextBlockWndWidth = TabSpace * 4 + BlockWidth ;NextBlockWndPosition = NextBlockWndXPos NextBlockWndYPos NextBlockWndWidth NextBlockWndHeight ;NextBlockAxes = axes( 'Units', 'pixels', 'Pos', NextBlockWndPo

21、sition, 'XTick', , 'YTick',.  'XTickLabel', , 'YTickLabel', , 'XLim', 0 NextBlockWndWidth,.  'YLim', 0 NextBlockWndHeight, .  'Box', 'on', 'Tag', 'NextBlockAxes', 'Color', 0.85 0.85 0.85 ) ;% 创建一组控

22、件,包括(两个文本框用于显示当前方块数和成绩,两个按钮用于暂停和退出)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

23、1 0.3, 0.5 1 1, 0.5 1 1 ;ButtonString = '退出', '暂停', '方块数', '积分' ;ButtonCallback = 'close(gcf)', 'RussiaBlock( ''ButtonPauseGame_Callback'', gcbo )', '', '' ;ButtonNumber = length( ButtonTag ) ;ButtonWidth = NextBlockWndW

24、idth ;ButtonHeight = 50 ;ButtonXPos = NextBlockWndXPos ;ButtonYPos = MainWindowYPos + TabSpace ;ButtonPosition = ButtonXPos ButtonYPos ButtonWidth ButtonHeight ;ButtonTabSpace = (NextBlockWndYPos - 2 * TabSpace - ButtonHeight * ButtonNumber) / ButtonNumber ;for num = 1: ButtonNumber   

25、;   TempButtonPosition = ButtonPosition ;  TempButtonPosition(2) = ButtonPosition(2) + (num - 1) * (ButtonTabSpace + ButtonHeight);  if findstr( ButtonStylenum, 'button' )  TempButtonPosition(1) = TempButtonPosition(1) + 10 ;  TempButtonPosition(2) = TempButtonPositi

26、on(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) = TempB

27、uttonPosition(3) + 10 * 2;  TempButtonPosition(4) = TempButtonPosition(4) + 5 * 2 ;  end      ButtonHandle = uicontrol( 'Tag', ButtonTagnum, 'Style', ButtonStylenum, 'Pos', TempButtonPosition,.  'Foregroundcolor', FontColornum, '

28、Backgroundcolor', ButtonColornum,.  'Fontsize', 16, 'String', ButtonStringnum, 'Cal', ButtonCallbacknum ) ;      if findstr( ButtonStylenum, 'text' )  set( ButtonHandle, 'Max', 2 ) ;  end  if findstr( ButtonTagnum, &

29、#39;PauseButton' )  set( ButtonHandle, 'Enable', 'inactive', 'ButtonDownFcn', ButtonCallbacknum, 'Cal', '' ) ;  endendMainBlockAxes = axes( 'Units', 'pixels', 'Pos', MainWindowPosition, 'XTick', , 'YTick',

30、'XTickLabel', ,.  'YTickLabel', , 'Box', 'on', 'Tag', 'MainBlockAxes', 'Hittest', 'off',.  'XLim', 0 MainWindowWidth, 'YLim', 0 MainWindowHeight, 'Color', 'none' ) ;line( 'Visible', '

31、;on', 'Tag', 'BlockHandle', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest', 'off',.  'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', nan, 'YData', nan, 'LineStyle', 'none' )

32、 ;line( 'Visible', 'off', 'Tag', 'TempBlock', 'Markersize', 18, 'Parent', MainBlockAxes, 'HitTest', 'off',.  'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 130, 'YData', 30, 'Lin

33、eStyle', 'none' ) ;line( 'Visible', 'off', 'Tag', 'NextBlock', 'Markersize', 18, 'Parent', NextBlockAxes, 'HitTest', 'off',.  'Marker', 's', 'MarkerEdgeColor', 'k', 'XData', 30, &

34、#39;YData', 30, 'LineStyle', 'none' ) ;setappdata( FigureHandle, 'XLim', 0 MainWindowWidth )setappdata( FigureHandle, 'YLim', 0 MainWindowHeight )handles = guihandles( FigureHandle ) ;guidata( FigureHandle, handles ) ;% -function init_FigureContent( FigureHandle )

35、handles = guidata( FigureHandle ) ;ColorInfo = ;try  ColorInfo = load('ColorInfo.mat') ;   catch    endif isempty( ColorInfo )  ColorInfo.BlockColor = GetDefaultBlockColor ;  ColorInfo.MainAxesColor = GetDefaultMainAxesColor ;  ColorInfo.MainAxesI

36、mage.ImageData = ;endset( handles.MainAxes, 'Color', ColorInfo.MainAxesColor ) ;if isempty( ColorInfo.MainAxesImage.ImageData )  ImageHandle = image( ColorInfo.MainAxesImage.ImageData, 'Parent', handles.MainAxes ) ;  set( ImageHandle, ColorInfo.MainAxesImage.Property ) ;

37、60; setappdata( FigureHandle, 'ImageData', ColorInfo.MainAxesImage.ImageData ) ;endset( handles.BlockHandle, 'MarkerFaceColor', ColorInfo.BlockColor ) ;set( handles.TempBlock, 'MarkerFaceColor', ColorInfo.BlockColor ) ;set( handles.NextBlock, 'MarkerFaceColor', ColorI

38、nfo.BlockColor ) ;setappdata( FigureHandle, 'BlockColor', ColorInfo.BlockColor ) ; % -function StartNewGame_Callback( h, StartType )handles = guidata( h ) ;global PauseTimeif nargin = 1  StartType = 'NewStart' ;  setappdata( handles.RussiaBlock, 'BlockNumber',

39、0 ) ;  set( handles.BlockNumText, 'String', '方块数','0' ) ;      setappdata( handles.RussiaBlock, 'CurrentScore', 0 ) ;  set( handles.ScoreText, 'String', '积分','0' ) ;      set( handles.BlockHandl

40、e, 'XData', nan, 'YData', nan ) ;  set( handles.TempBlock, 'XData', nan, 'YData', nan ) ;      TextHandle = findobj( 'Parent', handles.MainBlockAxes, 'Type', 'text' ) ;     delete( TextHandle ) ;elseendse

41、t( 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 ) )  &

42、#160; 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&

43、#39; ) ;  break;  end      if length( TotalYData ) >= 4    TotalXData = get( handles.BlockHandle, 'XData' ) ;  LastBlockYData = TotalYData( end - 3: end ) ;  LastBlockYData = unique( LastBlockYData ) ;  CompleteLine = ;  UsefulIn

44、dex = ;  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( Compl

45、eteLine )  TotalXData( UsefulIndex ) = ;  TotalYData( UsefulIndex ) = ;      LineNumber = length( CompleteLine ) ;  ScoreArray = 100 300 600 1000 ;  NewScore = ScoreArray(LineNumber) ;  CurrentScore = getappdata( handles.RussiaBlock, 'CurrentScore'

46、; ) ;  TextString = get( handles.ScoreText, 'String' ) ;  TextString2 = CurrentScore + NewScore ;     set( handles.ScoreText, 'String', TextString ) ;  setappdata( handles.RussiaBlock, 'CurrentScore', CurrentScore + NewScore ) ;   

47、   UpdateGameLevel( handles.RussiaBlock, CurrentScore + NewScore ) ;  % 处理需要下移的方块  for num = LineNumber : -1 : 1  YData, Index = find( TotalYData > LastBlockYData(num) ) ;  TotalYData(Index) = TotalYData(Index) - 20 ;  end  end  set( handles.BlockHandle, &#

48、39;XData', TotalXData, 'YData', TotalYData ) ;  end      BlockNumber = getappdata( handles.RussiaBlock, 'BlockNumber' ) ;  TextString = get( handles.BlockNumText, 'String' ) ;  TextString2 = BlockNumber + 1 ;     set( hand

49、les.BlockNumText, 'String', TextString ) ;  setappdata( handles.RussiaBlock, 'BlockNumber', BlockNumber + 1 ) ;      GameLevel = getappdata( handles.RussiaBlock, 'GameLevel' ) ;  if isempty( GameLevel )  PauseTime = 0.3 ;  else  Pa

50、useTime = ceil( 20 / GameLevel ) / 100 ;  end  TempBlockPos.LeftStep = 0 ;  TempBlockPos.DownStep = 0 ;  setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ;  Status = 1;     BlockXArray,BlockYArray = Com_GetBlock( h ) ;  set( handles

51、.TempBlock, 'XData', BlockXArray, 'YData', BlockYArray ) ;  while( Status & ishandle( h ) )  if(PauseTime) = 0  pause( PauseTime )  end  Status = test_MoveBlock( h, 'Down' ) ;  endend % -function KeyPress_Callback( h )handles = guidata(

52、h ) ;PauseState = get( handles.PauseTool, 'State' ) ;if strcmp( PauseState, 'on' )    returnendBoardConfig = getappdata( handles.RussiaBlock, 'BoardConfig' ) ;if isempty( BoardConfig )  Left = 'leftarrow' ;  Right = 'rightarrow' ;  Dow

53、n = 'downarrow' ;  Change = 'uparrow' ;  Drop = 'space' ;else  Left = BoardConfig.Left ;  Right = BoardConfig.Right ;  Down = BoardConfig.Down ;  Change = BoardConfig.Change ;  Drop = BoardConfig.Drop ;endCurrentKey = get( handles.RussiaBloc

54、k, 'CurrentKey' ) ;switch CurrentKey  case Left  test_MoveBlock( h, 'Left' ) ;  case Right  test_MoveBlock( h, 'Right' ) ;  case Down  test_MoveBlock( h, 'Down' ) ;  case Change  test_MoveBlock( h, 'Change' ) ;  cas

55、e Drop  test_MoveBlock( h, 'Drop' ) ;     otherwise  return ;end% -function WindowColor_Callback( h )handles = guidata( h ) ;CurrentColor = get( handles.MainAxes, 'Color' ) ;NewColor = uisetcolor( CurrentColor, '请选择窗口颜色' ) ;if length( NewColor ) = 0&#

56、160; return;else  set( handles.MainAxes, 'Color', NewColor ) ;end% -function WindowPicture_Callback( h )handles = guidata( h ) ;PictureFile, Path = uigetfile( '*.jpg; *.bmp','请选择图片' );if isnumeric( PictureFile )  return ;else  % if length( PictureFile ) > 31

57、  % errordlg( '文件名过长,读取失败' ) ;  % end  try  Picture = imread( Path, PictureFile ) ;      for num = 1: size( Picture, 3 )  ValidPicture(:, :, num) = flipud( Picture(:,:,num) ) ;  end  AxesXLim = get( handles.MainAxes, 'XLim' ) ;&#

58、160; AxesYLim = get( handles.MainAxes, 'YLim' ) ;  BlockHandle = findobj( handles.MainAxes, 'Style', 'line' ) ;  cla( BlockHandle ) ;      ImageXLimit = size(Picture, 2) ;  ImageYLimit = size(Picture, 1) ;      if diff( A

59、xesXLim ) < size(Picture, 2) | diff( AxesYLim ) < size(Picture, 1)    % 超出坐标轴范围,压缩显示  XScale = diff( AxesXLim ) / size(Picture, 2) ;  YScale = diff( AxesYLim ) / size(Picture, 1) ;  % 取较小比例压缩  Scale = min( XScale, YScale ) ;  ImageXLimit = size(Picture, 2) *

60、Scale ;  ImageYLimit = size(Picture, 1) * Scale ;  end      ImageXData(1) = AxesXLim(1) + (diff( AxesXLim ) - ImageXLimit) / 2 + 1;  ImageXData(2) = ImageXData(1) + ImageXLimit - 1;  ImageYData(1) = AxesYLim(1) + (diff( AxesYLim ) - ImageYLimit) / 2 + 1; 

61、 ImageYData(2) = ImageYData(1) + ImageYLimit - 1;      image( ValidPicture, 'Parent', handles.MainAxes, 'Hittest', 'off', .  'XData',ImageXData,'YData',ImageYData, 'Tag', 'MainImage' );  setappdata( handles.RussiaB

62、lock, 'ImageData', ValidPicture ) ;  catch  ErrorString = sprintf( '读取图片失败,错误信息为:n',lasterr ) ;  errordlg( ErrorString ) ;  endend% -function BlockColor_Callback( h )handles = guidata( h ) ;CurrentColor = getappdata( handles.RussiaBlock, 'BlockColor' ) ;if

63、 isempty( CurrentColor )  CurrentColor = GetDefaultBlockColor ;  setappdata( handles.RussiaBlock, 'BlockColor', CurrentColor ) ;endNewColor = uisetcolor( CurrentColor, '请选择方块颜色' ) ;if length( NewColor ) = 0  return;else  setappdata( handles.RussiaBlock, 'Block

64、Color', NewColor ) ;  set( handles.BlockHandle, 'MarkerFaceColor', NewColor ) ;  set( handles.TempBlock, 'MarkerFaceColor', NewColor ) ;    set( handles.NextBlock, 'MarkerFaceColor', NewColor ) ;end % -function Default_Callback( h )handles = guida

65、ta( h ) ;BlockColor = GetDefaultBlockColor ;AxesColor = GetDefaultMainAxesColor ;set( handles.MainAxes, 'Color', AxesColor ) ;set( handles.BlockHandle, 'MarkerFaceColor', BlockColor ) ;set( handles.TempBlock, 'MarkerFaceColor', BlockColor ) ;set( handles.NextBlock, 'Marke

66、rFaceColor', BlockColor ) ;% -function PauseGame_Callback( h )handles = guidata( h ) ;ToolStart = get( handles.PauseTool, 'State' ) ;if strcmp( ToolStart, 'on' )  set( handles.PauseButton, 'Value', 1 ) ;  waitfor( handles.PauseTool, 'State', 'off'

67、; ) ;else  set( handles.PauseButton, 'Value', 0 ) ;end% -function ButtonPauseGame_Callback( h )handles = guidata( h ) ;ToggleButtonValue = get( h, 'Value' ) ;if ToggleButtonValue = 0  set( h, 'Value', 1 ) ;  set( h, 'String', '继续' ) ;  set(

68、 handles.PauseTool, 'State', 'on' ) ;  waitfor( handles.PauseTool, 'State', 'off' ) ;else  set( h, 'Value', 0 ) ;  set( h, 'String', '暂停' ) ;  set( handles.PauseTool, 'State', 'off' ) ;  set( handles.Rus

69、siaBlock, 'CurrentObject', handles.MainAxes ) ;end% -function CloseFigure_Callback( h )handles = guidata( h ) ;BlockColor = getappdata( handles.RussiaBlock, 'BlockColor' ) ;MainAxesColor = get( handles.MainAxes, 'Color' ) ;MainAxesImageHandle = findobj( handles.MainAxes, 'Type', 'image' ) ;if isempt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论