-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminigame.py
More file actions
319 lines (257 loc) · 11.8 KB
/
minigame.py
File metadata and controls
319 lines (257 loc) · 11.8 KB
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
import pygame
import random
import sys
pygame.init()
# ─── Configurações da tela ───────────────────────────────────────────────────
LARGURA, ALTURA = 800, 600
tela = pygame.display.set_mode((LARGURA, ALTURA))
pygame.display.set_caption("Mini-Game — Ports & Adapters Crew")
relogio = pygame.time.Clock()
# ─── Cores ───────────────────────────────────────────────────────────────────
PRETO = (15, 15, 20)
BRANCO = (240, 240, 240)
AZUL_JOGADOR= (70, 130, 220)
VERMELHO = (210, 50, 50)
LARANJA = (230, 130, 30)
ROXO = (160, 70, 210)
AMARELO = (230, 200, 40)
VERDE = (60, 180, 80)
CINZA = (120, 120, 130)
CIANO = (60, 200, 200)
# ─── Fontes ──────────────────────────────────────────────────────────────────
fonte_hud = pygame.font.SysFont("monospace", 22, bold=True)
fonte_grande= pygame.font.SysFont("monospace", 42, bold=True)
fonte_media = pygame.font.SysFont("monospace", 28, bold=True)
# ─── Configuração de Níveis (Nível 3) ────────────────────────────────────────
NIVEIS = {
1: {"vel_inimigo": 2, "qtd_inimigos": 5, "pontos_prox": 500},
2: {"vel_inimigo": 3, "qtd_inimigos": 7, "pontos_prox": 1000},
3: {"vel_inimigo": 4, "qtd_inimigos": 10, "pontos_prox": float("inf")},
}
# ─── Classes ─────────────────────────────────────────────────────────────────
class Jogador:
def __init__(self):
self.rect = pygame.Rect(LARGURA // 2 - 25, ALTURA - 70, 50, 50)
self.vel = 5
self.vidas = 5 # Nível 1: 5 vidas iniciais
self.cor = AZUL_JOGADOR
def mover(self, teclas):
if teclas[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.vel
if teclas[pygame.K_RIGHT] and self.rect.right < LARGURA:
self.rect.x += self.vel
if teclas[pygame.K_UP] and self.rect.top > 0:
self.rect.y -= self.vel
if teclas[pygame.K_DOWN] and self.rect.bottom< ALTURA:
self.rect.y += self.vel
def desenhar(self, surf):
pygame.draw.rect(surf, self.cor, self.rect, border_radius=8)
# detalhe — cockpit
pygame.draw.rect(surf, CIANO,
pygame.Rect(self.rect.x + 15, self.rect.y + 8, 20, 14),
border_radius=4)
class Inimigo:
"""Inimigo base — Nível 2: 3 pontos de vida."""
LARGURA = 50
ALTURA_I = 50
VIDA_MAX = 3
def __init__(self, vel):
x = random.randint(0, LARGURA - self.LARGURA)
self.rect = pygame.Rect(x, -self.ALTURA_I, self.LARGURA, self.ALTURA_I)
self.vel = vel
self.vida = self.VIDA_MAX
self.cor = VERMELHO
def atualizar(self):
self.rect.y += self.vel
def fora_da_tela(self):
return self.rect.top > ALTURA
def desenhar(self, surf):
pygame.draw.rect(surf, self.cor, self.rect, border_radius=6)
# barra de vida
proporcao = self.vida / self.VIDA_MAX
barra_w = self.rect.width
pygame.draw.rect(surf, CINZA,
(self.rect.x, self.rect.y - 10, barra_w, 6))
pygame.draw.rect(surf, VERDE,
(self.rect.x, self.rect.y - 10, int(barra_w * proporcao), 6))
class EinimigoRapido(Inimigo):
"""Nível 4: velocidade dobrada, cor diferente."""
VIDA_MAX = 3
def __init__(self, vel):
super().__init__(vel * 2) # velocidade dobrada
self.cor = LARANJA
class EinimigoGigante(Inimigo):
"""Nível 4: tamanho 80×80, 5 de vida."""
LARGURA = 80
ALTURA_I = 80
VIDA_MAX = 5
def __init__(self, vel):
super().__init__(vel)
x = random.randint(0, LARGURA - self.LARGURA)
self.rect = pygame.Rect(x, -self.ALTURA_I, self.LARGURA, self.ALTURA_I)
self.vida = self.VIDA_MAX
self.cor = ROXO
class Projetil:
"""Nível 2: projétil disparado com SPACE."""
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 8, 20)
self.vel = -10
self.cor = AMARELO
def atualizar(self):
self.rect.y += self.vel
def fora_da_tela(self):
return self.rect.bottom < 0
def desenhar(self, surf):
pygame.draw.rect(surf, self.cor, self.rect, border_radius=4)
# ─── Funções auxiliares ───────────────────────────────────────────────────────
def gerar_inimigos_nivel4(qtd, vel):
"""Gera lista heterogênea de inimigos para o Nível 4."""
inimigos = []
for _ in range(qtd):
sorteio = random.random()
if sorteio < 0.40:
inimigos.append(EinimigoRapido(vel))
elif sorteio < 0.70:
inimigos.append(EinimigoGigante(vel))
else:
inimigos.append(Inimigo(vel))
return inimigos
def spawn_inimigo(nivel_cfg, usar_nivel4=False):
vel = nivel_cfg["vel_inimigo"]
if usar_nivel4:
sorteio = random.random()
if sorteio < 0.40:
return EinimigoRapido(vel)
elif sorteio < 0.70:
return EinimigoGigante(vel)
return Inimigo(vel)
def desenhar_hud(surf, jogador, pontos, nivel):
# Pontos
txt_pontos = fonte_hud.render(f"Pontos: {pontos}", True, BRANCO)
surf.blit(txt_pontos, (10, 10))
# Nível
txt_nivel = fonte_hud.render(f"Nivel: {nivel}", True, CIANO)
surf.blit(txt_nivel, (10, 38))
# Vidas como texto (Nível 1)
coracoes = "♥ " * jogador.vidas
txt_vidas = fonte_hud.render(f"Vidas: {coracoes}", True, (220, 60, 80))
surf.blit(txt_vidas, (10, 66))
# Controles (canto direito)
controles = fonte_hud.render("SETAS mover | SPACE atirar", True, CINZA)
surf.blit(controles, (LARGURA - controles.get_width() - 10, 10))
def exibir_mensagem_nivel(surf, nivel):
"""Mostra mensagem de avanço de nível por 2 segundos (Nível 3)."""
overlay = pygame.Surface((LARGURA, ALTURA), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 140))
surf.blit(overlay, (0, 0))
txt1 = fonte_grande.render(f"NIVEL {nivel}!", True, AMARELO)
txt2 = fonte_media.render("Velocidade aumentou!", True, BRANCO)
surf.blit(txt1, (LARGURA // 2 - txt1.get_width() // 2, ALTURA // 2 - 50))
surf.blit(txt2, (LARGURA // 2 - txt2.get_width() // 2, ALTURA // 2 + 20))
pygame.display.flip()
pygame.time.delay(2000)
def tela_game_over(surf, pontos):
surf.fill(PRETO)
txt1 = fonte_grande.render("GAME OVER", True, VERMELHO)
txt2 = fonte_media.render(f"Pontuacao final: {pontos}", True, BRANCO)
txt3 = fonte_hud.render("Pressione R para jogar novamente ou Q para sair", True, CINZA)
surf.blit(txt1, (LARGURA // 2 - txt1.get_width() // 2, 180))
surf.blit(txt2, (LARGURA // 2 - txt2.get_width() // 2, 270))
surf.blit(txt3, (LARGURA // 2 - txt3.get_width() // 2, 360))
pygame.display.flip()
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit(); sys.exit()
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_r:
return True
if ev.key == pygame.K_q:
pygame.quit(); sys.exit()
# ─── Loop principal ───────────────────────────────────────────────────────────
def jogo():
jogador = Jogador()
inimigos = []
projeteis = []
pontos = 0
nivel = 1
timer_spawn = 0
intervalo_spawn = 80 # frames entre spawns
while True:
relogio.tick(60)
teclas = pygame.key.get_pressed()
# ── Eventos ──────────────────────────────────────────────────────────
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit(); sys.exit()
# Disparo com SPACE (Nível 2)
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_SPACE:
px = jogador.rect.centerx - 4
py = jogador.rect.top
projeteis.append(Projetil(px, py))
# ── Lógica ───────────────────────────────────────────────────────────
jogador.mover(teclas)
# Spawn de inimigos
timer_spawn += 1
if timer_spawn >= intervalo_spawn:
timer_spawn = 0
usar_n4 = (nivel == 3) # no nível máximo usa Nível 4 classes
cfg = NIVEIS[nivel]
inimigos.append(spawn_inimigo(cfg, usar_nivel4=usar_n4))
# Atualizar inimigos
for ini in inimigos[:]:
ini.atualizar()
if ini.fora_da_tela():
inimigos.remove(ini)
jogador.vidas -= 1
if jogador.vidas <= 0:
tela_game_over(tela, pontos)
return # reinicia chamando jogo() de novo via main
# Atualizar projéteis
for proj in projeteis[:]:
proj.atualizar()
if proj.fora_da_tela():
projeteis.remove(proj)
continue
# Colisão projétil × inimigo (Nível 2)
for ini in inimigos[:]:
if proj.rect.colliderect(ini.rect):
ini.vida -= 1
if proj in projeteis:
projeteis.remove(proj)
if ini.vida <= 0:
inimigos.remove(ini)
pontos += 100
break
# Colisão jogador × inimigo
for ini in inimigos[:]:
if jogador.rect.colliderect(ini.rect):
inimigos.remove(ini)
jogador.vidas -= 1
if jogador.vidas <= 0:
reiniciar = tela_game_over(tela, pontos)
if reiniciar:
return # volta ao main loop que chama jogo() novamente
# Progressão de nível (Nível 3) — a cada 500 pontos
nivel_esperado = min(3, pontos // 500 + 1)
if nivel_esperado > nivel:
nivel = nivel_esperado
exibir_mensagem_nivel(tela, nivel)
# Ajusta intervalo de spawn conforme nível
intervalo_spawn = max(30, 80 - (nivel - 1) * 20)
# ── Desenho ──────────────────────────────────────────────────────────
tela.fill(PRETO)
# Grade de fundo sutil
for gx in range(0, LARGURA, 60):
pygame.draw.line(tela, (25, 25, 35), (gx, 0), (gx, ALTURA))
for gy in range(0, ALTURA, 60):
pygame.draw.line(tela, (25, 25, 35), (0, gy), (LARGURA, gy))
jogador.desenhar(tela)
for ini in inimigos: ini.desenhar(tela)
for proj in projeteis: proj.desenhar(tela)
desenhar_hud(tela, jogador, pontos, nivel)
pygame.display.flip()
# ─── Entry point ─────────────────────────────────────────────────────────────
if __name__ == "__main__":
while True:
jogo()