Added item text to wheel
This commit is contained in:
45
game.py
45
game.py
@ -6,6 +6,17 @@ import NoPELib
|
||||
|
||||
import numpy as np
|
||||
|
||||
def rotateRad(surface: pygame.Surface, radians: float) -> pygame.Surface:
|
||||
"""
|
||||
Rotates a surface clockwise in radians.
|
||||
"""
|
||||
# Calculate the degrees
|
||||
deg = radians*(180/maths.pi)
|
||||
# Convert to CCW becaus for some reason that's what Pygame uses?
|
||||
deg *= -1
|
||||
# Rotate the surface
|
||||
return pygame.transform.rotate(surface, deg)
|
||||
|
||||
class Game(gameUtils.Game):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
@ -22,16 +33,42 @@ class Game(gameUtils.Game):
|
||||
|
||||
self.wheelSurf = pygame.Surface([min(self.size)]*2) # Don't ask.
|
||||
|
||||
items = [f'Example item {i+1}' for i in range(10)]
|
||||
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, (100, 100, 100), (circleR, circleR), circleR)
|
||||
# Calculate the gap, in radians, between each item
|
||||
gap = maths.pi * 2 / len(items)
|
||||
|
||||
for idx, item in enumerate(items):
|
||||
scaledIdx = (idx / len(items)) * maths.pi * 2
|
||||
vector = maths.cos(scaledIdx), maths.sin(scaledIdx)
|
||||
# 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)
|
||||
pygame.draw.line(self.wheelSurf, (255, 255, 255), (circleR, circleR), endPos)
|
||||
# Draw the dividing line
|
||||
pygame.draw.line(self.wheelSurf, (c, 0, 0), (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, (0, 0, 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(gameUtils.centre(textSurf, self.wheelSurf.size), (0, 0))
|
||||
|
||||
|
||||
def onEvent(self, event):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user