I have many overlapping shapes representing irrelevant background items on a canvas. I also have a pattern of non-overlapping circles, each of which is a "hole". Each "hole" sprite (circle) has an associated "hole" object, though never explicitly in the code. (side note: I would love to have a logical association between model and view with these objects, but haven't found a smart way to do that). Each "hole" is different, and has different effects.
There is a small circular "ball" which can be dragged into any "hole". I found how to drag and drop from this question. I need to find which hole the ball went into.
The best way I have found to do that so far is to:
create a dict mapping the coordinates of the center of the hole sprite to the hole object
tag each hole like this:
t=("hole", "hole_at_{}_{}".format(x, y))
on releasing the ball, do this:
def on_ball_release(self, event): '''Process button event when user releases mouse holding ball.'''
# use small invisible rectangle and find all overlapping items items = self._canvas.find_overlapping(event.x - 10, event.y - 10, event.x + 10, event.y + 10) for item in items: # there should only be 1 overlapping hole if "hole" in self._canvas.gettags(item): # get the coordinates from the tag coords = tuple([int(i) for i in self._canvas.gettags(item)[1].replace("hole_at_", "").split("_")]) # get associated object using dictionary established before hole = self._hole_dict[coords] hole.process_ball() return
That seems very messy. I feel there should be some smarter way to do this.