-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameScene.py
376 lines (335 loc) · 15.6 KB
/
GameScene.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
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
import pygame
from Variables import *
from MapGenarator import *
from PacMan import *
from Ghost import *
from FoodPiece import *
from DB_communicator import *
class GameScene:
def __init__(self):
self.screen = pygame.Surface((1280, 720))
self.screen_map = pygame.Surface((644, 713))
self.stay_here = True
self.username = ""
self.paused = False
self.music = True
self.ivent_timer = 0
self.pacman = None
self.ghosts = []
self.map = None
self.food = []
self.score = 0
self.score_high = 0
self.lives = 3
self.start_sound = pygame.mixer.Sound('Static/Sounds/game_start.ogg')
def setup(self, map_type):
if map_type == "default":
self.map = default_map
elif map_type == "generated":
self.map = map_generator.generate_map()
width = self.screen_map.get_height() // len(self.map)
qw = width // 4 #quater width
pacman_spawn = get_pacman_spawn(self.map)
self.pacman = PacMan(width + 2 * qw, self.map, pacman_spawn, width)
self.ghosts = self.init_ghosts()
self.food = self.init_food()
self.score = 0
self.lives = 3
self.start_sound.stop()
self.start_sound.play()
def update(self, user_input):
self.ivent_timer += 1
self.manage_user_input(user_input)
if not self.paused:
self.pacman.update(user_input)
self.update_gosts()
self.game_logic()
self.screen_map.fill(color_black)
self.screen.fill(color_black)
self.render_map()
self.render_food()
self.render_ghosts()
self.render_pacman()
self.render_ui()
self.screen.blit(self.screen_map, (0, 0))
def game_logic(self):
if self.pacman_bumped_into_ghost():
if self.ghosts[0].mode == "Normal":
self.death()
else:
eat_ghost_sound = pygame.mixer.Sound('Static/Sounds/eat_ghost.ogg')
eat_ghost_sound.play()
self.send_ghost_to_prison()
if len(self.food) == 0:
win_sound = pygame.mixer.Sound('Static/Sounds/win.ogg')
win_sound.play()
make_a_record(self.username, self.score)
self.setup("generated")
self.update_food()
self.check_gates()
self.score_high = get_high(self.username)
self.score_high = max(self.score_high, self.score)
def send_ghost_to_prison(self):
for ghost in self.ghosts:
if self.pacman.pos_x == ghost.pos_x and self.pacman.pos_y == ghost.pos_y:
spawn = get_ghost_spawn(self.map)
sp_i = spawn[0]
sp_j = spawn[1]
ghost.pos_x = sp_j + 2
ghost.pos_y = sp_i
width = self.screen_map.get_height() // len(self.map)
ghost.screen_pos_x = width * ghost.pos_x - width // 4
ghost.screen_pos_y = width * ghost.pos_y - width // 4
num_of_ghost = self.how_many_prisoned_ghosts()
self.score += (200 * num_of_ghost)
def manage_user_input(self, user_input):
if self.ivent_timer < 10:
return
if user_input[pygame.K_p]:
self.paused = not self.paused
self.ivent_timer = 0
if user_input[pygame.K_r] and user_input[pygame.K_c]:
self.replay_on_current_map()
self.ivent_timer = 0
if user_input[pygame.K_r] and user_input[pygame.K_g]:
self.setup("generated")
self.ivent_timer = 0
if user_input[pygame.K_r] and user_input[pygame.K_d]:
self.setup("default")
self.ivent_timer = 0
if user_input[pygame.K_v]:
self.stay_here = False
if user_input[pygame.K_m]:
self.music = not self.music
if self.music:
pygame.mixer.music.play()
else:
pygame.mixer.music.pause()
self.ivent_timer = 0
def replay_on_current_map(self):
width = self.screen_map.get_height() // len(self.map)
qw = width // 4 #quater width
pacman_spawn = get_pacman_spawn(self.map)
self.pacman = PacMan(width + 2 * qw, self.map, pacman_spawn, width)
self.ghosts = self.init_ghosts()
self.food = self.init_food()
self.score = 0
self.lives = 3
def death(self):
death_sound = pygame.mixer.Sound('Static/Sounds/death.ogg')
death_sound.play()
self.lives -= 1
if self.lives > 0:
width = self.screen_map.get_height() // len(self.map)
qw = width // 4 #quater width
pacman_spawn = get_pacman_spawn(self.map)
self.pacman = PacMan(width + 2 * qw, self.map, pacman_spawn, width)
self.ghosts = self.init_ghosts()
else:
make_a_record(self.username, self.score)
self.setup("generated")
def render_ui(self):
small_font = pygame.font.Font('Static/Fonts/mini_pixel-7.ttf', 23)
regular_font = pygame.font.Font('Static/Fonts/mini_pixel-7.ttf', 30)
regular_font_large = pygame.font.Font('Static/Fonts/mini_pixel-7.ttf', 40)
header_font = pygame.font.Font('Static/Fonts/PAC-FONT.ttf', 98)
header = header_font.render("Pac---Man", True, color_white)
score_text = regular_font_large.render(
f"Score: {str(self.score)}", True, color_white
)
lives_text = regular_font_large.render(
f"Lives: {str(self.lives)}", True, color_white
)
replay_default_text = regular_font.render("R + D -> replay on default map", True, color_white)
replay_generated_text = regular_font.render("R + G -> replay on generated map", True, color_white)
replay_text = regular_font.render("R + C -> replay on current map", True, color_white)
see_records_text = regular_font.render("V -> see records", True, color_white)
pause_text = regular_font.render("P -> play/pause", True, color_white)
escape_text = regular_font.render("Esc -> quit game", True, color_white)
paused_text = regular_font_large.render("PAUSED", True, color_white)
username_text = regular_font_large.render(
f"Player: {self.username}", True, color_white
)
high_score_text = regular_font_large.render(
f"High: {str(self.score_high)}", True, color_white
)
mute_music_text = None
if self.music:
mute_music_text = regular_font.render("M -> mute music", True, color_white)
else:
mute_music_text = regular_font.render("M -> unmute music", True, color_white)
credit_text = small_font.render("G.Koganovskiy 2020", True, color_white)
self.screen.blit(header, (self.screen_map.get_width() + 20, 10))
self.screen.blit(score_text, (self.screen_map.get_width() + 20, 120))
self.screen.blit(high_score_text, (self.screen_map.get_width() + 20, 150))
self.screen.blit(lives_text, (self.screen_map.get_width() + 20, 180))
self.screen.blit(username_text, (self.screen_map.get_width() + 20, 220))
self.screen.blit(pause_text, (self.screen_map.get_width() + 20, 310))
self.screen.blit(replay_text, (self.screen_map.get_width() + 20, 340))
self.screen.blit(replay_generated_text, (self.screen_map.get_width() + 20, 370))
self.screen.blit(replay_default_text, (self.screen_map.get_width() + 20, 400))
self.screen.blit(see_records_text, (self.screen_map.get_width() + 20, 430))
self.screen.blit(mute_music_text, (self.screen_map.get_width() + 20, 460))
self.screen.blit(escape_text, (self.screen_map.get_width() + 20, 490))
self.screen.blit(credit_text, (self.screen.get_width() - credit_text.get_width() - 10, self.screen.get_height() - 20))
middle = (self.screen.get_width() - self.screen_map.get_width()) // 2 + self.screen_map.get_width()
if self.paused:
self.screen.blit(paused_text, (middle - paused_text.get_width() // 2, 550))
def check_gates(self):
gates_pos = get_gates_pos(self.map)
i = gates_pos[0]
j = gates_pos[1]
if self.prisoned_ghosts():
self.map[i][j] = 'U'
self.map[i][j + 1] = 'U'
else:
self.map[i][j] = '#'
self.map[i][j + 1] = '#'
def prisoned_ghosts(self):
for ghost in self.ghosts:
i = ghost.pos_y
j = ghost.pos_x
if self.map[i][j] == 'U' and ghost.mode == "Normal":
return True
return False
def how_many_prisoned_ghosts(self):
result = 0
for ghost in self.ghosts:
i = ghost.pos_y
j = ghost.pos_x
if self.map[i][j] in ['U', 'g']:
result += 1
return result
def scare_ghosts(self):
for ghost in self.ghosts:
ghost.go_to_scare_mode()
def update_food(self):
ind = 0
while ind < len(self.food):
food_piece = self.food[ind]
i = food_piece.i
j = food_piece.j
if self.pacman.pos_y == i and self.pacman.pos_x == j:
if food_piece.type == "Energizer":
energizer_sound = pygame.mixer.Sound('Static/Sounds/energizer.ogg')
energizer_sound.play()
self.scare_ghosts()
if len(self.food) % 4 == 0:
eat_sound = pygame.mixer.Sound('Static/Sounds/eating.ogg')
eat_sound.play()
self.food.pop(ind)
self.score += 10
ind += 1
def render_food(self):
width = self.screen_map.get_height() // len(self.map)
for food_piece in self.food:
i = food_piece.i
j = food_piece.j
if food_piece.type == "Energizer":
if self.ivent_timer % 30 > 15:
pygame.draw.circle(self.screen_map, color_food, (j * width + width // 2, i * width + width // 2), 12)
else:
pygame.draw.circle(self.screen_map, color_food, (j * width + width // 2, i * width + width // 2), 3)
def init_food(self):
food_array = []
for i in range(len(self.map)):
for j in range(len(self.map[0])):
if self.map[i][j] == 'O':
new_food_piece = None
if (i == 1 and j == 1) or (i == 29 and j == 1) or (i == 1 and j == 26) or (i == 29 and j == 26):
new_food_piece = FoodPiece(i, j, "Energizer")
else:
new_food_piece = FoodPiece(i, j)
food_array.append(new_food_piece)
return food_array
def pacman_bumped_into_ghost(self):
return any(
self.pacman.pos_x == ghost.pos_x and self.pacman.pos_y == ghost.pos_y
for ghost in self.ghosts
)
def update_gosts(self):
blinky = self.ghosts[0]
for ghost in self.ghosts:
ghost.update(self.pacman, blinky)
def render_map(self):
width = self.screen_map.get_height() // len(self.map)
qw = width // 4 #quater width
for i in range(len(self.map)):
for j in range(len(self.map[0])):
if self.map[i][j] == '#':
pygame.draw.rect(self.screen_map, color_dark_blue, (j * width, i * width, width, width))
lines = get_render_lines(self.map, i, j)
if lines[0]:
pygame.draw.line(self.screen_map, color_bright_blue, (j * width, i * width + qw), ((j + 1) * width, i * width + qw), 10)
if lines[1]:
pygame.draw.line(self.screen_map, color_bright_blue, ((j + 1) * width - qw, i * width), ((j + 1) * width - qw, (i + 1) * width), 10)
if lines[2]:
pygame.draw.line(self.screen_map, color_bright_blue, ((j + 1) * width, (i + 1) * width - qw), (j * width, (i + 1) * width - qw), 10)
if lines[3]:
pygame.draw.line(self.screen_map, color_bright_blue, (j * width + qw, i * width), (j * width + qw, (i + 1) * width), 10)
if lines[0]:
pygame.draw.line(self.screen_map, color_blue, (j * width, i * width + qw), ((j + 1) * width, i * width + qw), 5)
if lines[1]:
pygame.draw.line(self.screen_map, color_blue, ((j + 1) * width - qw, i * width), ((j + 1) * width - qw, (i + 1) * width), 5)
if lines[2]:
pygame.draw.line(self.screen_map, color_blue, ((j + 1) * width, (i + 1) * width - qw), (j * width, (i + 1) * width - qw), 5)
if lines[3]:
pygame.draw.line(self.screen_map, color_blue, (j * width + qw, i * width), (j * width + qw, (i + 1) * width), 5)
for i in range(len(self.map)):
for j in range(len(self.map[0])):
if self.map[i][j] != '#':
pygame.draw.rect(self.screen_map, color_black, (j * width - qw, i * width - qw, width + 2 * qw, width + 2 * qw))
pygame.draw.rect(self.screen_map, color_black, (0, 0, 644, 713), width // 2)
# pygame.draw.rect(self.screen_map, color_yellow, (self.ghost.pos_x * width, self.ghost.pos_y * width, width, width))
def render_pacman(self):
self.screen_map.blit(self.pacman.screen, (self.pacman.screen_pos_x, self.pacman.screen_pos_y))
def render_ghosts(self):
for ghost in self.ghosts:
self.screen_map.blit(ghost.screen, (ghost.screen_pos_x, ghost.screen_pos_y))
def init_ghosts(self):
width = self.screen_map.get_height() // len(self.map)
qw = width // 4 #quater width
spawn = get_ghost_spawn(self.map)
sp_i = spawn[0]
sp_j = spawn[1]
blinky = Ghost("Blinky", width + 2 * qw, self.map, [sp_i, sp_j + 2], width)
pinky = Ghost("Pinky", width + 2 * qw, self.map, [sp_i, sp_j + 3], width)
inky = Ghost("Inky", width + 2 * qw, self.map, [sp_i + 1, sp_j + 2], width)
clyde = Ghost("Clyde", width + 2 * qw, self.map, [sp_i + 1, sp_j + 3], width)
return [blinky, pinky, inky, clyde]
def get_render_lines(map, i, j):
result = [False, False, False, False] #up - right - down - left
if map[i][j] == '#':
if i == 0 or map[i - 1][j] != '#':
result[0] = True
if j == len(map[0]) - 1 or map[i][j + 1] != '#':
result[1] = True
if i == len(map) - 1 or map[i + 1][j] != '#':
result[2] = True
if j == 0 or map[i][j - 1] != '#':
result[3] = True
return result
def get_pacman_spawn(map):
result = [None, None]
for i in range(len(map)):
for j in range(len(map[0])):
if map[i][j] == 'p':
result[0] = i
result[1] = j
return result
def get_ghost_spawn(map):
result = [None, None]
for i in range(len(map)):
for j in range(len(map[0])):
if map[i][j] == 'g':
result[0] = i
result[1] = j
return result
def get_gates_pos(map):
result = [None, None]
for i in range(len(map)):
for j in range(len(map[0])):
if map[i][j] == 'g':
result[0] = i - 2
result[1] = j + 2
return result