-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
274 lines (214 loc) · 7.79 KB
/
setup.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
import pygame
import pygame_menu.events
import pygame_menu.font
import pygame_menu.locals
import pygame_menu.themes
import pygame_menu
import sys
from src.utils.scale_image import scale_image
import pygame_menu.widgets
from src.utils.health_bar import HealthBar
from src.utils.paused import paused
from src.utils.victory import Victory
from src.utils.lose import lose
from src.utils.inactive_animation import inactive_animation
from src.utils.clock import clock_timer
from src.utils.draw_scrolling_background import draw_scrolling_background
from src.audios.audios import Audios
from src.config.screen import Screen
from src.vectors.alien.handle_alien_attack import handle_alien_attack
from src.vectors.alien.handle_alien_bullets import handle_alien_bullets
from src.vectors.plane.handle_plane_bullets import handle_plane_bullets
from src.vectors.plane.plane import Plane
from src.vectors.alien.alien import Alien
pygame.init()
screen = Screen(
540,
740,
)
start_time = pygame.time.get_ticks()
total_pause_duration = 0
running = True
def game():
global running
global start_time
global total_pause_duration
clock = pygame.time.Clock()
dt = 0
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
bullets = []
ANIMATION_INTERVAL = 0.025
INACTIVITY_THRESHOLD = 0.02
last_animation_time = pygame.time.get_ticks()
alien = Alien()
alien.rect.center = (player_pos.x, player_pos.y - 300)
health_bar_alien = HealthBar(x=0, y=0, w=540, h=10, hp=alien.hp, max_hp=100)
plane = Plane()
plane.rect.center = (player_pos.x, player_pos.y + 300)
health_bar_plane = HealthBar(
x=0,
y=screen.get_height() / 2.20,
w=10,
h=screen.get_height() / 2.25,
hp=plane.hp,
max_hp=100,
)
explosion_sprites = pygame.sprite.Group()
sprite_sheet_explosion = pygame.image.load("assets/Explosion.png")
sprite_sheet_explosion = sprite_sheet_explosion.convert_alpha()
sprite_sheet_bullet = pygame.image.load("assets/bullets/bullet-r.png")
sprite_sheet_bullet = sprite_sheet_bullet.convert_alpha()
sprite_sheet_fireball = pygame.image.load("assets/alien_shot/Fireball-All.png")
sprite_sheet_fireball = sprite_sheet_fireball.convert_alpha()
sprite_sheet_laser = pygame.image.load("assets/alien_shot/Laser/laser.png")
sprite_sheet_laser = sprite_sheet_laser.convert_alpha()
background = pygame.image.load("assets/scene/background.png")
background = scale_image(background, 2)
background = background.convert_alpha()
background_speed = 200
background_positions = [0, -screen.get_height()]
audios = Audios()
theme = pygame.mixer.music
theme.load("assets/audio/theme.wav")
theme.play(-1)
theme.set_volume(0.09)
boos_music = pygame.mixer.music
win = True
lose_playning = False
play_again_label = None
left_alien_shot = False
plane_prop_audio = audios.plane_prop
plane_prop_audio.play(-1).set_volume(0.25)
while running:
for event in pygame.event.get():
if lose_playning:
pygame.mixer.music.stop()
boos_music.load("assets/audio/boos-loop.wav")
boos_music.play(-1)
lose_playning = not lose_playning
if event.type == pygame.QUIT:
plane_prop_audio.stop()
theme.stop()
running = False
pygame.quit()
sys.exit()
if not win:
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]:
mouse_pos = pygame.mouse.get_pos()
mouse_rect = pygame.Rect(mouse_pos[0], mouse_pos[1], 1, 1)
if play_again_label.rect.colliderect(mouse_rect):
game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game()
if not plane.alive:
plane_prop_audio.stop()
dt = clock.tick(60) / 1000
explosion_sprites.update()
draw_scrolling_background(
screen, background, background_speed, dt, background_positions
)
explosion_sprites.draw(screen)
keys = pygame.key.get_pressed()
current_time = (pygame.time.get_ticks() - start_time) / 1000
clock_timer(current_time, screen)
if plane.alive:
plane.plane_move(
dt=dt,
keys=keys,
bullets=bullets,
max_x=screen.get_width() - 40,
max_y=screen.get_height() - 60,
victory=not alien.live,
sprite_sheet=sprite_sheet_bullet,
)
(plane.current_sprite, plane, last_animation_time) = inactive_animation(
INACTIVITY_THRESHOLD=INACTIVITY_THRESHOLD,
ticks=pygame.time.get_ticks(),
last_animation_time=last_animation_time,
ANIMATION_INTERVAL=ANIMATION_INTERVAL,
last_move_time=plane.last_move_time,
current_sprite=plane.current_sprite,
plane=plane,
)
handle_plane_bullets(
plane,
alien,
screen,
dt,
plane.bullets,
sprite_sheet_explosion,
explosion_sprites,
health_bar_alien,
audios,
)
health_bar_plane.drawY(screen, "red", "white")
if alien.live:
if alien.hp < 95:
alien.fury = True
left_alien_shot = handle_alien_attack(
alien,
plane,
lose_playning,
left_alien_shot,
sprite_sheet_laser,
sprite_sheet_fireball,
)
handle_alien_bullets(
alien,
plane,
screen,
dt,
lose_playning,
theme,
audios,
boos_music,
health_bar_plane,
sprite_sheet_explosion,
explosion_sprites,
)
alien.alien_move(dt, screen.get_width())
screen.blit(alien.image, alien.rect)
health_bar_alien.drawX(screen, "red", "green")
else:
alien.rect.y = 3000
alien.kill()
screen.blit(plane.image, plane.rect)
if keys[pygame.K_p]:
pause_duration = paused(pygame, screen, clock, keys)
total_pause_duration += pause_duration
if not alien.live:
plane_prop_audio.stop()
theme.stop()
win = Victory(screen=screen, win=win)
if not plane.alive:
win = False
lose_playning = True
play_again_label = lose(screen=screen)
pygame.display.flip()
font_menu_buttons = pygame_menu.font.FONT_DIGITAL
theme = pygame_menu.Theme(
background_color=(0, 0, 0, 0), # transparent background
title_background_color=(4, 47, 126),
title_font_shadow=True,
widget_padding=25,
title_bar_style=pygame_menu.widgets.MENUBAR_STYLE_NONE,
widget_alignment=pygame_menu.locals.ALIGN_CENTER,
widget_font=font_menu_buttons,
title_font=font_menu_buttons,
title_font_size=20,
)
menu = pygame_menu.Menu(
"Barão Vermelhão VS LELIGENA",
screen.get_width(),
screen.get_height(),
theme=theme,
)
# menu.add.selector("Difficulty :", [("Hard", 1), ("Easy", 2)], onchange=set_difficulty)
menu.add.button("Play", game)
menu.add.button("Quit", pygame_menu.events.EXIT)
if not running:
pygame_menu.events.EXIT
menu.mainloop(screen)
pygame.quit()