-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
290 lines (247 loc) · 13.1 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
"""
Point d'entrée dans le programme
Ce module est divisé en trois partie
* Importation des modules externes puis internes
* Initialisation
* Boucle Principale
La boucle principale est elle-même composé des éléments suivants
* Affichage de la map
* Capture et réactions des events
* Affichage des sprites
* Vidage du stack des explosions
* Execution des mouvements
* Gestions des worms
* Flip de l'écran
* Calcul du framerate
"""
import pygame
from entity import Entity
from map import TileMap
from debug_utils import get_time_incache
from menu import Button, Menu, Text
from CONSTS import FRAMERATE, WINDOW_SIZE, GAME_NAME, PLAYERS, SPAWN_POINTS, COLORS, DEBUG
from physics import move_entities, def_map
from weapon import Bazooka, Sniper, Grenade, GrenadeFrag, Fleche, ChargeBar
from random import randint, choice
from math import pi
from utils import get_circle
from worms import Worm
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption(GAME_NAME)
# flags = pygame.FULLSCREEN | pygame.HWSURFACE
self.SCREEN = pygame.display.set_mode((WINDOW_SIZE[0], WINDOW_SIZE[1])) # , flags)
self.active_menu: Menu = Menu("Loading", WINDOW_SIZE[0], WINDOW_SIZE[1],
pygame.image.load("assets/textures/menu/backgrounds/loading.png").convert_alpha())
self.active_menu.draw(self.SCREEN)
pygame.display.flip()
self.path = "assets/map/01.map"
self.map = TileMap(self.path)
def_map(self.map)
self.Oclock = pygame.time.Clock()
self.tick = 0
self.fps = 0
self.map.blit_texture(all_pxs=True)
self.all_sprites: set[Entity] = set()
self.current_player = None
self.players: list[Worm] = []
self.running = False
self.pause = False
self.charg_bar = ChargeBar(self, (self.map.dimensions[0] // 2) - 30, (self.map.dimensions[1] // 2) - 60)
self.fleche = Fleche(self, (self.map.dimensions[0] // 2) - 25, (self.map.dimensions[1] // 2) - 20, 0, 50)
self.current_weapon = -1
self.weapon = None
self.inclinaison = 0.0
self.MAIN_MENU: Menu = None
self.PAUSE_MENU: Menu = None
self.END_MENU: Menu = None
self.load_menus()
self.generate_players()
def generate_players(self):
for i in range(PLAYERS):
x, y = SPAWN_POINTS[i % 6]
self.players.append(Worm(self, COLORS[i % 12], x, y))
self.current_player = 0
def load_menus(self):
title_font = pygame.font.SysFont("Showcard Gothic", 120, False, False)
small_title_font = pygame.font.SysFont("Showcard Gothic", 80, False, False)
resume_button = Button(self.SCREEN.get_width() // 2 - 105, self.SCREEN.get_height() // 2 - 44 - 100,
pygame.image.load("assets/textures/menu/buttons/resume.png").convert_alpha(), 1,
self.exit_menu)
options_button = Button(self.SCREEN.get_width() // 2 - 105, self.SCREEN.get_height() // 2 - 44,
pygame.image.load("assets/textures/menu/buttons/options.png").convert_alpha(), 1,
lambda: print("No Options set"))
quit_button = Button(self.SCREEN.get_width() // 2 - 105, self.SCREEN.get_height() // 2 - 44 + 100,
pygame.image.load("assets/textures/menu/buttons/quit.png").convert_alpha(), 1,
self.exit_game)
play_button = Button(self.SCREEN.get_width() // 2 - 210, self.SCREEN.get_height() // 2 - 44 + 200,
pygame.image.load("assets/textures/menu/buttons/play.png").convert_alpha(), 2,
self.exit_menu)
pause_text = Text(self.SCREEN.get_width() // 2 - 125, self.SCREEN.get_height() // 2 - 250, "Pause",
small_title_font, True)
title_text = Text(self.SCREEN.get_width() // 2 - 300, self.SCREEN.get_height() // 2 - 350, GAME_NAME,
title_font, True)
quit_button2 = Button(self.SCREEN.get_width() - 350, self.SCREEN.get_height() - 150,
pygame.image.load("assets/textures/menu/buttons/quit.png").convert_alpha(), 0.75,
self.exit_game)
self.PAUSE_MENU = Menu("Pause", WINDOW_SIZE[0], WINDOW_SIZE[1],
pygame.image.load("assets/textures/menu/backgrounds/pause.png").convert_alpha(),
[resume_button, options_button, quit_button], [pause_text])
self.MAIN_MENU = Menu("Main", WINDOW_SIZE[0], WINDOW_SIZE[1],
pygame.image.load("assets/textures/menu/backgrounds/main.png").convert_alpha(),
[play_button, options_button, quit_button], [title_text])
self.END_MENU = Menu("Fin", WINDOW_SIZE[0], WINDOW_SIZE[1],
pygame.image.load("assets/textures/menu/backgrounds/end.png").convert_alpha(),
[quit_button2], [])
def exit_game(self):
self.running = False
def set_active_menu(self, menu: Menu):
self.active_menu = menu
self.active_menu.draw(self.SCREEN)
def exit_menu(self):
self.active_menu = None
def get_current_player(self) -> Worm:
return self.players[self.current_player]
def next_player(self):
self.current_player += 1
self.current_player %= len(self.players)
def next_weapon(self):
if self.weapon is not None:
self.weapon.kill()
self.current_weapon += 1
self.current_weapon %= 4
match self.current_weapon:
case 0:
self.weapon = Bazooka(self, self.get_current_player().x, self.get_current_player().y,
self.inclinaison)
case 1:
self.weapon = Sniper(self, self.get_current_player().x, self.get_current_player().y,
self.inclinaison)
case 2:
self.weapon = Grenade(self, self.get_current_player().x, self.get_current_player().y,
self.inclinaison)
case 3:
self.weapon = GrenadeFrag(self, self.get_current_player().x, self.get_current_player().y,
self.inclinaison)
def run(self):
self.running = True
self.set_active_menu(self.MAIN_MENU)
power = 0.1
get_axis = 0
self.next_weapon()
# ----------------- Boucle principale -------------------
while self.running:
if self.active_menu is not None:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
self.active_menu.on_click(pygame.mouse.get_pos())
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DELETE:
self.exit_game()
else:
self.map.blit_texture()
self.SCREEN.blit(self.map.Surf, (0, 0))
# ------------- Gestion des événements -------------------
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DELETE:
self.exit_game()
if event.key == pygame.K_ESCAPE or event.key == pygame.K_p:
self.set_active_menu(self.PAUSE_MENU)
if event.key == pygame.K_e:
self.next_weapon()
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
if event.key == pygame.K_DOWN:
get_axis = -1
else:
get_axis = 1
elif event.key == pygame.K_SPACE:
if self.current_weapon == 1 and not self.map[self.weapon.x + 25, self.weapon.y + 25]:
self.weapon.shoot(power, self.inclinaison)
self.next_player()
if self.current_weapon == 0 or self.current_weapon == 2 or self.current_weapon == 3:
power = -self.tick * 0.015
self.charg_bar.charging = True
self.charg_bar.up_taille(10, self.get_current_player().x, self.get_current_player().y)
if DEBUG:
if event.key == pygame.K_m:
self.next_player()
if event.key == pygame.K_o:
get_time_incache()
print("Available :", self.map.available_ONMAPs)
print("To clean :", self.map.clear_ONMAPs)
if event.type == pygame.KEYUP:
self.charg_bar.charging = False
if event.key == pygame.K_SPACE:
power += self.tick * 0.015 + 0.2
if power > 1:
power = 1
if self.current_weapon in (0, 2, 3) and not self.map[
self.weapon.x + 25, self.weapon.y + 25]:
self.weapon.shoot(power, self.inclinaison)
self.next_player()
power = 0.2
self.charg_bar.reset_taille()
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
if event.key == pygame.K_DOWN:
get_axis = 0
else:
get_axis = 0
if event.type == pygame.MOUSEBUTTONDOWN and DEBUG:
print("Mouse point @", pygame.mouse.get_pos())
circle = get_circle(5, pos := pygame.mouse.get_pos(), radius := 40)
self.map.destruction_stack.append((pygame.mouse.get_pos(), 40.0))
self.map.destruction_stack.extend([(circle[i], 20.0) for i in range(len(circle))])
if event.type == pygame.QUIT:
self.exit_game()
# ------------- Affichage des Entités --------------------
for sprite in self.all_sprites:
sprite.draw(self.SCREEN)
# ------------- Gestion des gros joujoux -----------------
if self.charg_bar.charging:
charge = power + self.tick * 0.015 + 0.2
if charge < 1:
self.charg_bar.up_taille(1.5, self.get_current_player().x, self.get_current_player().y)
if get_axis != 0:
if get_axis == -1:
self.inclinaison += 0.030
else:
self.inclinaison -= 0.030
self.weapon.move_with_rota(self.inclinaison, self.get_current_player().x, self.get_current_player().y)
self.fleche.move_with_rota(self.inclinaison, self.get_current_player().x, self.get_current_player().y)
self.charg_bar.moove_bar(self.get_current_player().x, self.get_current_player().y)
# ------------- Execution des explosions -----------------
self.map.void_destruction_stack()
# ------------- Déplacement des Entités ------------------
move_entities()
# ---------------------- Fin du jeu ----------------------
if len(self.players) <= 1:
font = pygame.font.SysFont("Showcard Gothic", 80, False, False)
self.END_MENU.add_text(
Text(self.SCREEN.get_width() // 2 - 160, self.SCREEN.get_height() - 200, "Victoire !", font,
color=self.get_current_player().color))
self.set_active_menu(self.END_MENU)
continue
# ------------- Mouvement des joueurs --------------------
x_movement = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_q]:
x_movement = -2
if keys[pygame.K_d]:
x_movement = 2
self.get_current_player().move_worm(x_movement, 0, self.map.map)
if keys[pygame.K_z]:
self.players[self.current_player].jump_worm(self.tick, self.map.map)
for i in range(len(self.players)):
if i != self.current_player:
self.players[i].move_worm(0, 0, self.map.map)
# self.get_current_player().draw(SCREEN)
# -------------- Actualisation pygame --------------------
self.Oclock.tick(FRAMERATE)
self.tick += 1
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
theGame = Game()
theGame.run()