-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_1.py
173 lines (146 loc) · 6.24 KB
/
level_1.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
# Scripts:
from constants import *
from base_state import BaseState
from sound import boss_bg, win_bg, game_over_bg, win_level_bg
from bg_creator import BGCreator
from player import Player
from ui import UI
from enemies import Enemy
from boss import Boss
from items import HP, Life
from collisions import *
# Modules:
import pygame
import random
# Initialize Pygame:
pygame.init()
class Level1(BaseState):
def __init__(self):
super().__init__()
# Next State:
self.next_state = "GAME_OVER"
# Create Sprites Group:
self.player_group = pygame.sprite.Group()
self.player_bullets_group = pygame.sprite.Group()
self.enemies_group = pygame.sprite.Group()
self.bosses_group = pygame.sprite.Group()
self.enemies_bullets_group = pygame.sprite.Group()
self.hp_items_group = pygame.sprite.Group()
self.life_items_group = pygame.sprite.Group()
self.effects_group = pygame.sprite.Group()
# Initialize Objects:
self.background = BGCreator(*BACKGROUNDS['level_1'])
self.player = Player(*PLAYER, self.player_bullets_group, self.effects_group)
self.ui = UI(self.player)
# Add Player Sprites to group:
self.player_group.add(self.player) # type: ignore
# Define Number of Enemies to spawn in Level 1:
self.enemies = []
self.enemy_index = 0
for enemy in ENEMIES_LVL1:
self.enemies.append(ENEMIES[f'{enemy}'])
self.boss = [BOSSES['b']]
self.boss_to_spawn = True
# Define time delay between enemies to spawn:
self.time_to_spawn = 5000
self.enemy_event = pygame.USEREVENT + 0
pygame.time.set_timer(self.enemy_event, self.time_to_spawn)
# Level Music:
self.play_win_level_music = True
def handle_pause(self):
self.next_state = "PAUSE"
self.screen_done = True
pygame.mixer.music.pause()
def reset_enemy_timer(self, time):
self.time_to_spawn = time
pygame.time.set_timer(self.enemy_event, self.time_to_spawn)
def spawn_enemy(self):
if 0 <= self.enemy_index <= (len(self.enemies) - 1) * 1 / 3:
self.time_to_spawn = 3700
self.reset_enemy_timer(self.time_to_spawn)
elif (len(self.enemies) - 1) * 1 / 3 < self.enemy_index <= (len(self.enemies) - 1) * 2 / 3:
self.time_to_spawn = 2900
self.reset_enemy_timer(self.time_to_spawn)
elif (len(self.enemies) - 1) * 2 / 3 < self.enemy_index <= (len(self.enemies) - 1):
self.time_to_spawn = 1500
self.reset_enemy_timer(self.time_to_spawn)
k = self.enemies[self.enemy_index]
self.enemies_group.add(Enemy(*k, self.enemies_bullets_group, self.effects_group)) # type: ignore
self.enemy_index += 1
def spawn_boss(self):
k = self.boss[0]
self.enemies_group.add(Boss(*k, self.enemies_bullets_group, self.effects_group)) # type: ignore
self.boss_to_spawn = False
boss_bg.play_bg_music(-1, 6000)
def spawn_item_hp(self):
number = random.randint(1, 15)
if number == 1:
self.hp_items_group.add(HP(*ITEMS["hp"])) # type: ignore
def spawn_item_life(self):
self.life_items_group.add(Life(*ITEMS["life"])) # type: ignore
def handle_win(self):
if self.player.state == "winner":
self.next_state = "WIN"
self.screen_done = True
win_bg.play_bg_music(-1)
if self.player.state == "alive":
self.player.end_animation = True
if self.play_win_level_music:
win_level_bg.play_bg_music(0)
self.play_win_level_music = False
def handle_game_over(self):
if self.next_screen_rate >= self.next_screen_ref_time:
self.next_state = "GAME_OVER"
self.screen_done = True
game_over_bg.play_bg_music(-1)
else:
self.next_screen_rate += 1
def get_event(self, event):
if event.type == pygame.QUIT:
self.quit = True
elif event.type == pygame.KEYDOWN:
# Pause Menu Selection:
if event.key == pygame.K_ESCAPE:
self.handle_pause()
# Spawn Enemies According to Level + Extra HP:
if event.type == self.enemy_event and self.enemy_index < len(self.enemies):
self.spawn_enemy()
self.spawn_item_hp()
# Spawn Boss if there's no more enemies + Extra Life:
if self.enemy_index == len(self.enemies) and len(self.enemies_group) == 0 and self.boss_to_spawn:
self.spawn_boss()
self.spawn_item_life()
# Check Win Condition:
if not self.boss_to_spawn and len(self.enemies_group) == 0:
self.player.keyboard_blocked = True
self.handle_win()
# Check Game Over Condition:
if self.player.state == "dead":
self.handle_game_over()
def draw(self, surface):
# Check Collisions:
check_collision(self.player_bullets_group, self.enemies_group, True, False, "bullet") # Player Bullet vs Enemy
check_collision(self.enemies_bullets_group, self.player_group, True, False, "bullet") # Enemy Bullet vs Player
check_collision(self.enemies_group, self.player_group, False, False, "body") # Enemy Body vs Player Body
check_collision(self.hp_items_group, self.player_group, True, False, "hp item") # HP Item vs Player Body
check_collision(self.life_items_group, self.player_group, True, False, "life item") # Life Item vs Player Body
# Draw Background:
self.background.draw()
# Draw UI:
self.ui.draw()
# Update Sprites Group:
self.player_group.update()
self.player_bullets_group.update()
self.enemies_group.update()
self.enemies_bullets_group.update()
self.hp_items_group.update()
self.life_items_group.update()
self.effects_group.update()
# Draw Sprite Groups:
self.enemies_bullets_group.draw(SCREEN)
self.enemies_group.draw(SCREEN)
self.player_bullets_group.draw(SCREEN)
self.player_group.draw(SCREEN)
self.hp_items_group.draw(SCREEN)
self.life_items_group.draw(SCREEN)
self.effects_group.draw(SCREEN)