forked from CZHerrington/space-force-prime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_scroll.py
74 lines (66 loc) · 2.31 KB
/
text_scroll.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
import pygame
class TextScroller(object):
def __init__(self, lines, step, font, color, x = 0):
self.step, self.font, self.color, self.x = step, font, color, x
self.lines = [ (None, x) for x in lines]
self.offset = 0
self.running = True
def get_img(self, idx):
img, txt = self.lines[idx]
if not img:
# print("render", idx, txt)
img = self.font.render(txt, True, self.color)
self.lines[idx] = img, txt
return img
def render(self, screen):
_, screen_y = screen.get_size()
y = screen_y - self.offset
for i in range(len(self.lines)):
img = self.get_img(i)
h = img.get_height()
if y + h > 0:
screen.blit(img, (0, y))
y += h
if y>screen_y:
break
self.offset += self.step
self.running = y > 0
return self.running
def get_scroller():
lines = [
"It's the year 2069 (nice!)",
"and President Barron Trump's",
"U.S. Space Force program is in ruins.",
" ",
"Amazon's Cyborg CEO, Jeff Bezos 4.0",
"just bought out the derelict military branch.",
" ",
"As a newly-hired delivery human,",
"it's your job to deliver packages to",
"Prime customers while defending",
"your ship, crew, and cargo.",
" ",
"Good luck ...and watch out for asteroids!",
]
return TextScroller(lines, 5, pygame.font.Font(None, 45), (0, 168, 224))
def get_screen(w,h):
return pygame.display.set_mode((w,h), pygame.RESIZABLE)
def intro():
pygame.init()
pygame.display.set_caption("Space Force Prime")
SCROLL_EVENT = pygame.USEREVENT + 1
scroller = get_scroller()
pygame.time.set_timer(SCROLL_EVENT, 50)
screen = get_screen(720,720)
while screen:
event = pygame.event.wait()
if event.type == pygame.QUIT:
screen = None
elif event.type == pygame.KEYDOWN:
screen = None
elif event.type == pygame.VIDEORESIZE:
screen = get_screen(event.w, event.h)
elif event.type == SCROLL_EVENT:
screen.fill((0, 0, 0))
screen = {True: screen, False: None}[scroller.render(screen)]
pygame.display.flip()