-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitmapfont.py
34 lines (27 loc) · 1 KB
/
Bitmapfont.py
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
33
34
import pygame
def toIndex(char):
return ord(char) - ord(' ')
class BitmapFont:
def __init__(self, fontFile, width, height):
self.image = pygame.image.load(fontFile)
self.cellWidth = width
self.cellHeight = height
width = self.image.get_rect().width
height = self.image.get_rect().height
self.cols = width / self.cellWidth
self.rows = height / self.cellHeight
def draw(self, surface, msg, x, y):
for c in msg:
ch = toIndex(c)
ox = (ch % self.cols) * self.cellWidth
oy = (ch / self.cols) * self.cellHeight
cw = self.cellWidth
ch = self.cellHeight
sourceRect = (ox, oy, cw, ch)
surface.blit(self.image, (x, y, cw, ch), sourceRect)
x += self.cellWidth
def centre(self, surface, msg, y):
width = len(msg) * self.cellWidth
halfWidth = surface.get_rect().width
x = (halfWidth - width) / 2
self.draw(surface, msg, x, y)