Compare commits

...

7 Commits

Author SHA1 Message Date
a12a583444 Linted 2 lines 2025-06-26 14:53:57 +01:00
dce162ba4a Made the first item appear on the right 2025-06-26 14:53:46 +01:00
c8b25ff53b Fixed big with which way the text rendered 2025-06-26 14:52:56 +01:00
94fa33ebd0 Made KEYDOWN event more flexible 2025-06-26 14:51:06 +01:00
a1c5641eee Added global font & render current item 2025-06-26 14:50:41 +01:00
471de3ef6e Created WheelItem class 2025-06-26 14:48:33 +01:00
e8ee151267 Removed TODO 2025-06-26 14:47:11 +01:00

50
game.py
View File

@ -13,7 +13,6 @@ import pygame
# TODO: Prevent another animation from starting when the wheel's spinning # TODO: Prevent another animation from starting when the wheel's spinning
# TODO: Convery fade in / out time to be random # TODO: Convery fade in / out time to be random
# TODO: Make random finer (to prevent a range of 4-5 only having two possible speeds) # TODO: Make random finer (to prevent a range of 4-5 only having two possible speeds)
# TODO: Utilise spinner.mp3 (ideally without using 2k audio channels)
def translate(value, aMin, aMax, bMin, bMax): def translate(value, aMin, aMax, bMin, bMax):
""" """
@ -22,8 +21,8 @@ def translate(value, aMin, aMax, bMin, bMax):
properly here. properly here.
""" """
aRange = (aMax - aMin) aRange = aMax - aMin
bRange = (bMax - bMin) bRange = bMax - bMin
return (((value - aMin) * bRange) / aRange) + bMin return (((value - aMin) * bRange) / aRange) + bMin
def rotateRad(surface: pygame.Surface, radians: float) -> pygame.Surface: def rotateRad(surface: pygame.Surface, radians: float) -> pygame.Surface:
@ -47,6 +46,10 @@ def rotateDeg(surface: pygame.Surface, degrees: float) -> pygame.Surface:
rotSurface = gameUtils.centre(rotSurface, surface.size) rotSurface = gameUtils.centre(rotSurface, surface.size)
return rotSurface return rotSurface
class WheelItem:
def __init__(self, text):
self.text = text
class Game(gameUtils.Game): class Game(gameUtils.Game):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """
@ -82,7 +85,13 @@ class Game(gameUtils.Game):
font = pygame.font.SysFont('', 32) font = pygame.font.SysFont('', 32)
self.items = [f'Example item {i+1}' for i in range(13)] self.items = []
self.items.append(WheelItem('Drink 1 shot'))
self.items.append(WheelItem('Drink 2 shots'))
self.items.append(WheelItem('Get shocked at 50%'))
self.items.append(WheelItem('Get shocked at 100%'))
self.items.append(WheelItem('Get slapped by\neveryone in the room'))
self.items.append(WheelItem('Respin, wheel hidden'))
# How many pixels away from the outer side of the # How many pixels away from the outer side of the
# circle the text should be # circle the text should be
textMargin = 5 textMargin = 5
@ -97,8 +106,6 @@ class Game(gameUtils.Game):
for idx, item in enumerate(self.items): for idx, item in enumerate(self.items):
# Calculate the radians of this item # Calculate the radians of this item
rad = (idx / len(self.items)) * maths.pi * 2 rad = (idx / len(self.items)) * maths.pi * 2
# Rotate it CCW be 90deg so the first item is on the top
rad -= maths.pi / 2
# Calculate the vector offset by half the item gap # Calculate the vector offset by half the item gap
# This is because the text should be in the middle, # This is because the text should be in the middle,
# and the lines (which is what this is used by) on the edges # and the lines (which is what this is used by) on the edges
@ -111,7 +118,7 @@ class Game(gameUtils.Game):
# Create a surface that our text will be rendered onto # Create a surface that our text will be rendered onto
textSurf = pygame.Surface(self.wheelSurf.size, pygame.SRCALPHA) textSurf = pygame.Surface(self.wheelSurf.size, pygame.SRCALPHA)
# Render the item text # Render the item text
text = font.render(item, True, (255, 255, 255)) text = font.render(item.text, True, (255, 255, 255))
# Draw the item text onto the text surface, centred to the right # Draw the item text onto the text surface, centred to the right
textSurf.blit(text, (textSurf.size[0]-text.get_width()-textMargin, self.size[1]//2 - text.get_height()//2)) textSurf.blit(text, (textSurf.size[0]-text.get_width()-textMargin, self.size[1]//2 - text.get_height()//2))
# Draw debug lines # Draw debug lines
@ -119,9 +126,11 @@ class Game(gameUtils.Game):
#pygame.draw.line(textSurf, (255, 0, 0), (circleR, circleR), (textSurf.size[0], circleR)) #pygame.draw.line(textSurf, (255, 0, 0), (circleR, circleR), (textSurf.size[0], circleR))
#pygame.draw.line(textSurf, (0, 255, 0), (circleR, circleR), (circleR, textSurf.size[1])) #pygame.draw.line(textSurf, (0, 255, 0), (circleR, circleR), (circleR, textSurf.size[1]))
# Rotate the text surface # Rotate the text surface
textSurf = rotateRad(textSurf, rad) textSurf = rotateRad(textSurf, -rad)
# Blit the text surface to the wheel surface # Blit the text surface to the wheel surface
self.wheelSurf.blit(textSurf, (0, 0)) self.wheelSurf.blit(textSurf, (0, 0))
self.font = pygame.font.SysFont('', 50)
def spinnerTick(self): def spinnerTick(self):
# If there's a tick already playing # If there's a tick already playing
@ -138,9 +147,10 @@ class Game(gameUtils.Game):
Args: Args:
event (pygame.Event): The event that was fired. event (pygame.Event): The event that was fired.
""" """
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: if event.type == pygame.KEYDOWN:
self.wheelSpinSpeed = random.randint(*self.WHEEL_SPEED_RANGE) if event.key == pygame.K_SPACE:
self.wheelAnimStart = time.perf_counter() self.wheelSpinSpeed = random.randint(*self.WHEEL_SPEED_RANGE)
self.wheelAnimStart = time.perf_counter()
def update(self): def update(self):
""" """
@ -160,15 +170,17 @@ class Game(gameUtils.Game):
self.wheelRotation += speed * self.timeDelta self.wheelRotation += speed * self.timeDelta
self.wheelRotation %= 360 self.wheelRotation %= 360
# Calculates the current item index with... I don't know, magic? # Calculates the current item index with... I don't know, magic?
currentItemIdx = round((self.wheelRotation / 360) * len(self.items)) % len(self.items) currentItemIdx = round((self.wheelRotation / 360) * len(self.items)) % len(self.items)
# If the last tick was on a different index, # If the last tick was on a different index,
if currentItemIdx != self.lastTickItem: if currentItemIdx != self.lastTickItem:
# Click again # Click again
self.spinnerTick() self.spinnerTick()
# And set the last tick index to the current one # And set the last tick index to the current one
self.lastTickItem = currentItemIdx self.lastTickItem = currentItemIdx
self.surf.blit(self.font.render(self.items[currentItemIdx].text, True, (255, 255, 255)), (0, 0))
def close(self): def close(self):
""" """