-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
143 lines (120 loc) · 5.69 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
import pygame
from config import *
from map import create_map, create_minimap
from menu import MainMenu, show_info
from mode import Modes
from player import Player
from render import Render, wall_textures
from result_window import Win, Losing
from sound import Music
from sprite import create_sprites, sprites_update, is_win
from stats import Stats
from utils import is_game_over
from weapon import Weapon
"""
Павлов Тимур 26.12.2021. Создан класс Game
Вайман Ангелина 28.12.2021. Внесены поправки
Вайман Ангелина 03.01.2022. Создана новая поверхность screen_map для миникарты
Батталов Арслан 05.01.2022. Добавлены функция play_theme
Батталов Арслан 08.01.2022 Добавлена поддержка звуков выстрела
Павлов Тимур 08.01.2022. Исправлена ошибка анимации оружия
Батталов Арслан 08.01.2022 Исправлена проблема двойного проигрывания звуков
Павлов Тимур 09.01.2022. Добавлена проверка окончания игры
Павлов Тимур 09.01.2022. Добавлен способ выхода после проигрыша или выигрыша
Павлов Тимур 09.01.2022. Добавлена поддержка режим Arcade
"""
class Game:
def __init__(self):
self._screen = pygame.display.set_mode(SCREEN_SIZE, pygame.DOUBLEBUF)
self._minimap_screen = pygame.Surface((MAP_SIZE[0] * MAP_TILE, MAP_SIZE[1] * MAP_TILE))
self._clock = pygame.time.Clock()
self._caption = WINDOW_NAME
self._sprites = list()
def _pre_init(self):
pygame.display.set_caption('YOOM')
self._menu = MainMenu(self._screen, self._clock)
self._losing = Losing(self._screen, self._clock)
self._win = Win(self._screen, self._clock)
def run(self):
self._pre_init()
self._menu.run()
self._init()
self._play_theme()
self._config()
self._update()
self._finish()
def _init(self):
self._weapons = [
Weapon(self._screen, 'Gun1', (500, 450), SHOTGUN_ANIMATION_FRAME_COUNTS, SHOTGUN, SHOTGUN_AMMO,
SHOTGUN_DAMAGE, SHOTGUN_MAX_DISTANCE),
Weapon(self._screen, 'Gun2', (400, 400), PISTOL_ANIMATION_FRAME_COUNTS, PISTOL, PISTOL_AMMO, PISTOL_DAMAGE,
PISTOL_MAX_DISTANCE),
Weapon(self._screen, 'Gun3', (350, 300), RIFLE_ANIMATION_FRAME_COUNTS, RIFLE, RIFLE_AMMO, RIFLE_DAMAGE,
RIFLE_MAX_DISTANCE)]
self._sprites = create_sprites(self._menu.chosen_level)
self._stats = Stats()
self._player = Player(TILE * 2 - TILE // 2, TILE * 2 - TILE // 2, self._weapons, self._sprites, self._stats)
self._render = Render(self._screen, self._player, self._minimap_screen, self._sprites, self._menu.chosen_level)
self._running = True
self._is_game_end = False
create_map(self._menu.chosen_level)
create_minimap(self._menu.chosen_level)
pygame.init()
def _config(self):
pygame.display.set_caption(self._caption)
pygame.mouse.set_visible(False)
def _respawn_arcade_sprites(self):
self._menu.arcade_class.spawn(SPRITES_COUNT_TO_SPAWN_IN_ARCADE)
sprites = create_sprites(self._menu.arcade_class.get_map())
self._render.sprites = sprites
self._player.sprites = sprites
self._sprites = sprites
def _update(self):
while self._running:
self._screen.fill(SKYBLUE)
self._clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.run()
if self._is_game_end:
if event.type in [pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN]:
self._running = False
if event.type == pygame.MOUSEBUTTONDOWN and not self._is_game_end:
self._player.on_mouse_down(event)
if self._is_all_arcade_sprites_killed():
self._respawn_arcade_sprites()
self._is_game_end = self.is_game_end()
if self._is_game_end:
total_time = self._stats.total_time()
kills_count = self._stats.get_kills()
if is_game_over(self._player):
self._player.dead()
self._losing.run(total_time, kills_count)
self.run()
elif is_win(self._sprites):
self._win.run(total_time, kills_count)
self.run()
else:
self._player.update()
self._render.render()
sprites_update(self._sprites, self._player)
show_info(self._screen, self._player)
pygame.display.set_caption('FPS: ' + str(int(self._clock.get_fps())))
pygame.display.flip()
def _is_all_arcade_sprites_killed(self):
return self._menu.mode == Modes.ARCADE and is_win(self._sprites)
def is_game_end(self):
return is_game_over(self._player) or is_win(self._sprites)
@staticmethod
def _finish():
pygame.quit()
@staticmethod
def _play_theme():
theme = Music()
theme.play_music()
if __name__ == '__main__':
game = Game()
game.run()