Python find closest turtle via mouse click
我正在使用基于海turtle的网格设置创建一个扫雷风格的游戏。我需要在网格中找到最近的单元格并显示位于其下方的图标,无论是炸弹还是数字图标。我不想让它准确,我只需要单击鼠标即可找到网格中最近的单元格,即使单击不是直接在板上。目前我的代码只显示了在板上创建的最后一个海turtle的图标,然后通过进一步点击什么也不做。
我该怎么做才能让它识别真正最近的点击并多次执行直到找到最后一个炸弹?
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import random import turtle import cell class Game: def __init__(self, size): registershapes() def probe(self, x, y): else: def registershapes(): |
我相信您以错误的方式处理此问题。您正在激活
下面是我将您的代码从这个问题和您之前的问题重新编写成一个您可以运行的示例。我不得不猜测 Cell 类的定义:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
from turtle import Turtle, Screen import random class Cell(Turtle): def __init__(self, number): def number(self): class Game: def __init__(self, size): offset = (size – 1) * 17 for y in range(size): def hideMines(self, num): self.__bombnum = num while i < self.__bombnum: if currentnum not in self.__rnums: def probe(self, closest): if closest.number() in self.__rnums: self.__probe += 1 def registershapes(): def closure(closest): if rem == 0: over = screen.textinput("Text Input","Would you like to play again? (Y)es or (N)o") if over.upper() == ‘Y’: def main(): board = screen.numinput("Numeric Input","Enter desired board size:") nummine = screen.numinput("Numeric Input","Enter desired number of mines:") screen = Screen() mine = None screen.mainloop() |