-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
372 lines (315 loc) · 14.2 KB
/
main.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import sys
from objects import *
class Main:
def __init__(self):
pygame.init()
pygame.display.set_caption(GAME_NAME)
self.screen = pygame.display.set_mode(SIZE)
self.clock = pygame.time.Clock()
self.asset_map = {}
self.groups = Groups(self)
self.current_map = ''
self.control_object = None
self.merged_cdata = []
self.cdata = {}
self.gp_var = {}
self.money_balance = 0
self.chest_opened = 0
self.skateboards = set()
self.time = 0
self.hearts = 3
self.loaded_sg = False
self.event_load_new_map = None
self.event_load_last_save = False
self.time = 0
self.collect_money_sfx = None
self.collect_card_sfx = None
def terminate(self):
self.asset_map.clear()
pygame.quit()
sys.exit()
def prepare_asset_map(self):
# Tiles 0-95
self.asset_map = {i: j for i, j in enumerate(split_on_tiles(
load_texture('GP_Tiles.png', '#b3afbb'), TILE_WIDTH, TILE_HEIGHT))}
# Dynamic 96-107
self.asset_map[96] = load_texture('GP_Card.png')
self.asset_map[97] = load_texture('GP_Chest.png')
self.asset_map[98] = load_texture('GP_Money.png')
self.asset_map[99] = load_texture('GP_Skateboard.png')
self.asset_map[100] = load_texture('GP_Fountain.png', (255, 255, 255))
self.asset_map[102] = load_texture('GP_Ladder_1.png')
self.asset_map[103] = load_texture('GP_Ladder_2.png')
# Bench 108-119
for i in range(4):
self.asset_map[2 * i + 108] = load_texture(f'GP_Bench_{i + 1}.png')
# Bush 120-143a
for i in range(20):
self.asset_map[i + 120] = load_texture(f'GP_Bush_{i + 1}.png')
# Stone 144-148
for i in range(5):
self.asset_map[i + 144] = load_texture(f'GP_Stone_{i + 1}.png')
# Ramp 149-155
self.asset_map[149] = load_texture('GP_Ramp.png')
# Box 156-167
self.asset_map[156] = load_texture('GP_Box.png')
# Tree 168-179
for i in range(4):
self.asset_map[i + 168] = load_texture(f'GP_Tree_{i + 1}.png')
# End textures
self.collect_money_sfx = load_sound('collect_money_sfx')
self.collect_card_sfx = load_sound('collect_card_sfx')
def start_screen(self):
background = pygame.transform.scale(load_texture('CPO_BG.png'), (WIDTH, HEIGHT))
self.screen.blit(background, (0, 0))
title = Text(None, 70, GAME_NAME, COLOR_ORANGE, (0, 0))
title.move(((WIDTH - title.get_wh()[0]) // 2, 50))
title.draw(self.screen)
# Updater
update_text = Text(None, 40, 'Обновление игры...', COLOR_YELLOW, (0, 0))
update_text.move(((WIDTH - update_text.get_wh()[0]) // 2, 250))
update_text.draw(self.screen)
loading_pb = load_texture('LoadingPB.png')
loading_pb_rect = loading_pb.get_rect()
loading_pb_rect.x, loading_pb_rect.y = 340, 300
self.screen.blit(loading_pb, loading_pb_rect)
loading_p = load_texture('LoadingP.png')
# loading_p_rect = loading_p.get_rect()
# loading_p_rect.x, loading_p_rect.y = 340, 300
# screen.blit(loading_p, loading_p_rect)
updater = update_game()
for i in updater:
self.screen.fill((0, 0, 0))
self.screen.blit(background, (0, 0))
title.draw(self.screen)
if i < 0.0:
update_text.set_text('Сетевая ошибка. Игра будет запущена без обновлений')
update_text.move(((WIDTH - update_text.get_wh()[0]) // 2, 250))
elif i <= 100.0:
update_text.set_text(f'Обновление игры... ({i}%)')
update_text.move(((WIDTH - update_text.get_wh()[0]) // 2, 250))
loading_ps = pygame.transform.scale(loading_p, (600 * i // 100, 50))
loading_ps_rect = loading_p.get_rect()
loading_ps_rect.x, loading_ps_rect.y = 340, 300
self.screen.blit(loading_ps, loading_ps_rect)
self.screen.blit(loading_pb, loading_pb_rect)
update_text.draw(self.screen)
pygame.display.flip()
if i < 0.0:
sleep(5)
break
self.screen.fill((0, 0, 0))
self.screen.blit(background, (0, 0))
title.draw(self.screen)
rules_font = pygame.font.Font(None, 30)
text_coord = 100
for line in GAME_RULES:
string_rendered = rules_font.render(line, True, COLOR_YELLOW)
intro_rect = string_rendered.get_rect()
text_coord += 10
intro_rect.top = text_coord
intro_rect.x = 10
text_coord += intro_rect.height
self.screen.blit(string_rendered, intro_rect)
play_btn = Button(Text(None, 60, 'Загрузить' if self.loaded_sg else 'Новая игра',
COLOR_BLUE, (0, 0)), COLOR_YELLOW,
rect=pygame.Rect(WIDTH // 2 - 200, 350, 400, 100))
play_btn.draw(self.screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.terminate()
elif event.type == pygame.MOUSEBUTTONDOWN:
if play_btn.get_collide_rect().collidepoint(event.pos) \
and pygame.mouse.get_pressed()[0] == 1:
return
pygame.display.flip()
self.clock.tick(FPS)
@staticmethod
def save_start_game():
sg_data = {
'pos': ('mapT', 270, 1700),
'time': 0,
'merged_cdata': [],
'cdata': {},
'gp_var': {'true': 1, 'false': 0},
'money_balance': 0,
'chest_opened': 0,
'skateboards': set()
}
save_data_to_bin_file(sg_data, SAVE_GAME_FNAME)
def save_game(self, pos):
x, y = pos[0], pos[1] - 30
sg_data = {
'pos': (self.current_map, x, y),
'time': self.time,
'merged_cdata': self.merged_cdata,
'cdata': self.cdata,
'gp_var': self.gp_var,
'money_balance': self.money_balance,
'chest_opened': self.chest_opened,
'skateboards': self.skateboards
}
save_data_to_bin_file(sg_data, SAVE_GAME_FNAME)
def load_game(self):
sg_data = load_data_from_bin_file(SAVE_GAME_FNAME)
self.merged_cdata = sg_data['merged_cdata']
self.time = sg_data['time']
self.cdata = sg_data['cdata']
self.gp_var = sg_data['gp_var']
self.money_balance = sg_data['money_balance']
self.chest_opened = sg_data['chest_opened']
self.skateboards = sg_data['skateboards']
return sg_data['pos']
def collect_money(self, key):
self.collect_money_sfx.play()
count = self.cdata[key]['count']
self.money_balance += count
self.cdata.pop(key)
print(f'Money balance: {self.money_balance}')
def collect_card(self, key):
self.collect_card_sfx.play()
k, v = self.cdata[key]['gp_var_key'], self.cdata[key]['gp_var_value']
self.gp_var[k] = v
self.cdata.pop(key)
def collect_skateboard(self, key):
number = self.cdata[key]['number']
self.skateboards.add(number)
self.cdata.pop(key)
def interact_chest(self, key):
items = self.cdata[key]['items']
self.chest_opened += 1
print(f'Chest opened: {self.chest_opened}, {items}')
self.cdata.pop(key)
def load_new_map(self, new_map_name):
self.event_load_new_map = new_map_name
def load_last_save(self):
self.event_load_last_save = True
def make_level(self, level, level_name):
for y, row in enumerate(level):
for x, item in enumerate(row):
if item < 0:
continue
if 0 <= item <= 95:
t = Tile(self.groups, self.asset_map[item], (x, y))
if self.control_object is None:
self.control_object = t
elif 96 <= item <= 101:
obj_name = ['card', 'chest', 'money', 'skateboard', 'fountain', 'fountain'
][item - 96]
key = f'{level_name}_{obj_name}_{x}_{y}'
if key not in self.cdata:
continue
if item == 96:
Card(self.groups, self.asset_map[item], (x, y), key)
elif item == 97:
Chest(self.groups, self.asset_map[item], (x, y), key)
elif item == 98:
Money(self.groups, self.asset_map[item], (x, y), key)
elif item == 99:
Skateboard(self.groups, self.asset_map[item], (x, y), key)
elif item == 100 or item == 101: # Pic in 100
Fountain(self.groups, self.asset_map[100], (x, y), item == 100, key)
elif item == 102 or item == 103:
Ladder(self.groups, self.asset_map[item], (x, y))
elif 108 <= item <= 119:
if item not in [108, 110, 112, 114]:
continue
Bench(self.groups, self.asset_map[item], (x, y))
elif 120 <= item <= 179 and item != 156:
Object(self.groups, self.asset_map[item], (x, y), False)
elif item == 156:
Object(self.groups, self.asset_map[item], (x, y), True)
def load_map_and_cdata(self):
# Cdata
if self.current_map not in self.merged_cdata:
for i, j in load_cdata(self.current_map).items():
self.cdata[i] = j
self.merged_cdata.append(self.current_map)
# Map
m = load_map(self.current_map)
self.make_level(m, self.current_map)
def prepare_map_to_play(self, player_pos_x=None, player_pos_y=None):
self.load_map_and_cdata()
if player_pos_x is None or player_pos_y is None:
player_pos_x, player_pos_y = self.cdata[f'{self.current_map}_spawn_pos']
player = Player(self.groups, player_pos_x, player_pos_y)
camera = Camera()
return player, camera
def run(self):
self.loaded_sg = os.path.isfile(os.path.join('data', SAVE_GAME_FNAME))
self.start_screen()
if not self.loaded_sg:
self.save_start_game()
self.prepare_asset_map()
self.current_map, player_pos_x, player_pos_y = self.load_game()
background = pygame.transform.scale(load_texture('S_BG3.png'), SIZE)
player, camera = self.prepare_map_to_play(player_pos_x, player_pos_y)
running = True
is_paused, cont_btn, back_mm_btn = False, None, None
while running:
self.screen.fill('#FFFFFF')
self.screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
is_paused = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if cont_btn is not None and cont_btn.get_collide_rect().collidepoint(event.pos) \
and pygame.mouse.get_pressed()[0] == 1:
is_paused = False
continue
if back_mm_btn is not None and \
back_mm_btn.get_collide_rect().collidepoint(event.pos) \
and pygame.mouse.get_pressed()[0] == 1:
for i in self.groups.all_sprites.sprites():
i.kill()
self.groups.all_sprites.empty()
return self.run()
if is_paused:
cont_btn = Button(Text(None, 40, 'Продолжить', COLOR_BLUE, (0, 0)),
COLOR_YELLOW, rect=pygame.Rect(WIDTH // 2 - 200, 250, 400, 100))
back_mm_btn = Button(Text(None, 40, 'Вернуться в главное меню', COLOR_BLUE, (0, 0)),
COLOR_YELLOW, rect=pygame.Rect(WIDTH // 2 - 200, 400, 400, 100))
cont_btn.draw(self.screen)
back_mm_btn.draw(self.screen)
pygame.display.flip()
continue
if self.event_load_last_save:
for i in self.groups.all_sprites.sprites():
i.kill()
self.groups.all_sprites.empty()
self.current_map, player_pos_x, player_pos_y = self.load_game()
player, camera = self.prepare_map_to_play(player_pos_x, player_pos_y)
self.event_load_last_save = False
if self.event_load_new_map is not None:
# Load new map
for i in self.groups.all_sprites.sprites():
i.kill()
self.groups.all_sprites.empty()
self.current_map = self.event_load_new_map
player, camera = self.prepare_map_to_play()
self.event_load_new_map = None
player.update()
camera.update(player)
for sprite in self.groups.all_sprites:
camera.apply(sprite)
self.groups.updatable_group.update()
self.groups.all_sprites.draw(self.screen)
font = pygame.font.Font(None, 50)
minutes = str(self.time // 60000).rjust(2, '0')
seconds = str(self.time // 1000 % 60).rjust(2, '0')
text_money = font.render(f"{self.money_balance}", True, (255, 255, 0))
text_time = font.render(f'{minutes}:{seconds}', True, (255, 200, 200))
self.screen.blit(text_money, (900, 10))
self.screen.blit(text_time, (1100, 10))
for i in Widget.CONTAINER.values():
i.draw(self.screen)
self.time += self.clock.get_time()
self.clock.tick(FPS)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main = Main()
main.run()