I handle mouse clicks on objects depending on the location of the object on the screen. I record the xy coordinate of the mouse click and see if it matches any of the objects that are allowed to click on. The objects are in different lists or only in single objects, but I want them to be in one big list, so I can just skip this whole thing once, if the click is on one of the objects, do the work, take a break. You can click only one object.
First method: how am I doing it now:
list = [obj1, obj2, obj3] singleobj copylist = list copylist.append(singleobj) for item in copylist: if item.pos == mouseclick.pos: doWork(item) break
Second method: I would prefer to do something like below, but obviously list+singleobj
not valid:
for item in list+singleobj: if item.pos == mouseclick.pos: doWork(item) break
Third method: or, if I absolutely need it, I could make this terrible horrible code:
list = [obj1, obj2, obj3] foundobj = None for item in list: if item.pos == mouseclick.pos: foundobj = item break if foundobj is None: if singleobj.pos == mouseclick.pos: foundobj = singleobj
The first method seems slow because I need to copy all (possibly many) lists and individual objects into one list.
The second method seems ideal, because it is compact and easy to maintain. Although, as of now, this is just pseudo-code.
The third method is cumbersome and awkward.
Which method to use? If the second, can you provide the actual code?