Код:
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))
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)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -animation