当前位置:文档之家› 用matlab编写的俄罗斯方块小游戏

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

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

用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 )' );

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 ;

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', [],...

'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'} ) ;

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 ) ;

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, 'XData', TotalXData, 'YData', TotalYData ) ;

end

-

BlockNumber = getappdata( handles.RussiaBlock, 'BlockNumber' ) ;

TextString = get( handles.BlockNumText, 'String' ) ;

TextString{2} = BlockNumber + 1 ;

set( handles.BlockNumText, 'String', TextString ) ;

setappdata( handles.RussiaBlock, 'BlockNumber', BlockNumber + 1 ) ;

GameLevel = getappdata( handles.RussiaBlock, 'GameLevel' ) ;

if isempty( GameLevel )

PauseTime = 0.3 ;

else

PauseTime = 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.TempBlock, 'XData', BlockXArray, 'YData', BlockYArray ) ;

while( Status & ishandle( h ) )

if(PauseTime) ~= 0

pause( PauseTime )

end

Status = test_MoveBlock( h, 'Down' ) ;

end

end

% -------------------------------------------------------------------------

function KeyPress_Callback( h )

handles = guidata( h ) ;

PauseState = get( handles.PauseTool, 'State' ) ;

if strcmp( PauseState, 'on' )

return

end

BoardConfig = getappdata( handles.RussiaBlock, 'BoardConfig' ) ;

if isempty( BoardConfig )

Left = 'leftarrow' ;

Right = 'rightarrow' ;

Down = 'downarrow' ;

Change = 'uparrow' ;

Drop = 'space' ;

else

Left = BoardConfig.Left ;

Right = BoardConfig.Right ;

Down = BoardConfig.Down ;

-

Change = BoardConfig.Change ;

Drop = BoardConfig.Drop ;

end

CurrentKey = get( handles.RussiaBlock, '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' ) ;

case 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

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

% errordlg( '文件名过长,读取失败' ) ;

% end

try

Picture = imread( [Path, PictureFile] ) ;

for num = 1: size( Picture, 3 )

ValidPicture(:, :, num) = flipud( Picture(:,:,num) ) ;

AxesXLim = get( handles.MainAxes, 'XLim' ) ;

AxesYLim = get( handles.MainAxes, 'YLim' ) ;

BlockHandle = findobj( handles.MainAxes, 'Style', 'line' ) ;

cla( BlockHandle ) ;

ImageXLimit = size(Picture, 2) ;

ImageYLimit = size(Picture, 1) ;

if diff( AxesXLim ) < 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) * 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; ImageYData(2) = ImageYData(1) + ImageYLimit - 1;

image( ValidPicture, 'Parent', handles.MainAxes, 'Hittest', 'off', ...

'XData',ImageXData,'YData',ImageYData, 'Tag', 'MainImage' ); setappdata( handles.RussiaBlock, 'ImageData', ValidPicture ) ;

catch

ErrorString = sprintf( ['读取图片失败,错误信息为:\n',lasterr] ) ; errordlg( ErrorString ) ;

end

end

% -------------------------------------------------------------------------

function BlockColor_Callback( h )

handles = guidata( h ) ;

CurrentColor = getappdata( handles.RussiaBlock, 'BlockColor' ) ;

if isempty( CurrentColor )

CurrentColor = GetDefaultBlockColor ;

setappdata( handles.RussiaBlock, 'BlockColor', CurrentColor ) ;

end

NewColor = uisetcolor( CurrentColor, '请选择方块颜色' ) ;

if length( NewColor ) == 0

return;

setappdata( handles.RussiaBlock, 'BlockColor', NewColor ) ; set( handles.BlockHandle, 'MarkerFaceColor', NewColor ) ;

set( handles.TempBlock, 'MarkerFaceColor', NewColor ) ;

set( handles.NextBlock, 'MarkerFaceColor', NewColor ) ;

end

% ------------------------------------------------------------------------ function Default_Callback( h )

handles = guidata( h ) ;

BlockColor = GetDefaultBlockColor ;

AxesColor = GetDefaultMainAxesColor ;

set( handles.MainAxes, 'Color', AxesColor ) ;

set( handles.BlockHandle, 'MarkerFaceColor', BlockColor ) ;

set( handles.TempBlock, 'MarkerFaceColor', BlockColor ) ;

set( handles.NextBlock, 'MarkerFaceColor', 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' ) ;

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( handles.PauseTool, 'State', 'on' ) ;

waitfor( handles.PauseTool, 'State', 'off' ) ;

else

set( h, 'Value', 0 ) ;

set( h, 'String', '暂停' ) ;

set( handles.PauseTool, 'State', 'off' ) ;

set( handles.RussiaBlock, '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 ~isempty( MainAxesImageHandle )

MainAxesImage.Property.Tag = get( MainAxesImageHandle, 'Tag' );

MainAxesImage.Property.Hittest = get( MainAxesImageHandle, 'Hittest' );

MainAxesImage.Property.XData = get( MainAxesImageHandle, 'XData' );

MainAxesImage.Property.YData = get( MainAxesImageHandle, 'YData' );

MainAxesImage.ImageData = getappdata( handles.RussiaBlock, 'ImageData' ) ;

else

MainAxesImage.ImageData = [] ;

end

save ColorInfo.mat BlockColor MainAxesColor MainAxesImage

delete( handles.RussiaBlock ) ;

% -------------------------------------------------------------------------

function Color = GetDefaultBlockColor

Color = [0 0 1] ;

% -------------------------------------------------------------------------

function Color = GetDefaultMainAxesColor

Color = [1 1 1] ;

% ----------------------------------------------------------------

function [BlockXArray, BlockYArray] = Com_GetBlock( varargin )

global BlockIndex ;

BlockXArray = [] ;

BlockYArray = [] ;

handles = guidata( varargin{1} ) ;

if nargin == 1

BlockArray = getappdata( handles.RussiaBlock, 'BlockArray' ) ;

BlockIndex = ceil( rand(1) * 24 ) ;

else % nargin == 2

BlockIndex = varargin{2} ;

end

switch(BlockIndex)

case {1,2,3,4} % 方块

BlockXArray = [0;0;1;1] * 20 - 10 ;

BlockYArray = [0;1;1;0] * 20 - 10 ;

case {5,6} % 竖长条

BlockXArray = [0;0;0;0] * 20 - 10 ;

case {7,8} % 横长条BlockXArray = [-1;0;1;2] * 20 - 10 ; BlockYArray = [1;1;1;1] * 20 - 10 ; case {9} % 4类T T1 BlockXArray = [-1;0;1;0] * 20 - 10 ; BlockYArray = [1;1;1;0] * 20 - 10 ; case {10} % T2

BlockXArray = [0;0;1;0] * 20 - 10 ; BlockYArray = [2;1;1;0] * 20 - 10 ; case {11} % T3

BlockXArray = [0;0;1;-1] * 20 - 10 ; BlockYArray = [2;1;1;1] * 20 - 10 ; case {12} % T4

BlockXArray = [0;0;0;-1] * 20 - 10 ; BlockYArray = [2;1;0;1] * 20 - 10 ; case {13} % 8类L L1 BlockXArray = [0;0;0;1] * 20 - 10 ; BlockYArray = [1;0;-1;-1] * 20 - 10 ; case {14} % L2

BlockXArray = [-1;0;1;1] * 20 - 10 ; BlockYArray = [0;0;0;1] * 20 - 10 ; case {15} % L3

BlockXArray = [-1;0;0;0] * 20 - 10 ; BlockYArray = [1;1;0;-1] * 20 - 10 ; case {16} % L4

BlockXArray = [-1;-1;0;1] * 20 - 10 ; BlockYArray = [-1;0;0;0] * 20 - 10 ; case {17} % L5

BlockXArray = [-1;0;0;0] * 20 - 10 ; BlockYArray = [-1;-1;0;1] * 20 - 10 ; case {18} % L6

BlockXArray = [-1;-1;0;1] * 20 - 10 ; BlockYArray = [1;0;0;0] * 20 - 10 ; case {19} % L7

BlockXArray = [0;0;0;1] * 20 - 10 ; BlockYArray = [-1;0;1;1] * 20 - 10 ; case {20} % L8

BlockXArray = [-1;0;1;1] * 20 - 10 ; BlockYArray = [0;0;0;-1] * 20 - 10 ; case {21 22} % 4类Z Z1 BlockXArray = [-1;0;0;1] * 20 - 10 ; BlockYArray = [1;1;0;0] * 20 - 10 ; case {23 24} % Z2

BlockYArray = [-1;0;0;1] * 20 - 10 ;

case {25 26} % Z3

BlockXArray = [-1;0;0;1] * 20 - 10 ;

BlockYArray = [0;0;1;1] * 20 - 10 ;

case {27 28} % Z4

BlockXArray = [0;0;1;1] * 20 - 10 ;

BlockYArray = [1;0;0;-1] * 20 - 10 ;

end

if nargin == 1

NewBlockArray.BlockXArray = BlockXArray ;

NewBlockArray.BlockYArray = BlockYArray ;

NewBlockArray.BlockIndex = BlockIndex ;

NextAxesXLim = get( handles.NextBlockAxes, 'XLim' ) ;

NextAxesYLim = get( handles.NextBlockAxes, 'YLim' ) ;

set( handles.NextBlock, 'XData', [BlockXArray + 0.5 * diff( NextAxesXLim ) -

ceil( sum( BlockXArray ) / 4 ) ],...

'YData', [BlockYArray + 0.5 * diff( NextAxesYLim )] - ceil( sum( BlockYArray ) / 4 ) ) ; setappdata( handles.RussiaBlock, 'BlockArray', NewBlockArray ) ;

if isempty( BlockArray )

Com_GetBlock( varargin{1} ) ;

else

BlockXArray = BlockArray.BlockXArray ;

BlockYArray = BlockArray.BlockYArray ;

BlockIndex = BlockArray.BlockIndex ;

end

end

AxesXLim = getappdata( handles.RussiaBlock, 'XLim' ) ;

AxesYLim = getappdata( handles.RussiaBlock, 'YLim' ) ;

BlockXArray = BlockXArray + 0.5 * diff( AxesXLim ) ;

BlockYArray = BlockYArray + diff( AxesYLim ) ;

% -------------------------------------------------------------------------

function Status = test_MoveBlock( h, MoveMode )

Status = 1;

if ~ishandle( h )

return

end

handles = guidata( h ) ;

TempXData = get( handles.TempBlock, 'XData' ) ;

TempYData = get( handles.TempBlock, 'YData' ) ;

TempXData = TempXData';

TempYData = TempYData' ;

-

TotalXData = get( handles.BlockHandle, 'XData' ) ;

TotalYData = get( handles.BlockHandle, 'YData' ) ;

TotalXData = TotalXData' ;

TotalYData = TotalYData' ;

TempBlockPos = getappdata( handles.RussiaBlock, 'TempBlockPos' ) ;

if isempty( TempBlockPos )

return

end

AxesXLim = getappdata( handles.RussiaBlock, 'XLim' ) ;

AxesYLim = getappdata( handles.RussiaBlock, 'YLim' ) ;

switch MoveMode

case 'Left'

if any( TempXData - 20 < AxesXLim(1) )

return

end

TestArray = ismember( [TempXData - 20, TempYData], [TotalXData, TotalYData],

'rows' ) ;

if any( TestArray )

return;

else

set( handles.TempBlock, 'XData', TempXData - 20 ) ;

TempBlockPos.LeftStep = TempBlockPos.LeftStep + 1 ;

setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ;

end

case 'Right'

if any( TempXData + 20 > AxesXLim(2) )

return

end

TestArray = ismember( [TempXData + 20, TempYData], [TotalXData, TotalYData],

'rows' ) ;

if any( TestArray )

return;

else

set( handles.TempBlock, 'XData', TempXData + 20 ) ;

TempBlockPos.LeftStep = TempBlockPos.LeftStep - 1 ;

setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ;

end

case 'Down'

if any( TempYData - 20 < AxesYLim(1) )

set( handles.BlockHandle, 'XData', [TotalXData; TempXData],...

'YData', [TotalYData; TempYData] ) ;

Status = 0 ;

return

-

end

TestArray = ismember( [TempXData, TempYData - 20], [TotalXData, TotalYData],

'rows' ) ;

if any( TestArray )

set( handles.BlockHandle, 'XData', [TotalXData; TempXData],...

'YData', [TotalYData; TempYData] ) ;

Status = 0 ;

else

set( handles.TempBlock, 'YData', TempYData - 20 ) ;

TempBlockPos.DownStep = TempBlockPos.DownStep + 1 ;

setappdata( handles.RussiaBlock, 'TempBlockPos', TempBlockPos ) ;

end

case 'Drop'

global PauseTime

PauseTime = 0 ;

case 'Change'

global BlockIndex

OldBlockIndex = BlockIndex ;

switch BlockIndex

case {1,2,3,4}

return;

case {5,6}

NewIndex = 7 ;

case {7,8}

NewIndex = 5 ;

case {9,10,11,12}

NewIndex = mod( OldBlockIndex, 4 ) + 9;

case {13,14,15,16}

NewIndex = mod( OldBlockIndex, 4 ) + 13;

case {17,18,19,20}

NewIndex = mod( OldBlockIndex, 4 ) + 17;

case {21,22}

NewIndex = 23;

case {23,24}

NewIndex = 21;

case {25,26}

NewIndex = 27 ;

case {27,28}

NewIndex = 25 ;

end

[BlockXArray, BlockYArray] = Com_GetBlock( h, NewIndex ) ;

NewTempXData = BlockXArray - TempBlockPos.LeftStep * 20 ;

NewTempYData = BlockYArray - TempBlockPos.DownStep * 20 ;

if any( NewTempXData < AxesXLim(1) ) | any( NewTempXData > AxesXLim(2) ) |...

any( NewTempYData < AxesYLim(1) )

BlockIndex = OldBlockIndex ;

return;

end

TestArray = ismember( [NewTempXData, NewTempYData], [TotalXData, TotalYData],

'rows' ) ;

if any( TestArray )

BlockIndex = OldBlockIndex ;

else

BlockIndex = NewIndex ;

set( handles.TempBlock, 'XData', NewTempXData, 'YData', NewTempYData ) ;

end

end

% -------------------------------------------------------------------------

function UpdateGameLevel( FigureHandle, Score ) ;

GameLevel = ceil( Score / 10000 ) ;

if GameLevel > 9

GameLevel = 9 ;

end

setappdata( FigureHandle, 'GameLevel', GameLevel ) ;

% -------------------------------------------------------------------------

function GameLevel_Callback( h )

handles = guidata( h ) ;

ScoreFigure = figure( 'name', '游戏难度设置', 'menubar', 'none', 'numbertitle', 'off',...

'pos', [200 200 200 180], 'windowstyle', 'modal', 'resize', 'off' ) ;

uicontrol( 'Parent', ScoreFigure, 'Style', 'listbox', 'Pos', [20 20 80 140],...

'String', [sprintf( '%d 级|', 1:8 ),'9 级'] ) ;

uicontrol( 'Parent', ScoreFigure, 'Style', 'pushbutton', 'Pos', [120 100 60 50], 'String', '确定',...

'Cal', ['GameLevel = get( findobj( ''Style'', ''listbox''), ''Value'');',...

'set( 0, ''Userdata'', GameLevel ); close( gcf );'] ) ;

uicontrol( 'Parent', ScoreFigure, 'Style', 'pushbutton', 'Pos', [120 30 60 50], 'String', '取消',... 'Cal', 'close( gcf )' ) ;

waitfor( ScoreFigure ) ;

ButtonAction = questdlg( '重新设置游戏难度将初始化界面,是否继续?', '设置确认', '是的', '取消', '取消' ) ;

if strcmp( '取消', ButtonAction )

return

end

GameLevel = get( 0, 'Userdata' ) ;

setappdata( handles.RussiaBlock, 'GameLevel', GameLevel ) ;

相关主题
相关文档 最新文档