-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (118 loc) · 4.6 KB
/
main.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
from scripts.initialisation import *
from scripts.labyrinth import *
from scripts.pacman import *
from scripts.ghost import *
def reset(time_wait):
"""reset l'état du jeu
Returns:
pygame Groupe: groupe de joueur / fantôme
"""
ghost_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()
player = Pac_man(TILE_SIZE*13 + TILE_SIZE//2, TILE_SIZE*23, labyrinth)
blinky = Blinky(labyrinth, TILE_SIZE*13 + TILE_SIZE//2, TILE_SIZE*11, time_in_spawn=0, type_hunt="hunt", timer_hunt=8_000+time_wait)
pinky = Pinky(labyrinth, TILE_SIZE*11 + TILE_SIZE//2, TILE_SIZE*14, time_in_spawn=1000+time_wait, type_hunt="hunt", timer_hunt=7_000+time_wait)
inky = Inky(labyrinth, TILE_SIZE*13 + TILE_SIZE//2, TILE_SIZE*14, time_in_spawn=4000+time_wait, type_hunt="hunt", timer_hunt=6_000+time_wait)
clyde = Clyde(labyrinth, TILE_SIZE*15 + TILE_SIZE//2, TILE_SIZE*14, time_in_spawn=7000+time_wait, type_hunt="hunt", timer_hunt=5_000+time_wait)
player_group.add(player)
ghost_group.add(blinky)
ghost_group.add(pinky)
ghost_group.add(inky)
ghost_group.add(clyde)
return player_group, ghost_group
def display_lifes():
pygame.draw.rect(screen, "#000000", pygame.Rect((TILE_SIZE*(WIDTH + 0.5), 50, TILE_SIZE, TILE_SIZE * 1.2 * lifes)))
for l in range(lifes-1):
screen.blit(life_image, ((TILE_SIZE*(WIDTH + 0.5), 50 + TILE_SIZE* 1.2 * l)))
def display_score():
score_image = font.render(str(score), True,
(255, 255, 255))
size = font.size(str(score))
pygame.draw.rect(screen, "#000000", pygame.Rect(TILE_SIZE*WIDTH+1, TILE_SIZE * HEIGHT // 2, TILE_SIZE*2,TILE_SIZE + size[1]))
screen.blit(score_image, ((TILE_SIZE * (WIDTH + 1) - size[0] // 2, TILE_SIZE * HEIGHT // 2 + size[1])))
def display_end_message():
message = "GAME OVER"
font = pygame.font.SysFont("", 50)
message_image = font.render(message,True, (255, 0, 0))
size = font.size(message)
screen.blit(message_image, ((TILE_SIZE * (WIDTH) // 2 - size[0] // 2, TILE_SIZE * HEIGHT // 2 - size[1])))
def play_death():
sounds["siren"]["1"].stop()
sounds["siren"]["weak"].stop()
sound = sounds["death"]["1"]
sound.play()
pygame.time.wait(1200)
sound.stop()
sound = sounds["death"]["2"]
sound.play()
pygame.time.wait(200)
sound.play()
pygame.time.wait(200)
# ==== init ==== #
life_image = pygame.transform.scale(pygame.image.load(f"{DATA_DIRECTORY}/pacman/4.png").convert_alpha(), (TILE_SIZE, TILE_SIZE))
font = pygame.font.SysFont("", TILE_SIZE * 25 // 32)
labyrinth = Labyrinth(f"{DATA_DIRECTORY}/map.txt", screen)
score = 0
lifes = 3
sc_img = font.render("SCORE",True, (255, 255, 255))
size = font.size("SCORE")
screen.blit(sc_img, ((TILE_SIZE * (WIDTH + 1) - size[0] // 2, TILE_SIZE * HEIGHT // 2 - size[1])))
start_sound = sounds["off game"]["start"]
# ==== main loop ==== #
run = True
while run and lifes:
if lifes == 3:
start_sound.play()
time_wait = 4000
else:
time_wait = 1000
display_lifes()
player_group, ghost_group = reset(time_wait)
labyrinth.draw_level()
player_group.draw(screen)
ghost_group.draw(screen)
pygame.display.flip()
pygame.time.wait(time_wait)
game = True
sounds["siren"]["1"].play(-1)
while game and run:
display_score()
labyrinth.draw_level()
keys = pygame.event.get()
for event in keys:
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
for player in player_group:
death, add_score = player.update(keys, ghost_group)
if death:
play_death()
game = False
score += add_score
player_group.draw(screen)
ghost_group.update(player_group.sprites()[0], ghost_group.sprites()[0])
ghost_group.draw(screen)
pygame.display.flip()
if labyrinth.get_nbr_gommes() == 0:
game = False
run = False
#print(clock.get_fps()) # juste pour afficher les fps
clock.tick(GLOBAL_FPS)
lifes -= 1
print(score)
if score == 14_620:
print("Bravo ! Vous avez effectué le score maximume !")
sounds["siren"]["weak"].stop()
sounds["siren"]["1"].stop()
sounds["off game"]["end"].play()
display_score()
labyrinth.draw_level()
player_group.draw(screen)
ghost_group.draw(screen)
pygame.display.flip()
display_end_message()
pygame.display.flip()
pygame.time.wait(int(sounds["off game"]["end"].get_length()*1000))
pygame.quit()