|
11 | 11 | import sys |
12 | 12 | import concurrent.futures |
13 | 13 | import threading |
| 14 | +import time |
14 | 15 |
|
15 | 16 | # Constants |
16 | 17 | STEAM_PATH = os.path.expanduser(r"C:\Program Files (x86)\Steam") |
@@ -146,7 +147,7 @@ def __init__(self, root, installed_games, drives): |
146 | 147 |
|
147 | 148 | # Set initial animation speed |
148 | 149 | self.animation_speed = 200 # Controls the distance moved per frame |
149 | | - self.frame_delay = 10 # Controls the speed of the animation (time between frames) |
| 150 | + self.frame_delay = 16 # Controls the speed of the animation (time between frames) |
150 | 151 |
|
151 | 152 | self.root.title("Steam Roulette") |
152 | 153 | self.root.geometry("600x700") |
@@ -195,6 +196,7 @@ def __init__(self, root, installed_games, drives): |
195 | 196 | self.canvas = tk.Canvas(root, width=600, height=300, bg="black") |
196 | 197 | self.canvas.pack(pady=1) |
197 | 198 |
|
| 199 | + |
198 | 200 | # Create a frame to contain the game name label and other elements |
199 | 201 | utility_frame = tk.Frame(self.root, bg=self.light_mode_bg) |
200 | 202 | utility_frame.pack(pady=5) |
@@ -223,7 +225,7 @@ def __init__(self, root, installed_games, drives): |
223 | 225 | self.preloaded_images = {} |
224 | 226 | self.preload_images() |
225 | 227 |
|
226 | | - # Use resource_path to get the correct image path |
| 228 | + # Use resource_path to get the correct logo path |
227 | 229 | logo_path = resource_path("SteamRouletteLogo.png") |
228 | 230 | try: |
229 | 231 | logo_image = Image.open(logo_path) |
@@ -385,42 +387,61 @@ def cycle_images(self): |
385 | 387 | def animate_images(self): |
386 | 388 | """Animate the images sliding across the canvas.""" |
387 | 389 | canvas_width = self.canvas.winfo_width() # Get the width of the canvas |
388 | | - canvas_center = canvas_width / 2 # Center of the canvas |
| 390 | + |
| 391 | + # Calculate total distance for animation |
| 392 | + total_distance = len(self.active_images) * canvas_width |
| 393 | + |
| 394 | + # Set the desired duration (in milliseconds) |
| 395 | + desired_duration = 10000 # 10 seconds |
| 396 | + |
| 397 | + # Calculate the frame delay or speed dynamically |
| 398 | + self.frame_delay = 16 # Default frame delay in ms (60 FPS) |
| 399 | + frames = desired_duration // self.frame_delay # Total number of frames |
| 400 | + self.animation_speed = max(1, total_distance // frames) # Pixels per frame |
389 | 401 |
|
390 | 402 | def slide(): |
391 | 403 | # Move all images to the left |
392 | 404 | for image_item, img_tk in self.active_images: |
393 | 405 | self.canvas.move(image_item, -self.animation_speed, 0) # Move images to the left |
394 | 406 |
|
395 | | - # Find the three-quarters image by index |
396 | | - three_quarter_index = (len(self.active_images) * 3) // 4 # Three-quarters index |
397 | | - three_quarter_image = self.active_images[three_quarter_index] # Get the three-quarters image |
398 | | - three_quarter_image_x = self.canvas.coords(three_quarter_image[0])[0] # x-coordinate of the three-quarters image |
| 407 | + # Check if the current image is in the last 10 images |
| 408 | + for i, (image_item, img_tk) in enumerate(self.active_images[-5:]): # Last 5 images |
| 409 | + image_x = self.canvas.coords(image_item)[0] # x-coordinate of the image |
| 410 | + img_width = self.canvas.bbox(image_item)[2] - self.canvas.bbox(image_item)[0] # Image width |
| 411 | + image_left_x = image_x - img_width / 2 # Left edge of the image |
399 | 412 |
|
400 | | - # Slowing down effect: Gradually reduce the speed after the median image's left edge reaches the left side of the window (x = 0) |
401 | | - img_width = self.canvas.bbox(three_quarter_image[0])[2] - self.canvas.bbox(three_quarter_image[0])[0] # Image width |
402 | | - threequarter_image_left_x = three_quarter_image_x - img_width / 2 # Left edge of the three quarter index image |
403 | | - |
404 | | - if threequarter_image_left_x <= 0: # Once the three quarter index image's left edge reaches or crosses the left side of the canvas |
405 | | - self.animation_speed = max(5, self.animation_speed * 0.98) # Gradually slow down the speed |
| 413 | + # If the image is within the last 10 images, start slowing down |
| 414 | + if image_left_x <= 0: # If the image is near or at the left edge |
| 415 | + self.animation_speed = max(5, self.animation_speed * 0.98) # Gradually slow down the speed |
406 | 416 |
|
407 | 417 | # Get the x-coordinate of the top-left corner of the last image |
408 | | - last_image_x = self.canvas.coords(self.active_images[-1][0])[0] # x-coordinate of the top-left corner of the last image |
| 418 | + last_image_x = self.canvas.coords(self.active_images[-1][0])[0] # x-coordinate of the last image |
409 | 419 | img_width = self.canvas.bbox(self.active_images[-1][0])[2] - self.canvas.bbox(self.active_images[-1][0])[0] # Image width |
410 | | - |
411 | | - # Calculate the x-coordinate of the left side of the last image |
412 | 420 | last_image_left_x = last_image_x - img_width / 2 # Left edge of the last image |
413 | 421 |
|
414 | 422 | # Stop the animation when the last image's left edge reaches the left edge of the canvas (x = 0) |
415 | | - if last_image_left_x <= 0: # Last image's left edge reaches the left side of the canvas |
| 423 | + if last_image_left_x <= 0: # Last image reaches the left side |
416 | 424 | self.display_selected_game() # Display selected game image |
| 425 | + print(f"Final spinning duration: {time.time() - self.start_time:.2f} seconds") |
417 | 426 | return # End the animation |
418 | 427 |
|
419 | 428 | # Continue moving the images if they haven't reached the left edge |
420 | 429 | self.animation_id = self.root.after(self.frame_delay, slide) |
421 | 430 |
|
| 431 | + self.start_time = time.time() # Start the timer to calculate total spinning duration |
422 | 432 | slide() |
423 | 433 |
|
| 434 | + # Dynamically adjust both speed and frame delay |
| 435 | + def setup_animation(self): |
| 436 | + canvas_width = self.canvas.winfo_width() |
| 437 | + total_distance = len(self.active_images) * canvas_width |
| 438 | + desired_duration = 10000 # 10 seconds in milliseconds |
| 439 | + |
| 440 | + # Adjust frame delay or animation speed dynamically |
| 441 | + self.frame_delay = max(1, 16) # Default to 60 FPS if possible |
| 442 | + frames = desired_duration // self.frame_delay |
| 443 | + self.animation_speed = max(1, total_distance // frames) |
| 444 | + |
424 | 445 | def display_selected_game(self): |
425 | 446 | """Display the selected game image after the animation stops.""" |
426 | 447 | self.label_game_name.config(text=f"{self.selected_game['name']}") |
|
0 commit comments