-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlv_2_advanced.py
343 lines (299 loc) · 17.4 KB
/
lv_2_advanced.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
import pygame
import sys
from player import Player
from enemy_purple import Enemy
from button import Button
from contador import tiempo
import random
from progress import set_current_level
from Localization_manager import localization
from controls import ControlsScreen
class Level2:
def __init__(self, state_manager):
# Datos de pantalla
self.screen = pygame.display.set_mode((1280, 720)) # Creamos la ventana con sus medidas
self.clock = pygame.time.Clock() # Reloj para controlar los FPS
self.state_manager = state_manager
# Mostrar pantalla de controles antes de iniciar el nivel
controls_screen = ControlsScreen(self.state_manager)
controls_screen.show_controls_screen()
self.TILE_SIZE = 32
self.character_index = self.state_manager.get_selected_character()
if self.character_index is None:
print("Error: No character selected")
self.character_index = 0
else:
print(f"Selected character index: {self.character_index}")
# Obtener la dificultad del state_manager
self.difficulty = self.state_manager.get_difficulty()
print(f"Level1 initialized with difficulty: {self.difficulty}") # Añade esta línea para depurar
self.player = Player(400, 400, self.character_index, self.difficulty)
self.paused = False
self.keys_pressed = None
self.timer = tiempo()
self.start_time = pygame.time.get_ticks()
self.time_left = 100
self.all_bubbles = pygame.sprite.Group()
self.all_enemies = pygame.sprite.Group()
self.score = 0
self.lose_sound = pygame.mixer.Sound("assets/sounds/perder.mp3")
self.select_sound = pygame.mixer.Sound("assets/sounds/select.mp3")
self.enemy_hurt_sound = pygame.mixer.Sound("assets/sounds/enemy_hurt.mp3")
self.win_sound = pygame.mixer.Sound("assets/sounds/victoria.mp3")
# Ajustar puntos por enemigo según la dificultad
if self.difficulty == "Beginner":
self.points_per_enemy = 5
elif self.difficulty == "Advanced":
self.points_per_enemy = 15
# Posiciones iniciales de los enemigos
self.enemy_positions = [
(900, 400),
(960, 560),
(1060, 450),
(1060, 500),
(800, 500),
(860, 400),
(960, 460),
(1060, 500)
]
self.create_enemies()
self.enemy_count = len(self.all_enemies)
# Creamos el mapa de obstáculos (1 = obstáculo, 0 = espacio libre)
self.map_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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0, 0, 1, 0, 0, 1, 0],
[1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# Convertimos el mapa en una lista de rectángulos que representan las colisiones
self.obstacles = [] # Esta es la lista donde almacenamoslos obstaculos del mapa
for row_idx, row in enumerate(self.map_data): # Recorre cada fila del mapa y la indexa
for col_idx, cell in enumerate(row): # Recorre cada columna de la fila
if cell == 1: # Si la celda es igual a 1 (osea un obstaculo) crae un rectangulo
obstacle_rect = pygame.Rect(col_idx * self.TILE_SIZE, row_idx * self.TILE_SIZE, self.TILE_SIZE, self.TILE_SIZE) # Crea el rectangulo para el obstaculo
self.obstacles.append(obstacle_rect) # Con esto añadimos el rectangulo a la lista
# Carga de sonidos
self.enemy_sound = pygame.mixer.Sound("assets/sounds/hit_hurt-3.mp3")
# Carga de recursos
self.background = pygame.image.load("assets/sprites/level2.png")
self.pause_image = pygame.image.load("assets/sprites/pauseButton.png")
self.font = pygame.font.Font("font.ttf", 35)
self.font2 = pygame.font.Font("assets/fonts/SCREEN.TTF", 25)
self.fondo1_1= pygame.image.load("assets/sprites/PANTALLASELECCIONPERSONAJE1.png")
self.botonR_1 = pygame.image.load("assets/sprites/boton_crditos1.png")
self.botonS_1 = pygame.image.load("assets/sprites/boton_crditos1.png")
self.reloj = pygame.image.load("assets/sprites/reloj.png")
self.viruscount = pygame.image.load("assets/sprites/virus-count.png")
self.score_image = pygame.image.load("assets/sprites/score.png")
# Escalar los recursos
self.fondo1_1 = pygame.transform.scale(self.fondo1_1, (1280, 720 ))
self.botonR_1 = pygame.transform.scale(self.botonR_1, (370, 250))
self.botonS_1 = pygame.transform.scale(self.botonS_1, (370, 250))
self.viruscount = pygame.transform.scale(self.viruscount, (154, 54))
self.reloj = pygame.transform.scale(self.reloj, (154, 54))
self.score_image = pygame.transform.scale(self.score_image, (154, 54))
# Crear botones
self.pause_button = Button(self.pause_image, (640, 40), "", self.get_font(25), "black", "Green")
self.resume_button = Button(self.botonR_1,(642, 250), "", self.get_font(15), "Black", "Green")
self.go_out_button = Button(self.botonS_1,(642, 370), "", self.get_font(15), "Black", "Green")
# Texto
self.texto1 = self.font.render("pause", True, "black")
self.texto1_rect = self.texto1.get_rect(center = (642, 130))
self.resume_texto = self.font.render("resume", True, "white")
self.resume_texto_rect = self.resume_texto.get_rect(center = (648,250))
self.go_out_texto = self.font.render("go out", True, "white")
self.go_out_texto_rect = self.go_out_texto.get_rect(center = (690, 370))
def create_enemies(self):
self.all_enemies.empty() # Vacía el grupo de enemigos
for pos in self.enemy_positions:
enemy = Enemy(pos[0], pos[1])
self.all_enemies.add(enemy)
self.enemy_count = len(self.all_enemies)
self.all_enemies.add(enemy)
def get_font(self, size):
return pygame.font.Font("assets/fonts/GAME.TTF", size)
def draw_overlay(self):
overlay = pygame.Surface((1280, 720))
overlay.fill((0, 0, 0))
overlay.set_alpha(128)
self.screen.blit(overlay, (0, 0))
self.screen.blit(self.fondo1_1, (0, 0))
self.resume_button.update(self.screen)
self.go_out_button.update(self.screen)
self.screen.blit(self.texto1, self.texto1_rect)
self.screen.blit(self.resume_texto, self.resume_texto_rect)
self.screen.blit(self.go_out_texto, self.go_out_texto_rect)
self.pause_button.update(self.screen)
pygame.display.flip()
def update_text(self):
self.texto1 = self.get_font(40).render(localization.get_text("pause"), True,"black")
self.resume_texto = self.get_font(30).render(localization.get_text("resume"), True,"white")
self.go_out_texto = self.get_font(30).render(localization.get_text("go out"), True,"white")
def update(self):
self.check_collision()
if not self.paused:
elapsed_time = (pygame.time.get_ticks() - self.start_time) // 1000
self.time_left = max(0, 100 - elapsed_time)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.pause_button.checkForInput(pygame.mouse.get_pos()):
self.paused = True
self.pause_start_time = pygame.time.get_ticks()
self.select_sound.play()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.paused = True
self.pause_start_time = pygame.time.get_ticks()
elif event.key == pygame.K_w:
self.player.move('UP', self.obstacles)
elif event.key == pygame.K_s:
self.player.move('DOWN', self.obstacles)
elif event.key == pygame.K_a:
self.player.move('LEFT', self.obstacles)
elif event.key == pygame.K_d:
self.player.move('RIGHT', self.obstacles)
elif event.key == pygame.K_z:
self.player.change_health()
elif event.key == pygame.K_SPACE:
self.player.shoot(self.all_bubbles, self.difficulty)
elif event.key == pygame.K_l:
print(f"character_index: {self.player.character_index}")
self.update_text()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.player.move('UP', self.obstacles)
if keys[pygame.K_s]:
self.player.move('DOWN', self.obstacles)
if keys[pygame.K_a]:
self.player.move('LEFT', self.obstacles)
if keys[pygame.K_d]:
self.player.move('RIGHT', self.obstacles)
else:
self.player.snap_to_grid()
self.player.update()
self.check_collision()
self.check_player_enemy_collision()
for enemy in self.all_enemies:
enemy.update(self.player.rect, self.obstacles)
self.all_bubbles.update(self.obstacles, self.all_enemies, self)
pygame.display.flip()
if self.time_left == 0 or self.player.is_dead:
self.state_manager.set_state("lose_menu")
self.lose_sound.play()
if len(self.all_enemies) == 0:
self.state_manager.set_state("win_menu")
self.win_sound.play()
set_current_level(2)
if self.paused:
self.draw_overlay()
pygame.display.flip()
while self.paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.pause_button.checkForInput(pygame.mouse.get_pos()):
self.paused = False
self.start_time += pygame.time.get_ticks() - self.pause_start_time
self.select_sound.play()
elif self.resume_button.checkForInput(pygame.mouse.get_pos()):
self.paused = False
self.start_time += pygame.time.get_ticks() - self.pause_start_time
self.select_sound.play()
elif self.go_out_button.checkForInput(pygame.mouse.get_pos()):
self.paused = False
self.reset_game_state()
self.state_manager.set_state("levels")
self.select_sound.play()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.paused = False
self.start_time += pygame.time.get_ticks() - self.pause_start_time
elif event.key == pygame.K_p:
self.paused = False
self.reset_game_state()
self.state_manager.set_state("levels")
self.clock.tick(60)
pygame.display.flip()
self.clock.tick(60)
def check_player_enemy_collision(self):
for enemy in self.all_enemies:
if self.player.rect.colliderect(enemy.rect):
self.player.change_health(-1)
self.move_enemy_away(enemy)
self.enemy_sound.play()
def move_enemy_away(self, enemy):
max_attempts = 100 # Número máximo de intentos para encontrar una posición válida
attempts = 0
while attempts < max_attempts:
new_x = random.randint(0, self.screen.get_width() - enemy.rect.width)
new_y = random.randint(0, self.screen.get_height() - enemy.rect.height)
new_rect = pygame.Rect(new_x, new_y, enemy.rect.width, enemy.rect.height)
if not self.player.rect.colliderect(new_rect) and not any(obstacle.colliderect(new_rect) for obstacle in self.obstacles):
enemy.rect.topleft = (new_x, new_y)
enemy.get_random_direction() # Asignar un nuevo rumbo aleatorio
break
attempts += 1
else:
# Si no se encuentra una posición válida, mantener al enemigo en su posición actual
print("No se encontró una posición válida para alejar al enemigo.")
def reset_game_state(self):
self.player = Player(400, 400, self.character_index, self.difficulty)
self.paused = False
self.keys_pressed = None
self.timer = tiempo()
self.start_time = pygame.time.get_ticks()
self.time_left = 100
self.all_bubbles.empty()
self.create_enemies()
self.screen.fill((0, 0, 0)) # Limpiar la pantalla al resetear el estado
pygame.display.flip()
def check_collision(self):
for bubble in self.all_bubbles:
enemy_hit_list = pygame.sprite.spritecollide(bubble, self.all_enemies, True)
if enemy_hit_list:
bubble.kill()
self.enemy_hurt_sound.play()
for enemy in enemy_hit_list:
self.enemy_count -= 1
self.score += self.points_per_enemy
self.all_enemies.remove(enemy)
self.screen.blit(self.background, enemy.rect, enemy.rect)
def draw(self, screen):
self.screen.blit(self.background, (0, 0))
self.player.draw(screen)
self.all_enemies.draw(screen)
self.all_bubbles.draw(screen)
self.pause_button.update(screen)
self.screen.blit(self.reloj, (1120, 10))
self.screen.blit(self.viruscount, (1120, 64))
self.screen.blit(self.score_image, (1120, 118))
tiempo.draw_timer(screen, self.time_left)
self.enemy_count_text = self.font2.render(f"{self.enemy_count}", True, "black")
self.screen.blit(self.enemy_count_text, (1210, 80))
self.score_text = self.font2.render(f"{self.score}", True, "black")
self.screen.blit(self.score_text, (1210, 135))
#tiren paro