1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import pygame
class CLabel(pygame.sprite.Sprite):
""" a basic label
properties:
font: font to use
text: text to display
fgColor: foreground color
bgColor: background color
center: position of label's center
size: (width, height) of label
"""
def __init__(self, fontName = "freesansbold.ttf"):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(fontName, 20)
self.text = "Button"
self.fgColor = ((0x00, 0x00, 0x00))
self.bgColor = ((0xFF, 0xFF, 0xFF))
self.center = (400, 100)
self.size = (150, 30)
def update(self):
self.image = pygame.Surface(self.size)
self.image.fill(self.bgColor)
fontSurface = self.font.render(self.text, True, self.fgColor, self.bgColor)
#center the text
xPos = (self.image.get_width() - fontSurface.get_width())/2
self.image.blit(fontSurface, (xPos, 0))
self.rect = self.image.get_rect()
self.rect.center = self.center
|