getMouse 可以作用於 Python 中的一個特定對象嗎? (Can getMouse act on one specific object in Python?)


問題描述

getMouse 可以作用於 Python 中的一個特定對象嗎? (Can getMouse act on one specific object in Python?)

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.


參考解法

方法 1:

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.

(by user1946564icktoofay)

參考文件

  1. Can getMouse act on one specific object in Python? (CC BY‑SA 2.5/3.0/4.0)

#Python #python-3.3 #zelle-graphics






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論