[Python] Python Tkinter Row Stacking Animation

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I want to make an animation where a row of objects moves down the screen until they reach a specified row and then generate the next row that moves down from the same starting position and stops a row above the previous row. So it makes like a stacking animation for a slot machine game. How would I do this because right now with this code, all the rows are stopping at the bottom and overlapping each other.

    Code: class SpinSlot: def init(self, new, grid): self.new = new self.grid = grid

    def spin(self):
    self.drop_row(0)

    def drop_row(self, row):
    if row < 7:
    for c in range(7):
    # Create a dropping effect by placing images row by row
    randomNum = random.randint(1, 490000)
    for key in image_mapping.keys():
    if randomNum in key:
    file = image_mapping[key]
    break
    # Initially place the image off-screen
    self.grid.place_object(row=row, col=c, obj_type="image", image_file=file, offset_y=-100)

    # Move the row down step by step
    self.move_row_down(row, -100)

    def move_row_down(self, row, current_offset):
    # Calculate the target position for the row
    final_pos = self.grid.cell_size * (6 - row) # Each row stacks above the previous one
    step_size = 10
    if current_offset < final_pos:
    for c in range(7):
    # Move each image in the row down by one step
    self.grid.move_object(row=row, col=c, offset_y=current_offset + step_size)

    # Schedule the next step
    self.new.after(50, lambda: self.move_row_down(row, current_offset + step_size))
    else:
    # Ensure the row stops exactly at its final position
    for c in range(7):
    self.grid.move_object(row=row, col=c, offset_y=final_pos)
    # Drop the next row after a short delay
    self.new.after(100, lambda: self.drop_row(row + 1))


    class GridWrapper: def init(self, canvas, rows, columns, cell_size, images_ref): self.canvas = canvas self.rows = rows self.columns = columns self.cell_size = cell_size self.images_ref = images_ref self.create_grid()

    def create_grid(self):
    # Draw vertical lines
    for col in range(0, self.columns):
    x = col * self.cell_size
    line = self.canvas.create_line(x, 0, x, self.rows * self.cell_size, fill="black")
    self.grid_lines.append(line)

    def clear_grid(self):
    # Clear all objects from the canvas
    self.canvas.delete("all")
    self.images_ref.clear()

    def place_object(self, row, col, obj_type="image", image_file=None, offset_y=0):
    global images_ref

    # Calculate the top-left corner of the grid cell
    x1 = col * self.cell_size
    y1 = offset_y
    x_center = x1 + self.cell_size // 2
    y_center = y1 + self.cell_size // 2

    if obj_type == "image" and image_file:
    img = Image.open(image_file)
    img = img.resize((self.cell_size - 10, self.cell_size - 20), Image.LANCZOS)
    photo = ImageTk.PhotoImage(img)
    item_id = self.canvas.create_image(x_center, y_center, image=photo)
    images_ref[(row, col)] = (photo, item_id) # Prevent garbage collection

    def move_object(self, row, col, offset_y):
    global images_ref
    photo, item_id = images_ref[(row, col)]

    x1 = col * self.cell_size
    y1 = offset_y # Adjust y1 based on offset_y for smoother animation
    x_center = x1 + self.cell_size // 2
    y_center = y1 + self.cell_size // 2

    self.canvas.coords(item_id, x_center, y_center)


    I tried changing the offset and starting with a set current_offset but nothing is working.

    Continue reading...

Compartilhe esta Página