This repository has been archived on 2026-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
SpinTheSin/game.py
2025-06-26 07:21:41 +01:00

142 lines
5.4 KiB
Python

import math as maths
import logging
import random
import time
import gameUtils
import NoPELib
import numpy as np
import pygame
def translate(value, aMin, aMax, bMin, bMax):
"""
Translates `value` to go from `aMin`-`aMax` to `bMin`-`bMax`
Stolen from StackOverflow because we don't have time to do shit
properly here.
"""
aRange = (aMax - aMin)
bRange = (bMax - bMin)
return (((value - aMin) * bRange) / aRange) + bMin
def rotateRad(surface: pygame.Surface, radians: float) -> pygame.Surface:
"""
Rotates a surface clockwise in radians and re-centres it.
"""
# Calculate the degrees
deg = radians*(180/maths.pi)
# Rotate the surface re-centre
return rotateDeg(surface, deg)
def rotateDeg(surface: pygame.Surface, degrees: float) -> pygame.Surface:
"""
Rotates a surface clockwise in degrees and re-centres it.
"""
# Convert to CCW becaus for some reason that's what Pygame uses?
degrees *= -1
# Rotate surface which will expand it
rotSurface = pygame.transform.rotate(surface, degrees)
# re-centre it to the orinal surface bounding box
rotSurface = gameUtils.centre(rotSurface, surface.size)
return rotSurface
class Game(gameUtils.Game):
def __init__(self, *args, **kwargs):
"""
Ran when the game starts.
You have access to the following variables:
self.surf (pygame.Surface): This is the surface you draw onto.
self.pm (NoPELib.PlayerManager): This is where your players are stored.
self.cfg (dict): Everything from the `game.toml` file. You can access it like this: self.details['title']
"""
# Don't remove this. It does important things. :3
super().__init__(*args, **kwargs)
self.wheelSurf = pygame.Surface([min(self.size)]*2) # Don't ask.
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 = 2.5
# The minimum and maximum speeds of the wheel
self.WHEEL_SPEED_RANGE = [5, 15]
# Holds the decided speed of the wheel
self.wheelSpinSpeed = 0
# When the wheel started its spin animation
self.wheelAnimStart = 0
font = pygame.font.SysFont('', 32)
items = [f'Example item {i+1}' for i in range(17)]
# Calculate circle radius
circleR = self.wheelSurf.size[0] / 2
# Draw the base circle
pygame.draw.circle(self.wheelSurf, (25, 25, 25), (circleR, circleR), circleR)
# Calculate the gap, in radians, between each item
gap = maths.pi * 2 / len(items)
for idx, item in enumerate(items):
# Calculate the radians of this item
rad = (idx / len(items)) * maths.pi * 2
# Calculate the vector from that
vector = maths.cos(rad), maths.sin(rad)
# Calculate the end position of a line based on the vector
endPos = np.add(np.multiply(vector, circleR), circleR)
# Draw the dividing line
pygame.draw.line(self.wheelSurf, (128, 128, 128), (circleR, circleR), endPos)
# 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))
# Draw the item text onto the text surface, centred to the right
textSurf.blit(text, (textSurf.size[0]-text.get_width(), self.size[1]//2 - text.get_height()//2))
# Draw debug lines
#pygame.draw.rect(textSurf, (0, 128, 255), (0, 0, textSurf.size[0], textSurf.size[1]), 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
textSurf = rotateRad(textSurf, rad+gap/2)
# Blit the text surface to the wheel surface
self.wheelSurf.blit(textSurf, (0, 0))
def onEvent(self, event):
"""
Ran when an event is fired.
Args:
event (pygame.Event): The event that was fired.
"""
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
self.wheelSpinSpeed = random.randint(*self.WHEEL_SPEED_RANGE)
self.wheelAnimStart = time.perf_counter()
def update(self):
"""
Ran once per frame, put your drawing code and any
game logic that should be ran once per frame in here.
"""
self.surf.blit(rotateDeg(gameUtils.centre(self.wheelSurf, self.size), self.wheelRotation), (0, 0))
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:
speed = translate(animTime, 0, self.WHEEL_SPIN_FADE_IN, 0, self.wheelSpinSpeed)
else:
speed = translate(animTime, self.WHEEL_SPIN_FADE_IN, self.WHEEL_SPIN_FADE_IN+self.WHEEL_SPIN_FADE_OUT, self.wheelSpinSpeed, 0)
self.wheelRotation += speed
self.wheelRotation %= 360
print(self.wheelRotation)
def close(self):
"""
Ran when the game closes.
"""
pass