From 95e4df2810990e0fe78b96fd92df3d15e1cb35b7 Mon Sep 17 00:00:00 2001 From: Brosef Date: Thu, 26 Jun 2025 12:32:11 +0100 Subject: [PATCH] Implemented spinner ticking sound --- assets/{spinner.mp3 => spinner-tick.mp3} | Bin game.py | 28 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) rename assets/{spinner.mp3 => spinner-tick.mp3} (100%) diff --git a/assets/spinner.mp3 b/assets/spinner-tick.mp3 similarity index 100% rename from assets/spinner.mp3 rename to assets/spinner-tick.mp3 diff --git a/game.py b/game.py index 7ad5aa2..1d241bd 100644 --- a/game.py +++ b/game.py @@ -66,13 +66,19 @@ class Game(gameUtils.Game): # How long (in seconds) it takes for the wheel to get to full speed self.WHEEL_SPIN_FADE_IN = 0.25 # How long (in seconds) it takes for the wheel to go back to stationary - self.WHEEL_SPIN_FADE_OUT = 2.5 + self.WHEEL_SPIN_FADE_OUT = 3 # The minimum and maximum speeds of the wheel - self.WHEEL_SPEED_RANGE = [360, 550] + self.WHEEL_SPEED_RANGE = [360, 1000] # Holds the decided speed of the wheel self.wheelSpinSpeed = 0 # When the wheel started its spin animation self.wheelAnimStart = 0 + # The ticker sound effect + self.tickSfx = pygame.mixer.Sound("./assets/spinner-tick.mp3") + # The last time (in time.perf_counter()) the ticker was played + self.lastTickTime = 0 + # The last index the ticker ticked on + self.lastTickItem = -1 font = pygame.font.SysFont('', 32) @@ -117,6 +123,13 @@ class Game(gameUtils.Game): # Blit the text surface to the wheel surface self.wheelSurf.blit(textSurf, (0, 0)) + def spinnerTick(self): + # If there's a tick already playing + if time.perf_counter() - self.lastTickTime < self.tickSfx.get_length(): + return + + self.tickSfx.play() + self.lastTickTime = time.perf_counter() def onEvent(self, event): """ @@ -134,6 +147,7 @@ class Game(gameUtils.Game): Ran once per frame, put your drawing code and any game logic that should be ran once per frame in here. """ + self.surf.fill((0, 0, 0)) self.surf.blit(rotateDeg(gameUtils.centre(self.wheelSurf, self.size), self.wheelRotation), (0, 0)) animTime = time.perf_counter() - self.wheelAnimStart @@ -146,6 +160,16 @@ class Game(gameUtils.Game): self.wheelRotation += speed * self.timeDelta self.wheelRotation %= 360 + # Calculates the current item index with... I don't know, magic? + currentItemIdx = round((self.wheelRotation / 360) * len(self.items)) % len(self.items) + + # If the last tick was on a different index, + if currentItemIdx != self.lastTickItem: + # Click again + self.spinnerTick() + # And set the last tick index to the current one + self.lastTickItem = currentItemIdx + def close(self): """ Ran when the game closes.