I use a python dictionary with a coordinate string as a key to create
a mouse hash in my code. The upside is it's a constant time lookup,
the downside is it takes more memory. You would also have to update
it to track moving sprites since I use it mostly for interface stuff
like buttons and icons.
class Screen:
def __init__(self):
self.mouse_hash={}
def hashify(self, x, y):
return "%04d%04d" % (x, y)
def add_rectangle_to_mouse_hash(self, x, y, width, height, id,
align=None):
if align=='center':
x=x-width*0.5
y=y-height*0.5
for i in range(int(x), int(x+width)):
for j in range(int(y), int(y+height)):
self.mouse_hash[self.hashify(i,j)]=id
def on_mouse_press(self, x, y, button, modifiers):
id=self.mouse_hash.get(self.hashify(x,y))
if id:
#Do whatever you want with the id
-Ryan