I am a python beginner working on creating a 2048 game with Tkinter. So far, I have been working on getting multiple tiles to have the same movement, following the same general behavior. In order to do this, I have defined a class, Tile, that defines how each tile can move. The idea is that I can add several tile objects, and they would all move in unison, just as in 2048. However, the issue is that each tile on the Tkinter Canvas is moving at random whenever I hit an arrow key. If I kit the "" arrow key three times, it would move three tiles individually, randomly choosing the next tile to move.
How could I fix this, allowing a single press of an arrow key to move all the tiles on the canvas?
Here is the canvas I defined:
from tkinter import *
from tkinter.ttk import *
root = Tk()
w = 300
h = 300
my_canvas = Canvas(root, width=w, height=h, bg="white")
And below this code, I defined my class, such that is adds a rectangle to the canvas and defines movement in four directions
class Tile:
def __init__(self, starting_x, starting_y):
self.root = root
self.x = 0
self.y = 0
self.starting_y = starting_y
self.starting_x = starting_x
self.tile = my_canvas.create_rectangle(starting_x, starting_y, starting_x + 25, starting_y + 25,fill="black")
my_canvas.pack()
self.movement()
def movement(self):
root.bind("<Left>", self.left)
root.bind("<Right>", self.right)
root.bind("<Up>", self.up)
root.bind("<Down>", self.down)
my_canvas.move(self.tile, self.x, self.y)
self.coordinates = []
self.coordinates = my_canvas.coords(self.tile)
if self.coordinates[0] == 0: self.x = 0
if self.coordinates[2] == w: self.x = 0
if self.coordinates[1] == 0: self.y = 0
if self.coordinates[3] == h: self.y = 0
my_canvas.after(1, self.movement)
def left(self, e):
self.x = -1
self.y = 0
if self.coordinates[0] == 0:
self.x = 0
self.y = 0
def right(self, e):
self.x = 1
self.y = 0
if self.coordinates[2] == w:
self.x = 0
self.y = 0
def up(self, e):
self.x = 0
self.y = -1
if self.coordinates[1] == 0:
self.x = 0
self.y = 0
def down(self, e):
self.x = 0
self.y = 1
if self.coordinates[3] == h:
self.x = 0
self.y = 0
Finally, here is when I create three Tile objects, followed by root.mainloop():
if __name__ == "__main__":
tile1 = Tile(0,0)
tile2 = Tile(50,50)
tile3 = Tile(100,100)
root.mainloop()
As you can see, all three tiles are outputted, but they do not move in unison

Thanks in advance for the help!