/
font_mgr.py
110 lines (84 loc) · 3 KB
/
font_mgr.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Copyright 2014 Daniele Basile <asterix24@gmail.com>
# Francesca Basile <hidefix92@gmail.com>
#
# \author Daniele Basile <asterix24@gmail.com>
# \author Francesca Basile <hidefix92@gmail.com>
#
import sys
import pygame
from pygame.locals import *
import cfg
from coordinate import *
class FontMgr(object):
def __init__(self):
self.update_callback = []
self.texts = []
self.fall_texts = []
self.blit_texts = []
def draw_text(self, text, color, position, size=18):
font = pygame.font.Font(cfg.FONT, size)
txt = font.render(text, True, color)
self.texts.append({'text': txt, 'position': position.get()})
self.update_callback.append(self._draw_text_update)
def _draw_text_update(self, surface):
for i in self.texts:
surface.blit(i['text'], i['position'])
return surface
def fall_text(self, text, color, start_pos, end_pos, delay, size=18):
font = pygame.font.Font(cfg.FONT, size)
self.fall_texts.append({
'obj':font.render(text, True, color),
'p_start':Pos(start_pos.x(), start_pos.y()),
'p_curr': Pos(start_pos.x(), start_pos.y()),
'p_end': end_pos,
'start': pygame.time.get_ticks(),
'delta': delay
})
self.update_callback.append(self._fall_text_update)
def _fall_text_update(self, surface):
for i in self.fall_texts:
if i['p_curr'] is not None:
if (pygame.time.get_ticks() - i['start']) > i['delta']:
i['start'] = pygame.time.get_ticks()
i['p_curr'].add_y(1)
if i['p_curr'].y() == i['p_end'].y():
i['p_curr'] = Pos(i['p_start'].x(), i['p_start'].y())
surface.blit(i['obj'], i['p_curr'].get())
return surface
def blit_text(self, text, color, position, delay, size=18):
font = pygame.font.Font(cfg.FONT, size)
self.blit_texts.append({
'obj':font.render(text, True, color),
'delay': delay,
'start': pygame.time.get_ticks(),
'pos':position,
'on': True
})
self.update_callback.append(self._blit_text_update)
def _blit_text_update(self, surface):
for i in self.blit_texts:
if (pygame.time.get_ticks() - i['start']) > i['delay']:
i['start'] = pygame.time.get_ticks()
i['on'] = not i['on']
if i['on']:
surface.blit(i['obj'], i['pos'].get())
return surface
def update(self, surface):
for update_foo in self.update_callback:
surface = update_foo(surface)
return surface