Анимация укладки строк Python TkinterPython

Программы на Python
Anonymous
Анимация укладки строк Python Tkinter

Сообщение Anonymous »

Я хочу создать анимацию, в которой ряд объектов перемещается вниз по экрану, пока не достигнет указанной строки, а затем генерировать следующую строку, которая перемещается вниз из той же начальной позиции и останавливает строку над предыдущей строкой. Так что это похоже на сложную анимацию для игры в игровой автомат. Как бы я это сделал, потому что прямо сейчас с этим кодом все строки останавливаются внизу и перекрывают друг друга.
Код:
class SpinSlot:
def init(self, new, сетка):
self.new = новый
self.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))
класс 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)
Я пробовал изменить смещение и начать с заданного значения current_offset, но ничего не помогло.

Подробнее здесь: https://stackoverflow.com/questions/790 ... -animation

Вернуться в «Python»