-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScrollText.py
90 lines (81 loc) · 2.9 KB
/
ScrollText.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
#!/usr/bin/python3
# Original example by gunny26 at Github
# https://github.com/gunny26/pygame
import pygame
import sys
class ScrollText(object):
"""Simple 2d Scrolling Text"""
def __init__(self, surface, text, hpos, color, margin=(0,0), size=30, speed=1, font=None):
"""
(pygame.Surface) surface - surface to draw on
(string) text - text to draw
(int) hpos - horizontal position on y axis
(pygame.Color) color - color of font
(int) size - size of font
"""
self.surface = surface
# prepend and append some blanks
appendix = " " * (self.surface.get_width() / size)
self.text = appendix + text + appendix
self.hpos = hpos
self.color = color
self.margin = margin
self.size = size
self.speed = speed
# initialize
self.position = 0
if font is None:
self.font = pygame.font.SysFont("mono", self.size, bold=True)
else:
self.font = pygame.font.Font(font, self.size)
self.text_surface = self.font.render(self.text, True, self.color)
def update(self, hpos=None):
"""update every frame"""
if hpos is not None:
self.hpos = hpos
width = self.surface.get_width()-self.margin[0]-self.margin[1]
self.surface.blit(self.text_surface,
(self.margin[0], self.hpos),
(self.position, 0, width, self.text_surface.get_height())
)
if self.text_surface.get_width() > width:
if self.position < self.text_surface.get_width():
self.position += self.speed #1
else:
# set position on the far right, just outside the viewable area
self.position = -width
else:
self.position = 0
def update_text(self, text):
if text != self.text:
self.text = text
self.text_surface = self.font.render(self.text, True, self.color)
def test():
try:
fps = 25
surface = pygame.display.set_mode((600, 600))
pygame.init()
spheres = (
ScrollText(surface, "Dolor Ipsum Dolor uswef", 400, pygame.Color(255,255,0)),
)
clock = pygame.time.Clock()
pause = False
while True:
clock.tick(fps)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit(0)
keyinput = pygame.key.get_pressed()
if keyinput is not None:
if keyinput[pygame.K_ESCAPE]:
sys.exit(1)
if pause is not True:
surface.fill((0, 0, 0, 255))
for thing in spheres:
thing.update()
pygame.display.flip()
except KeyboardInterrupt:
print 'shutting down'
if __name__ == '__main__':
test()