forked from pytherman/pytherman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.py
202 lines (164 loc) · 6.15 KB
/
instruction.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""Screen with instruction how to play"""
from collections import OrderedDict
import pygame
import sys
from pygame.locals import *
from menuitem import MenuItem
import navigation_helpers as nh
class Instruction:
pygame.init()
def __init__(self, screen, bg_color=(0, 0, 0), font=None, font_size=35, font_color=(255, 255, 255)):
"""set up all variables, prepare basic navigation"""
self.screen = screen
self.scr_width = self.screen.get_rect().width
self.scr_height = self.screen.get_rect().height
self.bg_color = bg_color
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont(font, font_size)
self.font_color = font_color
self.items = []
self.funcs = OrderedDict()
self.text_color = (255, 255, 255)
self.funcs["Back"] = self.menu
self.funcs["Quit"] = sys.exit
items = self.funcs.keys()
for index, item in enumerate(items):
menu_item = MenuItem(item)
t_h = len(items) * menu_item.height
pos_x = (self.scr_width / 2) - (menu_item.width / 2)
pos_y = (self.scr_height / 2) - (t_h / 2) + ((index * 2) + index * menu_item.height) + 120
menu_item.set_position(pos_x, pos_y)
self.items.append(menu_item)
self.mouse_is_visible = True
self.cur_item = None
self.text_wall = TextWall()
self.text_wall.parse_text(instruction_text)
def menu(self):
"""Go back to menu"""
from mainmenu import Menu
gm = Menu(self.screen)
gm.run()
def run(self):
"""Main loop of screen, wait and react for user selection, show navigation, etc"""
mainloop = True
while mainloop:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False
if event.type == pygame.KEYDOWN:
self.mouse_is_visible = False
nh.set_item_selection(self, event.key)
if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
text = self.items[self.cur_item].text
mainloop = False
self.funcs[text]()
if event.type == pygame.MOUSEBUTTONDOWN:
for item in self.items:
(x, y) = pygame.mouse.get_pos()
if item.is_mouse_on_this(x, y):
self.funcs[item.text]()
if pygame.mouse.get_rel() != (0, 0):
self.mouse_is_visible = True
self.cur_item = None
nh.set_if_mouse_visible(self.mouse_is_visible)
self.screen.fill(self.bg_color)
for item in self.items:
if self.mouse_is_visible:
(x, y) = pygame.mouse.get_pos()
nh.set_mouse_selection(item, x, y)
self.screen.blit(item.label, item.position)
self.text_wall.draw()
pygame.display.flip()
class TextLine(object):
def __init__(self, font=None, size=24, text=""):
"""Set up all variables, colors and fonts"""
self.font_name = font
self.font_size = size
self.color_fg = Color("white")
self.color_bg = Color("gray20")
self._aa = True
self._text = text
self.font = pygame.font.Font(font, size)
self.screen = pygame.display.get_surface()
self.dirty = True
self.image = None
self._render()
def _render(self):
"""Render line of text"""
self.dirty = False
self.image = self.font.render(self._text, self.aa, self.color_fg)
self.rect = self.image.get_rect()
def draw(self):
"""Call this do draw, always prefers to use cache"""
if self.dirty or (self.image is None):
self._render()
self.screen.blit(self.image, self.rect)
@property
def text(self):
return self._text
@text.setter
def text(self, text):
self.dirty = True
self._text = text
@property
def aa(self): return self._aa
@aa.setter
def aa(self, aa):
self.dirty = True
self._aa = aa
class TextWall(object):
"""Manages multiple lines of text"""
def __init__(self, font=None, size=24):
self.font = font
self.font_size = size
self.offset = Rect(20, 20, 3, 3)
self.screen = pygame.display.get_surface()
self.dirty = True
self.text_lines = []
self._text_paragraph = "Empty\nText"
self._render()
def _render(self):
"""render list of lines"""
self.dirty = False
self.text_lines = [TextLine(self.font, self.font_size, line) for line in self._text_paragraph]
self.text_lines[0].rect.top = self.offset.top
# offset the height of each line
prev = Rect(0, 0, 0, 0)
for t in self.text_lines:
t.rect.top += prev.bottom
t.rect.left = self.offset.left
prev = t.rect
def parse_text(self, text):
"""parse raw text to something usable"""
self._text_paragraph = text.split("\n")
self._render()
def draw(self):
"""draw with cached surfaces"""
if self.dirty:
self._render()
for text in self.text_lines:
text.draw()
@property
def font_size(self):
return self._font_size
@font_size.setter
def font_size(self, size):
self.dirty = True
self._font_size = size
@property
def text(self):
return self._text_paragraph
@text.setter
def text(self, text_paragraph):
self.dirty = True
self.parse_text(text_paragraph)
if __name__ == '__main__':
pygame.display.set_caption('Instruction')
Instruction.run()
instruction_text = """Pytherman is strategic game, based on popular game called Bomberman.
You have to find secret door hidden in one of wooden walls on each level.
Wooden walls may be destroyed by bombs - normally, you need two explosions
but some of bonuses can reduce this number. Bonuses can also increase
number of your lives, bombs etc.
"""