Implemented spinner ticking sound

This commit is contained in:
2025-06-26 12:32:11 +01:00
parent 23d0ca1ee2
commit 95e4df2810
2 changed files with 26 additions and 2 deletions

28
game.py
View File

@ -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.