Effacer les filtres
Effacer les filtres

How could I code that if the user pushes “Q” then an X will be displayed the the coordinates (1,3)?

1 vue (au cours des 30 derniers jours)
I am creating a game of Tic Tac Toe and after someone suggested I use the ginput function, I found [x,y,button]=ginput(n). How could I tell Matlab that if a user pressed the “Q” button of the keyboard a text “X” or “O” would appear at a specific (x,y) coordinate?

Réponse acceptée

Geoff Hayes
Geoff Hayes le 14 Mar 2021
Eryn - you can use the following code to detect whether the Q (or q) button of the keyboard has been pressed
function KeyboardListenerExample
hFig = figure;
set(hFig,'KeyPressFcn',@OnKeyPressFcn);
function OnKeyPressFcn(hObject, eventdata)
if lower(eventdata.Character) == 'q'
fprintf('The q character has been pressed!\n');
end
end
end
but is that really what you want to do? Are you drawing an X or an O within the bounds of a square (one of the nine squares of the tic-tac-toe board), or just using a text object to write the X or O? You may want to detect, using the ButtonDownFcn where the user has pressed within your axes and then decide what to draw.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by