Capturing Mouse Clicks on an array/patch

조회 수: 10 (최근 30일)
Sarah Rasavanh
Sarah Rasavanh 2019년 12월 9일
댓글: Chris 2022년 11월 28일
clear
clc
[board, DrawRes, N, Theta, onerow, playerturn, FigSize] = setboard();
[Xline, Yline, BoardCenterX, BoardCenterY, radius] = Grid(onerow, FigSize)
[BoardColor] = DrawPiece(onerow, BoardCenterX, BoardCenterY, Theta, radius, board);
for i = 1:60
clc
disp(board)
while playerturn > 0
fprintf('Player %i turn\n', playerturn)
[x1,y1] = inputmoves();
if board(y1,x1) == 0
board(y1,x1) = playerturn;
playerturn = switchplayers(playerturn);
[BoardColor] = DrawPiece(onerow, BoardCenterX, BoardCenterY, Theta, radius, board);
end
end
end
I'm trying to make a game of tictactoe, and I have it set up where there are graphics and that there is a patch array which stores each place a player can go. my problem is that I don't know how to make it so when I click on the patch area it finds the patch coordinate and matches it with the one in the main array (in this case board) where the code then recognizes it as part of the board/ graphics. Right now I have it where the user inputs coordinates into the command window and then it goes to the board array but I want to be able to do this by clicking on the space I want.
  댓글 수: 2
Ruger28
Ruger28 2019년 12월 9일
Not an answer, but I have done similar things to this using uicontrol pushbuttons as an arrary. I actually just made the old game Snake using this method. You can determine which pushbutton was pressed, and get row/column coordinates via this method.
Ruger28
Ruger28 2019년 12월 9일
Also worth noting, that groot could be of use capturing your mouse location
GROOT = groot;
sprintf('Current Mouse Location = [%f, %f]',GROOT.PointerLocation(1),GROOT.PointerLocation(2));
Be aware of the units GROOT is in. Not sure if this helps, but it is something.

댓글을 달려면 로그인하십시오.

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 4월 23일
Sarah - very late to this, but you can use hittest to capture mouse clicks on graphics objects (in your case, the nine different patches). For example, the following code for a 3x3 grid changes the patch a different colour whenever pressed (red or blue for player 1 or player 2 respectively):
function TicTacToeExample
hFigure = figure;
hAxes = axes;
axis equal;
axis off;
hold on;
squareEdgeSize = 5;
% create the board of patch objects
hPatchObjects = zeros(3,3);
for j = 3:-1:1
for k = 1:3
hPatchObjects(3 - j+ 1, k) = rectangle('Position', [k*squareEdgeSize,j*squareEdgeSize,squareEdgeSize,squareEdgeSize], 'FaceColor', [0 0.5 0.5],...
'EdgeColor', 'k', 'LineWidth', 3, 'HitTest', 'on', 'ButtonDownFcn', {@OnPatchPressedCallback, 3 - j+ 1, k});
end
end
gameBoard = zeros(3,3);
atPlayer = 1;
playerColours = [1 0 0; 0 0 1];
xlim([squareEdgeSize 4*squareEdgeSize]);
ylim([squareEdgeSize 4*squareEdgeSize]);
function OnPatchPressedCallback(hObject, eventdata, rowIndex, colIndex)
% remove callback so square can't be chosen again
set(hObject, 'ButtonDownFcn', []);
% change FaceColor to player colour
set(hObject, 'FaceColor', playerColours(atPlayer, :));
% update the game board
gameBoard(rowIndex,colIndex) = atPlayer;
end
Each patch is assigned the OnPatchPressedCallback callback function with the only difference being the row and column index (for the patch) into the game board. Once any player has selected a square (patch) then this callback fires and we change its colour, update the game board, and remove the callback from the patch object (so that no player can choose it again).
  댓글 수: 1
Chris
Chris 2022년 11월 28일
Hello there,
Is there a way to retrieve the Board array once the user is done activating cells outside the function? I can't find a way to do this, even with a global statement.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by