Moved wheel config to its own class
This commit is contained in:
64
game.py
64
game.py
@ -45,6 +45,33 @@ 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 WCfg:
|
||||||
|
"""
|
||||||
|
Holds all the Wheel Configurations.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# The background colour of the wheel
|
||||||
|
bgColour = (25, 25, 25)
|
||||||
|
# The colour of the dividing lines
|
||||||
|
divColour = (75, 75, 75)
|
||||||
|
# The colour of the text
|
||||||
|
txtColour = (255, 255, 255)
|
||||||
|
# How much space is between the wheel and the screen
|
||||||
|
margin = 50
|
||||||
|
# The minimum and maximum time it takes (in seconds)
|
||||||
|
# for the wheel to get to full speed.
|
||||||
|
spinFadeInTimes = [0.25, 0.25]
|
||||||
|
# The minimum and maximum time it takes (in seconds)
|
||||||
|
# for the wheel to get to go back to being stationary.
|
||||||
|
spinFadeOutTimes = [3, 4]
|
||||||
|
# The minimum and maximum speeds of the wheel in deg/s
|
||||||
|
spinSpeedRange = [360, 1000]
|
||||||
|
# How many pixels away from the outer side of the
|
||||||
|
# circle the text should be
|
||||||
|
textMargin = 10
|
||||||
|
# The font to use for drawing the item text
|
||||||
|
font = pygame.font.SysFont('', 32)
|
||||||
|
|
||||||
class WheelItem:
|
class WheelItem:
|
||||||
def __init__(self, text):
|
def __init__(self, text):
|
||||||
self.text = text
|
self.text = text
|
||||||
@ -63,16 +90,14 @@ 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)-100]*2) # Don't ask.
|
self.wheelSurf = pygame.Surface([min(self.size)-WCfg.margin*2]*2) # Don't ask.
|
||||||
self.wheelRotation = 0
|
self.wheelRotation = 0
|
||||||
# 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 = 3
|
|
||||||
# The minimum and maximum speeds of the wheel
|
|
||||||
self.WHEEL_SPEED_RANGE = [360, 1000]
|
|
||||||
# Holds the decided speed of the wheel
|
# Holds the decided speed of the wheel
|
||||||
self.wheelSpinSpeed = 0
|
self.wheelSpinSpeed = 0
|
||||||
|
# Holds the decided fade in time
|
||||||
|
self.animFadeIn = 0
|
||||||
|
# Holds the decided fade out time
|
||||||
|
self.animFadeOut = 0
|
||||||
# When the wheel started its spin animation
|
# When the wheel started its spin animation
|
||||||
self.wheelAnimStart = 0
|
self.wheelAnimStart = 0
|
||||||
# The ticker sound effect
|
# The ticker sound effect
|
||||||
@ -82,8 +107,6 @@ class Game(gameUtils.Game):
|
|||||||
# The last index the ticker ticked on
|
# The last index the ticker ticked on
|
||||||
self.lastTickItem = -1
|
self.lastTickItem = -1
|
||||||
|
|
||||||
self.wheelFont = pygame.font.SysFont('', 32)
|
|
||||||
|
|
||||||
self.items = []
|
self.items = []
|
||||||
self.items.append(WheelItem('Drink 1 shot'))
|
self.items.append(WheelItem('Drink 1 shot'))
|
||||||
self.items.append(WheelItem('Drink 2 shots'))
|
self.items.append(WheelItem('Drink 2 shots'))
|
||||||
@ -93,11 +116,6 @@ class Game(gameUtils.Game):
|
|||||||
self.items.append(WheelItem('Respin, wheel hidden'))
|
self.items.append(WheelItem('Respin, wheel hidden'))
|
||||||
random.shuffle(self.items)
|
random.shuffle(self.items)
|
||||||
|
|
||||||
# How many pixels away from the outer side of the
|
|
||||||
# circle the text should be
|
|
||||||
self.textMargin = 10
|
|
||||||
|
|
||||||
|
|
||||||
self.font = pygame.font.SysFont('', 50)
|
self.font = pygame.font.SysFont('', 50)
|
||||||
self.drawWheel()
|
self.drawWheel()
|
||||||
|
|
||||||
@ -106,7 +124,7 @@ class Game(gameUtils.Game):
|
|||||||
circleR = self.wheelSurf.size[0] / 2
|
circleR = self.wheelSurf.size[0] / 2
|
||||||
|
|
||||||
# Draw the base circle
|
# Draw the base circle
|
||||||
pygame.draw.circle(self.wheelSurf, (25, 25, 25), (circleR, circleR), circleR)
|
pygame.draw.circle(self.wheelSurf, WCfg.bgColour, (circleR, circleR), circleR)
|
||||||
# Calculate the gap, in radians, between each item
|
# Calculate the gap, in radians, between each item
|
||||||
gap = maths.pi * 2 / len(self.items)
|
gap = maths.pi * 2 / len(self.items)
|
||||||
|
|
||||||
@ -120,14 +138,14 @@ class Game(gameUtils.Game):
|
|||||||
# Calculate the end position of a line based on the vector
|
# Calculate the end position of a line based on the vector
|
||||||
endPos = np.add(np.multiply(vector, circleR), circleR)
|
endPos = np.add(np.multiply(vector, circleR), circleR)
|
||||||
# Draw the dividing line
|
# Draw the dividing line
|
||||||
pygame.draw.line(self.wheelSurf, (128, 128, 128), (circleR, circleR), endPos)
|
pygame.draw.line(self.wheelSurf, WCfg.divColour, (circleR, circleR), endPos)
|
||||||
|
|
||||||
# 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 = self.wheelFont.render(item.text, True, (255, 255, 255))
|
text = WCfg.font.render(item.text, True, WCfg.txtColour)
|
||||||
# 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()-self.textMargin, textSurf.size[1]//2 - text.get_height()//2))
|
textSurf.blit(text, (textSurf.size[0]-text.get_width()-WCfg.textMargin, textSurf.size[1]//2 - text.get_height()//2))
|
||||||
# Draw debug lines
|
# Draw debug lines
|
||||||
#if idx < 1:
|
#if idx < 1:
|
||||||
# pygame.draw.rect(textSurf, (0, 128, 255), (0, 0, textSurf.size[0], textSurf.size[1]), 1)
|
# pygame.draw.rect(textSurf, (0, 128, 255), (0, 0, textSurf.size[0], textSurf.size[1]), 1)
|
||||||
@ -155,7 +173,7 @@ class Game(gameUtils.Game):
|
|||||||
"""
|
"""
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
self.wheelSpinSpeed = random.randint(*self.WHEEL_SPEED_RANGE)
|
self.wheelSpinSpeed = random.randint(*WCfg.spinSpeedRange)
|
||||||
self.wheelAnimStart = time.perf_counter()
|
self.wheelAnimStart = time.perf_counter()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -171,11 +189,11 @@ class Game(gameUtils.Game):
|
|||||||
pygame.draw.polygon(self.surf, (255, 255, 255), ((arrowX, arrowY), (arrowX+arrowSize, arrowY-arrowSize//2), (arrowX+arrowSize, arrowY+arrowSize//2)))
|
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 < WCfg.spinFadeInTimes[0] + WCfg.spinFadeOutTimes[0]:
|
||||||
if animTime < self.WHEEL_SPIN_FADE_IN:
|
if animTime < WCfg.spinFadeInTimes[0]:
|
||||||
speed = translate(animTime, 0, self.WHEEL_SPIN_FADE_IN, 0, self.wheelSpinSpeed)
|
speed = translate(animTime, 0, WCfg.spinFadeInTimes[0], 0, self.wheelSpinSpeed)
|
||||||
else:
|
else:
|
||||||
speed = translate(animTime, self.WHEEL_SPIN_FADE_IN, self.WHEEL_SPIN_FADE_IN+self.WHEEL_SPIN_FADE_OUT, self.wheelSpinSpeed, 0)
|
speed = translate(animTime, WCfg.spinFadeInTimes[0], WCfg.spinFadeInTimes[0]+WCfg.spinFadeOutTimes[0], self.wheelSpinSpeed, 0)
|
||||||
|
|
||||||
self.wheelRotation += speed * self.timeDelta
|
self.wheelRotation += speed * self.timeDelta
|
||||||
self.wheelRotation %= 360
|
self.wheelRotation %= 360
|
||||||
|
|||||||
Reference in New Issue
Block a user