Credit: Problem by Julia Lee, Kerem Goksel, and Chris Piech
Handouts: Graphics Reference
File: catch_me_if_you_can.py
When the mouse touches the "sneaky" square, it moves.
How can we tell if the mouse is touching the sneaky square? There is a function you can use called find_overlapping
on the canvas. It gives us back a list of all objects on the canvas that overlap with a given square, which we can iterate over. For example:
overlapping_objects = canvas.find_overlapping(x1, y1, x2, y2)
for overlapping_object in overlapping_objects:
# do something with overlapping_object ...
The coordinates provided to find_overlapping
are the top-left corner and bottom-right corner of the box you want to get all overlapping objects for. You can also specify a single point, like this:
overlapping_objects = canvas.find_overlapping(x, y, x, y)
This will get the objects overlapping with the point (x, y)
.