我正在使用学校项目指南在 MATLAB 中创建游戏,并且遇到了实现按钮的路障。我知道解决方案很简单,但我没有得到它......
这里是游戏的前提(它基于GBA游戏“Advance Wars”):我使用一个10x15的小方轴网格来显示“地图”。每个轴都加载了一个基于地图地形的图像,占据空间的单元,以及窗格是否被“光标”突出显示(我已经为每种可能的情况创建了一个图像,所有脚本需要做的是显示正确的图像)。
我已经成功制作了一个更改指定轴图像的功能。使用此功能,我可以通过将坐标轴图像更改为其突出显示的变体、暂停然后将其更改回其原始图像来使光标在指定坐标轴中“闪烁”。
用户应该能够通过单击四个按钮之一将光标移动到相邻的轴:“左”、“右”、“上”和“下”。
不幸的是,这是我遇到问题的地方。即使我按下按钮,光标也只会在同一位置闪烁。
如果有人可以帮助我将非常感激!
这是我的 GUI OpeningFcn 中的脚本,在我初始化每个轴中的图像之后:
% Begin Game
% Values of important game variables initialized:
% Player 1 has the first turn
Turn = 'P1';
% Cursor location initialized to upper left hand corner
handles.C_Loc = [1,1];
% Game set initially to continue
handles.game_cont = 1;
% Player turn initially set to continue
handles.turn_cont = 1;
%
% Cursor movement initially set to zero
handles.C_Move_H = 0;
handles.C_Move_V = 0;
%
% Until the game conditions are met, the following loop will execute:
while (handles.game_cont == 1)
% Reset cursor location based on which player's turn it is
switch Turn
case ('P1')
handles.C_Loc = [7,3];
case ('P2')
handles.C_Loc = [3,13];
end
% Until the player ends their turn, the following loop will execute:
while (handles.turn_cont == 1)
% Cursor location should be changed based on button pushes:
handles.C_Loc(1) = handles.C_Loc(1) + handles.C_Move_V;
handles.C_Loc(2) = handles.C_Loc(2) + handles.C_Move_H;
% Movement should be reset to avoid continuous movement:
handles.C_Move_V = 0;
handles.C_Move_H = 0;
% Current map is a cell array with the same dimensions as map.
% Each cell holds a string used to decide which image to load.
Old_Im = Current_Map{handles.C_Loc(1),handles.C_Loc(2)};
% Highlighted image is designated by adding "H" to string
New_Im = strcat(Old_Im,'H');
% User-defined function changes axes to highlighted image
Image_Change(handles.C_Loc(1),handles.C_Loc(2), New_Im, handles)
pause(0.75)
% User-defined function changes axes back to original image
Image_Change(handles.C_Loc(1),handles.C_Loc(2), Old_Im, handles)
end
switch Turn
case ('P1')
Turn = 'P2';
case ('P2')
Turn = 'P1';
end
end
end
这是我的按钮回调之一(用于“向上”按钮):
handles.C_Move_V = 1;
guidata(hObject, handles);
我究竟做错了什么?!请帮忙...