I am trying to make a Tic Tac Toe game in Python using the Zellegraphics module. I want to use getMouse for mouse input but I am unable to figure out how to make getMouse to work on one cell of the board. I defined 9 different squares and what I want to do is that if the user clicks on one certain square, I want to draw the X or the O there. I can't figure out how to do that, because as far as I know, getMouse only acts on the whole window.

有帮助吗?

解决方案

If you have some X and Y coordinates in the window (let's call them, say, window_x and window_y) and you want to get the coordinates on a grid (let's call them grid_x, andgrid_y), and each grid cell has a width and height of grid_size, it is relatively easy to calculate grid_x and grid_y: simply integer divide (round down; in Python, that would be the // operator) window_x by grid_size to get grid_x. You can probably figure out grid_y.

A 3x3 grid is labeled with (0, 0), (1, 0), (2, 0) on the first row, (0, 1), (1, 1), and (2, 1) on the second row, and so on. The width of a cell is labeled grid_size. A particular point is labeled as a red dot. The X and Y location of the point are labeled from the top left corner of the grid.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top