Compare commits
7 Commits
95e4df2810
...
a12a583444
| Author | SHA1 | Date | |
|---|---|---|---|
| a12a583444 | |||
| dce162ba4a | |||
| c8b25ff53b | |||
| 94fa33ebd0 | |||
| a1c5641eee | |||
| 471de3ef6e | |||
| e8ee151267 |
30
game.py
30
game.py
@ -13,7 +13,6 @@ import pygame
|
||||
# TODO: Prevent another animation from starting when the wheel's spinning
|
||||
# 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: Utilise spinner.mp3 (ideally without using 2k audio channels)
|
||||
|
||||
def translate(value, aMin, aMax, bMin, bMax):
|
||||
"""
|
||||
@ -22,8 +21,8 @@ def translate(value, aMin, aMax, bMin, bMax):
|
||||
properly here.
|
||||
"""
|
||||
|
||||
aRange = (aMax - aMin)
|
||||
bRange = (bMax - bMin)
|
||||
aRange = aMax - aMin
|
||||
bRange = bMax - bMin
|
||||
return (((value - aMin) * bRange) / aRange) + bMin
|
||||
|
||||
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)
|
||||
return rotSurface
|
||||
|
||||
class WheelItem:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
class Game(gameUtils.Game):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
@ -82,7 +85,13 @@ class Game(gameUtils.Game):
|
||||
|
||||
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
|
||||
# circle the text should be
|
||||
textMargin = 5
|
||||
@ -97,8 +106,6 @@ class Game(gameUtils.Game):
|
||||
for idx, item in enumerate(self.items):
|
||||
# Calculate the radians of this item
|
||||
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
|
||||
# This is because the text should be in the middle,
|
||||
# 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
|
||||
textSurf = pygame.Surface(self.wheelSurf.size, pygame.SRCALPHA)
|
||||
# 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
|
||||
textSurf.blit(text, (textSurf.size[0]-text.get_width()-textMargin, self.size[1]//2 - text.get_height()//2))
|
||||
# Draw debug lines
|
||||
@ -119,10 +126,12 @@ class Game(gameUtils.Game):
|
||||
#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
|
||||
textSurf = rotateRad(textSurf, rad)
|
||||
textSurf = rotateRad(textSurf, -rad)
|
||||
# Blit the text surface to the wheel surface
|
||||
self.wheelSurf.blit(textSurf, (0, 0))
|
||||
|
||||
self.font = pygame.font.SysFont('', 50)
|
||||
|
||||
def spinnerTick(self):
|
||||
# If there's a tick already playing
|
||||
if time.perf_counter() - self.lastTickTime < self.tickSfx.get_length():
|
||||
@ -138,7 +147,8 @@ class Game(gameUtils.Game):
|
||||
Args:
|
||||
event (pygame.Event): The event that was fired.
|
||||
"""
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.wheelSpinSpeed = random.randint(*self.WHEEL_SPEED_RANGE)
|
||||
self.wheelAnimStart = time.perf_counter()
|
||||
|
||||
@ -170,6 +180,8 @@ class Game(gameUtils.Game):
|
||||
# And set the last tick index to the current one
|
||||
self.lastTickItem = currentItemIdx
|
||||
|
||||
self.surf.blit(self.font.render(self.items[currentItemIdx].text, True, (255, 255, 255)), (0, 0))
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
Ran when the game closes.
|
||||
|
||||
Reference in New Issue
Block a user