Reputation: 1857
Using Tkinter in Python 3.6.0, is there a 'good' way to detect if a specific object, in this case my player sprite, is overlapping any other object with a specific tag?
I would want to do something like:
if canvas.find_overlapping( *canvas.coords(player) ) == (player, "item_tag"):
return True
where item_tag
is the tag applied to 100+ objects on the canvas
.
I can use canvas.find_withtag("item_tag")
to return the object ID of all the objects I wish the player to interact with, but I can't get this to work within find_overlapping
, e.g. take each object ID and let find_overlapping
detect if it is overlapping.
Thanks!
Upvotes: 2
Views: 1341
Reputation: 310
Considering both are lists, all you have to do is take each element of one and check if it is in the other:
tagged_objects = canvas.find_withtag("item_tag")
overlapping_objects = canvas.find_overlapping(*canvas.coords(player))
for item in overlapping_objects:
if (item in tagged_objects):
return True
You could do this in reverse and look to see if each element of tagged_objects
is in overlapping_objects
, but considering you said there are 100+ tagged objects, that would probably be slower.
Upvotes: 1