-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
683 lines (565 loc) · 24.3 KB
/
main.py
File metadata and controls
683 lines (565 loc) · 24.3 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import pygame
from sys import exit
from random import choice, randint
from os import path, getcwd
from scripts import obstacle
from scripts.player import Player
from scripts.alien import Alien, Extra
from scripts.laser import Laser
from utils.util import Data
class Menu:
"""
Classe responsável pelo menu principal do jogo
"""
def __init__(self):
# Click do mouse
self.click = False
# Controle dos laços de repetição
self.show_menu = True
self.show_help_screen = False
self.show_highscores_screen = False
def menu(self):
"""
Menu principal do jogo
"""
self.show_menu = True
self.click = False
while self.show_menu:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.show_menu = False
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.click = True
# Título do jogo
draw_text("SPACE", 42, (255, 255, 255), SCREEN_X/2, 80)
draw_text("INVADERS", 42, (255, 255, 255), SCREEN_X/2, 120)
draw_text("in python", 18, (55,113,161), SCREEN_X/2, 150)
# Botões do menu
new_game_button = self.create_button(SCREEN_X/2 - 120, 220, SCREEN_X/2 - 50, 50, (0, 0, 0), "NEW GAME")
highscores_button = self.create_button(SCREEN_X/2 - 120, 280, SCREEN_X/2 - 50, 50, (0, 0, 0), "HIGHTSCORES")
help_button = self.create_button(SCREEN_X/2 - 120, 340, SCREEN_X/2 - 50, 50, (0, 0, 0), "HELP")
quit_button = self.create_button(SCREEN_X/2 - 120, 400, SCREEN_X/2 - 50, 50, (0, 0, 0), "QUIT")
# Posição do mouse
mx, my = pygame.mouse.get_pos()
# Checa o input com os botões do menu
if new_game_button.collidepoint((mx, my)):
if self.click:
self.show_menu = False
new_game()
if highscores_button.collidepoint((mx, my)):
if self.click:
data.organize_file()
self.highscores_screen()
if help_button.collidepoint((mx , my)):
if self.click:
self.help_screen()
if quit_button.collidepoint((mx, my)):
if self.click:
pygame.quit()
exit()
# Depois de checar os inputs o click fica falso
self.click = False
# Style/Update
crt.draw()
pygame.display.update()
screen.fill((0, 0, 0))
def help_screen(self):
"""
Tela de ajuda (mostra os comandos para jogar o jogo)
"""
self.show_help_screen = True
self.click = False
while self.show_help_screen:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.click = True
# Texto e botões
draw_text("HELP", 42, (255, 255, 255), SCREEN_X/2, 80)
draw_text("COMANDS", 22, (255, 255, 255), 100, 150)
draw_text("- MOVIMENTATION", 14, (255, 255, 255), 50, 170, topleft=True)
menu.create_button(50, 200, 50, 50, (0, 0, 0), "<-")
menu.create_button(120, 200, 50, 50, (0, 0, 0), "->")
draw_text("OR", 18, (255, 255, 255), 250, 220)
menu.create_button(330, 200, 50, 50, (0, 0, 0), "A")
menu.create_button(400, 200, 50, 50, (0, 0, 0), "D")
draw_text("- SHOOT", 14, (255, 255, 255), 50, 270, topleft=True)
menu.create_button(50, 300, 250, 50, (0, 0, 0), "SPACE")
draw_text("- PAUSE", 14, (255, 255, 255), 50, 370,topleft=True)
menu.create_button(50, 400, 50, 50, (0, 0, 0), "ESC")
back_to_menu_button = menu.create_button(150, 530, 300, 50, (0, 0, 0), "BACK TO MENU")
# Posição do mouse
mx, my = pygame.mouse.get_pos()
# Checa o input com os botões do menu
if back_to_menu_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.show_help_screen = False
# Depois de checar os inputs o click fica falso
self.click = False
# Style/Update
crt.draw()
pygame.display.update()
screen.fill((0, 0, 0))
def highscores_screen(self):
"""
Tela de score (mostra os maiores scores)
"""
self.show_highscores_screen = True
self.click = False
while self.show_highscores_screen:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.click = True
# Título
draw_text("HIGHSCORES", 42, (255, 255, 255), SCREEN_X/2, 80)
# Laço para desenhar toda a lista de scores
y_pos = 0
for v in data.json_obj["score"]:
draw_text(f"Score: {v}", 16, (255, 255, 255), SCREEN_X/8 - 30, 150 + y_pos, topleft=True)
y_pos += 20
# Laço pra desenhar toda a lista de datas
y_pos = 0
for v in data.json_obj["date"]:
draw_text(f"Date: {v}", 16, (255, 255, 255), SCREEN_X/2 , 150 + y_pos, topleft=True)
y_pos += 20
# Botões
back_to_menu_button = menu.create_button(150, 530, 300, 50, (0, 0, 0), "BACK TO MENU")
reset_button = menu.create_button(500, 550, 80, 30, (0, 0, 0), "RESET", font_size=12) # Botão que reseta a lista de scores
# Posição do mouse
mx, my = pygame.mouse.get_pos()
# Checa o input com os botões do menu
if back_to_menu_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.show_highscores_screen = False
if reset_button.collidepoint((mx, my)):
if self.click:
self.click = False
data.reset()
# Depois de checar os inputs o click fica falso
self.click = False
# Style/Update
crt.draw()
pygame.display.update()
screen.fill((0, 0, 0))
@staticmethod
def create_button(left, top, width, height, color, text, font_size=18):
"""
Função para criar botões
:param left: Posição x da parte esquerda do botão.
:param top: Posição y do topo do botão.
:param width: Largura do botão.
:param height: Altura do botão.
:param color: Cor do botão.
:param text: Texto que vai dentro do botão.
:param font_size: Tamanho da fonte do botão.
:return: Retorna o botão para ser utilizado para checar inputs do mouse.
"""
button_border = pygame.Rect(left - 2, top - 2, width + 4, height + 4)
button = pygame.Rect(left, top, width, height)
pygame.draw.rect(screen, (255, 255, 255), button_border)
pygame.draw.rect(screen, color, button)
draw_text(text, font_size, (255, 255, 255), left+(width/2), top+(height/2))
return button
class Game:
"""
Classe responsável por toda a lógica do jogo
"""
def __init__(self, score=0, alien_speed_multiplier=0):
# Controle dos laços de repetição
self.game_over = False
self.show_pause_menu_screen = False
# Classe para estilizar o jogo
self.crt = CRT()
# Player
player_sprite = Player((SCREEN_X / 2, SCREEN_Y), SCREEN_X, 5)
self.player = pygame.sprite.GroupSingle(player_sprite)
# Vida
self.lives = 3
self.lives_surface = pygame.image.load(path.join(getcwd() + "/assets/images/player.png")).convert_alpha()
self.lives_x_start_pos = SCREEN_X - (self.lives_surface.get_size()[0] * 3 + 30)
# Score
self.score = score
# Obstáculo
self.shape = obstacle.shape
self.block_size = 6
self.blocks_group = pygame.sprite.Group()
self.obstacle_amount = 4
self.obstacle_x_positions = [num * (SCREEN_X / self.obstacle_amount) for num in range(self.obstacle_amount)]
self.create_multiple_obstacles(*self.obstacle_x_positions, x_start=SCREEN_X/15, y_start=480) # Não sei pq 15 :)
# Alien
self.alien_group = pygame.sprite.Group()
self.alien_setup(rows=6, cols=8, x_distance=60, y_distance=48, x_offet=70, y_offset=100)
self.alien_direction = 1
self.alien_speed_multiplier = alien_speed_multiplier
self.alien_lasers_group = pygame.sprite.Group()
# Extra alien
self.extra_alien = pygame.sprite.GroupSingle(None)
self.extra_spawn_time = randint(400, 800)
# Música
self.music = pygame.mixer.Sound("assets/audio/music.wav")
self.music.set_volume(0.06)
self.music.play(loops=-1)
# Som do laser
self.laser_sound = pygame.mixer.Sound("assets/audio/laser.wav")
self.laser_sound.set_volume(0.08)
# Som da explosão
self.explosion_sound = pygame.mixer.Sound("assets/audio/explosion.wav")
self.explosion_sound.set_volume(0.1)
# Mouse
self.click = False
def create_obstacle(self, x_start, y_start, off_set_x):
"""
Cria um obstáculo. A lista da classe dos obstáculos é formada por '0' e '1', que formam a figura do obstáculo,
se na coluna tiver um '1' ele cria um bloco, e os blocos formam o obstáculo.
:param x_start: Posição inicial do eixo x.
:param y_start: Posição inicial do eixo y.
:param off_set_x: Deslocamento do eixo x (para dar um espaço entre os obstáculos).
"""
for row_index, row in enumerate(self.shape):
for col_index, col in enumerate(row):
if col == "1":
x = x_start + col_index * self.block_size + off_set_x
y = y_start + row_index * self.block_size
block = obstacle.Block(self.block_size, (240, 80, 80), x, y)
self.blocks_group.add(block)
def create_multiple_obstacles(self, *offset, x_start, y_start):
"""
Cria vários obstáculos.
:param offset: Deslocamento
:param x_start: Posição inicial do eixo x.
:param y_start: Posição inicial do eixo y.
"""
for offset_x in offset:
self.create_obstacle(x_start, y_start, offset_x)
def alien_setup(self, rows, cols, x_distance, y_distance, x_offet, y_offset):
"""
Configuração dos aliens.
:param rows: Número de linhas.
:param cols: Número de colunas.
:param x_distance: Distancia do eixo x.
:param y_distance: Distancia do eixo y.
:param x_offet: Deslocamento do eixo x.
:param y_offset: Deslocamento do eixo y.
"""
for row_index, row in enumerate(range(rows)):
for col_index, col in enumerate(range(cols)):
x = col_index * x_distance + x_offet
y = row_index * y_distance + y_offset
if row_index == 0:
alien_sprite = Alien("yellow", x, y)
elif 1 <= row_index <= 2:
alien_sprite = Alien("green", x, y)
else:
alien_sprite = Alien("red", x, y)
self.alien_group.add(alien_sprite)
def alien_position_checker(self):
"""
Verifica se os aliens colidiram com a lateral da tela ou se colidiram com a borda inferior.
Se colidir com a borda inferior o Player perde.
Se colidir com as laterais o Alien se move para baixo.
"""
all_aliens = self.alien_group.sprites()
for aliens in all_aliens:
if aliens.rect.right >= SCREEN_X:
self.alien_direction = -1
self.alien_move_down(2)
if aliens.rect.left <= 0:
self.alien_direction = 1
self.alien_move_down(2)
if aliens.rect.bottom >= SCREEN_Y:
self.lives = 0
def alien_move_down(self, distance):
"""
Movimenta os aliens para baixo
:param distance: Distancia de movimentação do alien
"""
if self.alien_group:
for alien in self.alien_group.sprites():
alien.rect.y += distance
def alien_shot(self):
"""
Um alien aleatório atira um laser.
"""
if self.alien_group.sprites():
random_alien = choice(self.alien_group.sprites())
laser_sprite = Laser(random_alien.rect.center, 6)
self.alien_lasers_group.add(laser_sprite)
self.laser_sound.play()
def extra_alien_timer(self):
"""
Cronômetro de aparição do alien extra.
"""
self.extra_spawn_time -= 1
if self.extra_spawn_time <= 0:
self.extra_alien.add(Extra(choice(["right", "left"])))
self.extra_spawn_time = randint(400, 800)
def collision_checks(self):
"""
Checagem das colisões do jogo.
"""
# Player lasers
if self.player.sprite.laser_group:
for laser in self.player.sprite.laser_group:
# Obstacle collisions
if pygame.sprite.spritecollide(laser, self.blocks_group, True):
laser.kill()
# Alien collisions
alien_hit = pygame.sprite.spritecollide(laser, self.alien_group, True)
if alien_hit:
for alien in alien_hit:
self.score += alien.value
laser.kill()
self.explosion_sound.play() # Som do explossão com o alien
# Extra alien collision
if pygame.sprite.spritecollide(laser, self.extra_alien, True):
laser.kill()
self.score += 500
# Alien lasers
if self.alien_lasers_group:
for laser in self.alien_lasers_group:
# Obstacle collisions
if pygame.sprite.spritecollide(laser, self.blocks_group, True):
laser.kill()
# Player collision
if pygame.sprite.spritecollide(laser, self.player, False):
laser.kill()
self.lives -= 1
# Aliens
if self.alien_group:
for alien in self.alien_group:
pygame.sprite.spritecollide(alien, self.blocks_group, True)
if pygame.sprite.spritecollide(alien, self.player, False):
self.lives -= 1
def display_lives(self):
"""
Desenha as vidas do Player na tela.
"""
for live in range(self.lives):
x = self.lives_x_start_pos + (live * (self.lives_surface.get_size()[0] + 10))
screen.blit(self.lives_surface, (x, 8))
def display_score(self):
"""
Desenha o score na tela.
"""
draw_text(f"SCORE: {self.score}", 16, (255, 255, 255), 20, 30, topleft=True)
def victory_screen(self):
"""
Mostra a tela de vitória quando o Player mata todos os Aliens.
"""
if not self.alien_group.sprites():
draw_text("YOU WON", 44, (0, 255, 0), SCREEN_X/2, 200)
back_to_menu_button = menu.create_button(150, 250, 300, 50, (0, 0, 0), "BACK TO MENU")
continue_game_button = menu.create_button(150, 310, 300, 50, (0, 0, 0), "CONTINUE GAME")
mx, my = pygame.mouse.get_pos()
if back_to_menu_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.game_over = True
self.music.stop()
data.add_score(self.score)
menu.menu()
if continue_game_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.game_over = True
self.alien_speed_multiplier += 0.5
print("Aa")
self.music.stop()
new_game(score=self.score, alien_speed_multiplier=self.alien_speed_multiplier)
def game_over_screen(self):
"""
Mostra a tela de derrota quando player morre, ou quando o Alien colide com o Player.
"""
if self.lives <= 0:
draw_text("YOU LOSE", 44, (255, 0, 0), SCREEN_X/2, 200)
back_to_menu_button = menu.create_button(150, 250, 300, 50, (0, 0, 0), "BACK TO MENU")
new_game_button = menu.create_button(150, 310, 300, 50, (0, 0, 0), "NEW GAME")
mx, my = pygame.mouse.get_pos()
if back_to_menu_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.game_over = True
self.music.stop()
data.add_score(self.score)
menu.menu()
if new_game_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.game_over = True
self.music.stop()
data.add_score(self.score)
new_game()
def pause_menu_screen(self):
"""
Tela de pause
"""
self.show_pause_menu_screen = True
self.click = False
while self.show_pause_menu_screen:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.click = True
draw_text("PAUSE", 42, (255, 255, 255), SCREEN_X/2, 80)
back_to_game_button = menu.create_button(150, 170, 300, 50, (0, 0, 0), "BACK TO GAME")
back_to_menu_button = menu.create_button(150, 230, 300, 50, (0, 0, 0), "BACK TO MENU")
quit_button = menu.create_button(150, 290, 300, 50, (0, 0, 0), "QUIT GAME")
mx, my = pygame.mouse.get_pos()
if back_to_game_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.show_pause_menu_screen = False
if back_to_menu_button.collidepoint((mx, my)):
if self.click:
self.click = False
self.show_pause_menu_screen = False
self.game_over = True
self.music.stop()
menu.menu()
if quit_button.collidepoint((mx, my)):
if self.click:
self.click = False
pygame.quit()
exit()
self.click = False
self.crt.draw()
pygame.display.update()
screen.fill((0, 0, 0))
def run(self):
"""
Função que faz o jogo rodar, atualiza, checa colisões, desenha na tela, etc...
"""
# Atualiza todos os grupos de sprites
self.alien_position_checker()
self.collision_checks()
if self.alien_group.sprites():
self.extra_alien_timer()
self.display_lives()
self.display_score()
# Para quando as vidas acabarem, tudo fica parado
if self.lives > 0:
self.player.update()
self.alien_lasers_group.update()
self.extra_alien.update()
self.alien_group.update(self.alien_direction * self.alien_speed_multiplier)
# Desenha todos os grupos de sprites
self.player.sprite.laser_group.draw(screen)
self.player.draw(screen)
self.blocks_group.draw(screen)
self.alien_group.draw(screen)
self.alien_lasers_group.draw(screen)
self.extra_alien.draw(screen)
# Telas
self.game_over_screen()
self.victory_screen()
# No final de tudo o click do mouse volta a ser falso
self.click = False
class CRT:
"""
Classe para deixar o estilo do jogo um pouco mais retrô
"""
def __init__(self):
# Bordas de TV antiga
self.tv = pygame.image.load(path.join(getcwd() + "/assets/images/tv.png")).convert_alpha()
self.tv = pygame.transform.scale(self.tv, (SCREEN_X, SCREEN_Y))
def draw(self):
"""
Desenha na tela
"""
self.tv.set_alpha(randint(75, 90))
self.create_crt_lines()
screen.blit(self.tv, (0, 0))
def create_crt_lines(self):
"""
Cria linhas para dar um estilo retrô para o jogo
"""
line_height = 3
line_amount = int(SCREEN_X / line_height)
for line in range(line_amount):
y_pos = line * line_height
pygame.draw.line(self.tv, "black", (0, y_pos), (SCREEN_X, y_pos), 1)
if __name__ == '__main__':
# Inicia o pygame
pygame.init()
# Tamanho da tela
SCREEN_X = 600
SCREEN_Y = 600
# Tela
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
pygame.display.set_caption("Space Invaders") # Nome do jogo que fica na janela do jogo
# Ajuda a controlar a taxa de atualização do jogo - FPS
clock = pygame.time.Clock()
FPS = 60
def draw_text(text, tam, color, x, y, topleft=False):
"""
Desenha um texto na tela
:param text: Texto.
:param tam: Tamanho do texto.
:param color: Cor do texto.
:param x: Posição do eixo x.
:param y: Posição do eixo y.
:param topleft: Se o parametro for True se baseia no topo esquerdo para as cordenadas.
"""
fonte = pygame.font.Font("assets/8-bit_font.ttf", tam)
text_obj = fonte.render(text, False, color)
text_rect = text_obj.get_rect()
if topleft:
text_rect.topleft = (x, y)
else:
text_rect.center = (x, y)
screen.blit(text_obj, text_rect)
def new_game(score=0, alien_speed_multiplier=1):
"""
Cria um novo jogo.
"""
game = Game(score, alien_speed_multiplier)
ALIENLASER = pygame.USEREVENT + 1
pygame.time.set_timer(ALIENLASER, 800)
while not game.game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == ALIENLASER:
if game.lives > 0:
game.alien_shot()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
game.click = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game.pause_menu_screen()
'''if event.key == pygame.K_p:
for alien in game.alien_group:
alien.kill()'''
screen.fill((0, 0, 0))
game.run()
crt.draw()
pygame.display.flip()
clock.tick(60)
# Classe dos dados do jogo
data = Data()
# Classe para estilizar o jogo
crt = CRT()
# Classe do menu
menu = Menu()
# --- COMEÇA O JOGO AQUI --- #
menu.menu() # Começa o jogo pelo menu