1

I am creating a game in MATLAB using guide for a school project and am hitting a road block implementing pushbuttons. I know the solution is simple but I am not getting it...

Here is the premise of the game (It is based on the GBA game "Advance Wars"): I am using a 10x15 grid of small square axes to display the "Map". Each axes is loaded with an image based on the map terrain, the unit occupying the space, and whether or not the pane is highlighted by the "cursor" (I have created an image for each possible situation and all the script needs to do is display the correct image).

I have successfully made a function which changes the image of a specified axes. Using this function I can make a cursor "blink" in a specified axes by changing that axes image to its highlighted variation, pausing, and then changing it back to its original image.

The user should be able to move the cursor to an adjacent axes by clicking on one of four buttons: "Left", "Right", "Up", and "Down".

Unfortunately this is where i am having problems. The cursor just keeps blinking in the same position even though i am pressing the buttons.

If someone could help I would be extremely grateful!

Here is the script in my GUI OpeningFcn, after I have initialized the image in each axes:

% 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

Here is one of my pushbutton callbacks(for the "UP" button):

handles.C_Move_V = 1;
guidata(hObject, handles);

WHAT AM I DOING WRONG?! Please help...

1 Answers1

0

coming late to the party:

you can try:

drawnow

if this doesn't help, you can try adding

guidata(hObject, handles);

...although I guess, that you will never see this :(. I hope sometimes this will help someone else...

Lucius II.
  • 1,832
  • 12
  • 19