How it works...

The modification to the __init__ method is similar to the one in the Finding items by their position recipe, so you can review it in case you have any doubts and skip directly to the changes in the process_movements() method.

Before we calculate any overlap, the fill color of all the canvas items, except the one that can be controlled by the user, is changed to green. These item's identifiers are retrieved by the canvas.find_all() method:

    def process_movements(self):
all_items = self.canvas.find_all()
for item in filter(lambda i: i != self.item, all_items):
self.canvas.itemconfig(item, fill="green")

Now that the item colors are reset, we call canvas.find_overlapping() to get all the items that are currently colliding with the moving item. Again, the item controlled by the user is excluded from the loop, and the color of the rest of the overlapping items (if any) is changed to yellow:

    def process_movements(self):
# ...

x0, y0, x1, y1 = self.canvas.coords(self.item)
items = self.canvas.find_overlapping(x0, y0, x1, y1)
for item in filter(lambda i: i != self.item, items):
self.canvas.itemconfig(item, fill="yellow")

The method continues its execution by moving the blue rectangle by the calculated offset, and scheduling process_movements() itself again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset