-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenEngine.py
More file actions
269 lines (216 loc) · 9.64 KB
/
ScreenEngine.py
File metadata and controls
269 lines (216 loc) · 9.64 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import pygame
import collections
# список цветов
import Service
colors = {
"black": (0, 0, 0, 255),
"white": (255, 255, 255, 255),
"red": (255, 0, 0, 255),
"green": (0, 255, 0, 255),
"blue": (0, 0, 255, 255),
"wooden": (153, 92, 0, 255),
}
class ScreenHandle(pygame.Surface):
def __init__(self, *args, **kwargs):
if len(args) > 1:
self.successor = args[-1]
self.next_coord = args[-2]
args = args[:-2]
else:
self.successor = None
self.next_coord = (0, 0)
super().__init__(*args, **kwargs)
self.fill(colors["wooden"])
def draw(self, canvas):
if self.successor is not None:
canvas.blit(self.successor, self.next_coord)
self.successor.draw(canvas)
def connect_engine(self, engine):
if self.successor is not None:
return self.successor.connect_engine(engine)
class GameSurface(ScreenHandle):
def connect_engine(self, engine):
self.engine = engine
super().connect_engine(engine)
def calculate(self):
width, height = self.get_size()
x = int(width/self.engine.sprite_size)
y = int(height/self.engine.sprite_size)
if self.engine.hero.position[0] > (x-2):
min_x = self.engine.hero.position[0] - (x-2)
else:
min_x = 0
if self.engine.hero.position[1] > (y-2):
min_y = self.engine.hero.position[1] - (y-2)
else:
min_y = 0
return [min_x, min_y]
def draw_hero(self):
min_x, min_y = self.calculate()
self.blit(self.engine.hero.sprite,
((self.engine.hero.position[0] - min_x) * self.engine.sprite_size,
(self.engine.hero.position[1] - min_y) * self.engine.sprite_size))
def draw_map(self):
min_x, min_y = self.calculate()
if self.engine.map:
for i in range(len(self.engine.map[0]) - min_x):
for j in range(len(self.engine.map) - min_y):
self.blit(self.engine.map[min_y + j][min_x + i][
0], (i * self.engine.sprite_size, j * self.engine.sprite_size))
else:
self.fill(colors["white"])
def draw_object(self, sprite, coord):
size = self.engine.sprite_size
min_x, min_y = self.calculate()
self.blit(sprite, ((coord[0] - min_x) * self.engine.sprite_size,
(coord[1] - min_y) * self.engine.sprite_size))
def draw(self, canvas):
self.fill(colors["wooden"])
size = self.engine.sprite_size
min_x, min_y = self.calculate()
self.draw_map()
for obj in self.engine.objects:
self.blit(obj.sprite[0],
((obj.position[0] - min_x) * self.engine.sprite_size,
(obj.position[1] - min_y) * self.engine.sprite_size))
self.draw_hero()
super().draw(canvas)
class ProgressBar(ScreenHandle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fill(colors["wooden"])
def connect_engine(self, engine):
self.engine = engine
super().connect_engine(engine)
def draw(self, canvas):
self.fill(colors["wooden"])
pygame.draw.rect(self, colors["black"], (50, 30, 200, 30), 2)
pygame.draw.rect(self, colors["black"], (50, 70, 200, 30), 2)
pygame.draw.rect(self, colors[
"red"], (50, 30, 200 * self.engine.hero.hp / self.engine.hero.max_hp, 30))
pygame.draw.rect(self, colors["green"], (50, 70,
200 * self.engine.hero.exp / (100 * (2**(self.engine.hero.level - 1))), 30))
font = pygame.font.SysFont("comicsansms", 20)
self.blit(font.render(f'Герой на {self.engine.hero.position}', True, colors["black"]),
(250, 0))
self.blit(font.render(f'{self.engine.level} этаж', True, colors["black"]),
(10, 0))
self.blit(font.render(f'ОЗ', True, colors["black"]),
(10, 30))
self.blit(font.render(f'ОО', True, colors["black"]),
(10, 70))
self.blit(font.render(f'{self.engine.hero.hp}/{self.engine.hero.max_hp}', True, colors["black"]),
(80, 30))
self.blit(font.render(f'{self.engine.hero.exp}/{(100*(2**(self.engine.hero.level-1)))}', True, colors["black"]),
(80, 70))
self.blit(font.render(f'Уровень', True, colors["black"]),
(280, 30))
self.blit(font.render(f'Золото', True, colors["black"]),
(280, 70))
self.blit(font.render(f'{self.engine.hero.level}', True, colors["black"]),
(360, 30))
self.blit(font.render(f'{self.engine.hero.gold}', True, colors["black"]),
(360, 70))
self.blit(font.render(f'Сила', True, colors["black"]),
(420, 30))
self.blit(font.render(f'Удача', True, colors["black"]),
(420, 70))
self.blit(font.render(f'{self.engine.hero.stats["strength"]}', True, colors["black"]),
(480, 30))
self.blit(font.render(f'{self.engine.hero.stats["luck"]}', True, colors["black"]),
(480, 70))
self.blit(font.render(f'СЧЁТ', True, colors["black"]),
(550, 30))
self.blit(font.render(f'{self.engine.score:.4f}', True, colors["black"]),
(550, 70))
super().draw(canvas)
class InfoWindow(ScreenHandle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.len = 30
clear = []
self.data = collections.deque(clear, maxlen=self.len)
def connect_engine(self, engine):
self.engine = engine
engine.subscribe(self)
super().connect_engine(engine)
def update(self, value):
self.data.append(f"> {str(value)}")
def draw(self, canvas):
self.fill(colors["wooden"])
size = self.get_size()
font = pygame.font.SysFont("comicsansms", 10)
for i, text in enumerate(self.data):
self.blit(font.render(text, True, colors["black"]),
(5, 20 + 18 * i))
# FIXME draw next surface in chain (нарисуйте следующую поверхность в цепочке)
super().draw(canvas)
class HelpWindow(ScreenHandle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.len = 30
clear = []
self.data = collections.deque(clear, maxlen=self.len)
self.data.append([" →", "Переместить героя вправо"])
self.data.append([" ←", "Переместить героя влево"])
self.data.append([" ↑ ", "Переместить героя вверх"])
self.data.append([" ↓ ", "Переместить героя вниз"])
self.data.append([" H ", "Отобразить/Скрыть окно 'Помощь'"])
self.data.append(["Num+", "Увеличить карту"])
self.data.append(["Num-", "Уменьшить карту"])
self.data.append([" R ", "Перезапуск игры"])
self.data.append([" M ", "Отобразить/Скрыть миникарту"])
def connect_engine(self, engine):
self.engine = engine
super().connect_engine(engine)
def draw(self, canvas):
alpha = 0
if self.engine.show_help:
alpha = 128
self.fill((0, 0, 0, alpha))
size = self.get_size()
font1 = pygame.font.SysFont("courier", 24)
font2 = pygame.font.SysFont("serif", 24)
if self.engine.show_help:
pygame.draw.lines(self, (255, 0, 0, 255), True, [
(0, 0), (700, 0), (700, 500), (0, 500)], 5)
for i, text in enumerate(self.data):
self.blit(font1.render(text[0], True, ((128, 128, 255))),
(50, 50 + 30 * i))
self.blit(font2.render(text[1], True, ((128, 128, 255))),
(150, 50 + 30 * i))
super().draw(canvas)
class MiniMap(ScreenHandle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.size = 8
self.alpha = 100
self.colors = {"hero": (0, 128, 1),
"wall": (0, 0, 0, self.alpha),
"ground": (168, 168, 168, self.alpha),
"object": (117, 187, 253)
}
def connect_engine(self, engine):
self.engine = engine
super().connect_engine(engine)
def canvas_pos(self, args): #
return args[0] * self.size, args[1] * self.size
def draw_object(self, pos, color):
x, y = self.canvas_pos(pos)
pygame.draw.rect(self, color, (x, y, self.size, self.size))
def draw(self, canvas):
self.fill((0, 0, 0, 0))
if self.engine.show_minimap:
if self.engine.map:
for i in range(len(self.engine.map[0])):
for j in range(len(self.engine.map)):
if self.engine.map[j][i] == Service.wall:
color = self.colors["wall"]
else:
color = self.colors["ground"]
self.draw_object((i, j), color)
if self.engine.hero:
self.draw_object(self.engine.hero.position, self.colors["hero"])
for obj in self.engine.objects:
self.draw_object(obj.position, self.colors["object"])
super().draw(canvas)