Compare commits

...

8 Commits

Author SHA1 Message Date
0dc6555499 Changed text margin 2025-06-26 16:55:15 +01:00
8b47026f0e Added arrow 2025-06-26 16:55:06 +01:00
097dd7486a Fixed bug in text rendering 2025-06-26 16:54:58 +01:00
6adb31f466 Updated debug line code 2025-06-26 16:54:49 +01:00
db4f7d05f7 Added wheel margin 2025-06-26 16:54:33 +01:00
dafeb80b77 Fixed typo 2025-06-26 16:44:40 +01:00
3e4907f192 Moved wheel drawing into its own function 2025-06-26 16:44:27 +01:00
1366c10fd7 Added item shuffling 2025-06-26 16:43:40 +01:00

34
game.py
View File

@ -9,9 +9,8 @@ import NoPELib
import numpy as np import numpy as np
import pygame import pygame
# TODO: Add the ability to re-draw the wheel to hightlight the selected item
# 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: Convert 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)
def translate(value, aMin, aMax, bMin, bMax): def translate(value, aMin, aMax, bMin, bMax):
@ -64,7 +63,7 @@ class Game(gameUtils.Game):
# Don't remove this. It does important things. :3 # Don't remove this. It does important things. :3
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.wheelSurf = pygame.Surface([min(self.size)]*2) # Don't ask. self.wheelSurf = pygame.Surface([min(self.size)-100]*2) # Don't ask.
self.wheelRotation = 0 self.wheelRotation = 0
# How long (in seconds) it takes for the wheel to get to full speed # How long (in seconds) it takes for the wheel to get to full speed
self.WHEEL_SPIN_FADE_IN = 0.25 self.WHEEL_SPIN_FADE_IN = 0.25
@ -83,7 +82,7 @@ class Game(gameUtils.Game):
# The last index the ticker ticked on # The last index the ticker ticked on
self.lastTickItem = -1 self.lastTickItem = -1
font = pygame.font.SysFont('', 32) self.wheelFont = pygame.font.SysFont('', 32)
self.items = [] self.items = []
self.items.append(WheelItem('Drink 1 shot')) self.items.append(WheelItem('Drink 1 shot'))
@ -92,9 +91,17 @@ class Game(gameUtils.Game):
self.items.append(WheelItem('Get shocked at 100%')) self.items.append(WheelItem('Get shocked at 100%'))
self.items.append(WheelItem('Get slapped by\neveryone in the room')) self.items.append(WheelItem('Get slapped by\neveryone in the room'))
self.items.append(WheelItem('Respin, wheel hidden')) self.items.append(WheelItem('Respin, wheel hidden'))
random.shuffle(self.items)
# 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 self.textMargin = 10
self.font = pygame.font.SysFont('', 50)
self.drawWheel()
def drawWheel(self):
# Calculate circle radius # Calculate circle radius
circleR = self.wheelSurf.size[0] / 2 circleR = self.wheelSurf.size[0] / 2
@ -118,19 +125,18 @@ 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.text, True, (255, 255, 255)) text = self.wheelFont.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()-self.textMargin, textSurf.size[1]//2 - text.get_height()//2))
# Draw debug lines # Draw debug lines
#pygame.draw.rect(textSurf, (0, 128, 255), (0, 0, textSurf.size[0], textSurf.size[1]), 1) #if idx < 1:
#pygame.draw.line(textSurf, (255, 0, 0), (circleR, circleR), (textSurf.size[0], circleR)) # pygame.draw.rect(textSurf, (0, 128, 255), (0, 0, textSurf.size[0], textSurf.size[1]), 1)
#pygame.draw.line(textSurf, (0, 255, 0), (circleR, circleR), (circleR, textSurf.size[1])) # 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]))
# 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
@ -159,6 +165,10 @@ class Game(gameUtils.Game):
""" """
self.surf.fill((0, 0, 0)) self.surf.fill((0, 0, 0))
self.surf.blit(rotateDeg(gameUtils.centre(self.wheelSurf, self.size), self.wheelRotation), (0, 0)) self.surf.blit(rotateDeg(gameUtils.centre(self.wheelSurf, self.size), self.wheelRotation), (0, 0))
arrowX = (self.size[0] - self.wheelSurf.size[0]) // 2 + self.wheelSurf.size[0]
arrowY = self.size[1] // 2
arrowSize = 20
pygame.draw.polygon(self.surf, (255, 255, 255), ((arrowX, arrowY), (arrowX+arrowSize, arrowY-arrowSize//2), (arrowX+arrowSize, arrowY+arrowSize//2)))
animTime = time.perf_counter() - self.wheelAnimStart animTime = time.perf_counter() - self.wheelAnimStart
if animTime < self.WHEEL_SPIN_FADE_IN + self.WHEEL_SPIN_FADE_OUT: if animTime < self.WHEEL_SPIN_FADE_IN + self.WHEEL_SPIN_FADE_OUT: