Skip to content

Commit 4f9ca01

Browse files
Update SteamRoulette.py
1 parent 66eae17 commit 4f9ca01

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

SteamRoulette.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sys
1212
import concurrent.futures
1313
import threading
14+
import time
1415

1516
# Constants
1617
STEAM_PATH = os.path.expanduser(r"C:\Program Files (x86)\Steam")
@@ -146,7 +147,7 @@ def __init__(self, root, installed_games, drives):
146147

147148
# Set initial animation speed
148149
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)
150151

151152
self.root.title("Steam Roulette")
152153
self.root.geometry("600x700")
@@ -195,6 +196,7 @@ def __init__(self, root, installed_games, drives):
195196
self.canvas = tk.Canvas(root, width=600, height=300, bg="black")
196197
self.canvas.pack(pady=1)
197198

199+
198200
# Create a frame to contain the game name label and other elements
199201
utility_frame = tk.Frame(self.root, bg=self.light_mode_bg)
200202
utility_frame.pack(pady=5)
@@ -223,7 +225,7 @@ def __init__(self, root, installed_games, drives):
223225
self.preloaded_images = {}
224226
self.preload_images()
225227

226-
# Use resource_path to get the correct image path
228+
# Use resource_path to get the correct logo path
227229
logo_path = resource_path("SteamRouletteLogo.png")
228230
try:
229231
logo_image = Image.open(logo_path)
@@ -385,42 +387,61 @@ def cycle_images(self):
385387
def animate_images(self):
386388
"""Animate the images sliding across the canvas."""
387389
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
389401

390402
def slide():
391403
# Move all images to the left
392404
for image_item, img_tk in self.active_images:
393405
self.canvas.move(image_item, -self.animation_speed, 0) # Move images to the left
394406

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
399412

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
406416

407417
# 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
409419
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
412420
last_image_left_x = last_image_x - img_width / 2 # Left edge of the last image
413421

414422
# 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
416424
self.display_selected_game() # Display selected game image
425+
print(f"Final spinning duration: {time.time() - self.start_time:.2f} seconds")
417426
return # End the animation
418427

419428
# Continue moving the images if they haven't reached the left edge
420429
self.animation_id = self.root.after(self.frame_delay, slide)
421430

431+
self.start_time = time.time() # Start the timer to calculate total spinning duration
422432
slide()
423433

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+
424445
def display_selected_game(self):
425446
"""Display the selected game image after the animation stops."""
426447
self.label_game_name.config(text=f"{self.selected_game['name']}")

0 commit comments

Comments
 (0)