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
|
import os
import sys
import time
from direction import DIRECTION
from game_state import GameState
from mode import MODE
from settings import settings
from game_state import WIDTH, HEIGHT, TILE_WIDTH, TILE_HEIGHT
import pygame
class Game():
def __init__(self, lolsettings):
self.settings = lolsettings
def show_gameover_screen(self, screen, game_state, sprites):
font = pygame.font.SysFont(None, 64)
self.score(screen, game_state.score , 200 , 800)
# Render the "Game Over" text to a surface
game_over_text_1 = font.render(
"Game Over", True, (255, 255, 255))
game_over_text_2 = font.render(
"Press R to try again or Q to quit.", True, (255, 255, 255))
# Blit the "Game Over" text onto the screen
text_rect_1 = game_over_text_1.get_rect(
center=(WIDTH / 2, HEIGHT / 2 - 75))
text_rect_2 = game_over_text_2.get_rect(
center=(WIDTH / 2, HEIGHT / 2))
screen.blit(game_over_text_1, text_rect_1)
screen.blit(game_over_text_2, text_rect_2)
# Update the display
pygame.display.flip()
quit_game = False # Initialize the flag variable
while not quit_game:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
# Reset the game and start again
# Add your own code here to reset the game state
self.reset_game(game_state, sprites)
game_state.game_over = False
quit_game = True # Set the flag to True to break out of both loops
break
elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
game_state.game_over = True
quit_game = True
pygame.quit()
os.system("python MacPan.py 1")
exit()
elif event.type == pygame.QUIT:
game_state.game_over = True
quit_game = True # Set the flag to True to break out of both loops
break
def score(self, screen, text , POS1, POS2 ):
font = pygame.font.SysFont(None, 21)
wining_text_123 = font.render(
"Score", True, (255, 255, 255))
# Render the "Game Over" text to a surface
wining_text_12 = font.render(
str(text), True, (255, 255, 255))
# Blit the "Game Over" text onto the screen
text_rect_12 = wining_text_12.get_rect(
center=(POS1 / 2, POS2 / 2))
text_rect_123 = wining_text_123.get_rect(
center=(POS1 / 2,( (POS2 / 2)-50) ))
screen.blit(wining_text_12, text_rect_12)
screen.blit(wining_text_123, text_rect_123)
# Update the display
pygame.display.flip()
def show_wining_screen(self, screen, game_state, sprites):
font = pygame.font.SysFont(None, 64)
self.score(screen, game_state.score, 200, 800)
# Render the "Game Over" text to a surface
wining_text_1 = font.render(
"Congratulation You Won!!", True, (255, 255, 255))
wining_text_2 = font.render(
"Press R to play again or Q to quit", True, (255, 255, 255))
# Blit the "Game Over" text onto the screen
text_rect_1 = wining_text_1.get_rect(
center=(WIDTH / 2, HEIGHT / 2))
text_rect_2 = wining_text_2.get_rect(
center=(WIDTH / 2, HEIGHT / 2 + 100))
screen.blit(wining_text_1, text_rect_1)
screen.blit(wining_text_2, text_rect_2)
# Update the display
pygame.display.flip()
quit_game = False # Initialize the flag variable
while not quit_game:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
# Reset the game and start again
# Add your own code here to reset the game state
self.reset_game(game_state, sprites)
game_state.game_over = False
quit_game = True # Set the flag to True to break out of both loops
break
elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
game_state.game_over = True
quit_game = True
pygame.quit()
os.system("python MacPan.py 1")
exit()
elif event.type == pygame.QUIT:
game_state.game_over = True
quit_game = True # Set the flag to True to break out of both loops
break
def reset_game(self, game_state, sprites):
game_state.reset(sprites,self.settings)
def run(self):
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Sprite sheet for pacman
sprite_sheet = pygame.image.load(
'../assets/pacman_left_sprite.png').convert_alpha()
# Sprite sheets for the ghosts
blinky_sprite = pygame.image.load(
'../assets/blinky.png').convert_alpha()
pinky_sprite = pygame.image.load('../assets/pinky.png').convert_alpha()
clyde_sprite = pygame.image.load('../assets/clyde.png').convert_alpha()
inky_sprite = pygame.image.load('../assets/inky.png').convert_alpha()
sprites = [sprite_sheet, blinky_sprite,
pinky_sprite, inky_sprite, clyde_sprite]
# Set the timer to trigger after 10,000 milliseconds (10 seconds)
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, 1000 * 10, 1)
game_state = GameState(sprites, self.settings)
self.score(screen,game_state.score,WIDTH,HEIGHT)
# Set the pacman velocity
dx = 0
dy = 0
# counter used to cycle through pacman sprite animation
counter = 0
clock = pygame.time.Clock()
pygame.mixer.set_num_channels(5)
pygame.mixer.music.load('../assets/sfx/game_start.wav')
siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav')
if self.settings.sound:
pygame.mixer.music.play()
siren_sound.play(-1)
# Main game loop
while not game_state.game_over:
# setting game fps
clock.tick(settings.fps)
self.score(screen, game_state.score,WIDTH,HEIGHT)
# counter logic for cycling between pacman different sprites
if counter < 19:
counter += 1
else:
counter = 0
screen.fill((0, 0, 0)) # Clear the screen
# Temporary values for delta_x and delta_y in the position of pacman
tx = dx
ty = dy
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_state.game_over = True
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
game_state.pacman.direction = DIRECTION.UP
ty = -game_state.pacman.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
game_state.pacman.direction = DIRECTION.DOWN
ty = game_state.pacman.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
game_state.pacman.direction = DIRECTION.LEFT
tx = -game_state.pacman.speed
ty = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
game_state.pacman.direction = DIRECTION.RIGHT
tx = game_state.pacman.speed
ty = 0 # Necssarry to move only horizontal or vertical
# Check for the timer event
if event.type == timer_event:
game_state.pinky.mode = MODE.CHASING
game_state.inky.mode = MODE.CHASING
game_state.blinky.mode = MODE.CHASING
game_state.clyde.mode = MODE.CHASING
keys = pygame.key.get_pressed()
# Simulates holding the key which adds better playability for pacman
if keys[pygame.K_w]:
game_state.pacman.direction = DIRECTION.UP
ty = -game_state.pacman.speed
tx = 0
elif keys[pygame.K_s]:
game_state.pacman.direction = DIRECTION.DOWN
ty = game_state.pacman.speed
tx = 0
elif keys[pygame.K_a]:
game_state.pacman.direction = DIRECTION.LEFT
tx = -game_state.pacman.speed
ty = 0
elif keys[pygame.K_d]:
game_state.pacman.direction = DIRECTION.RIGHT
tx = game_state.pacman.speed
ty = 0
# if tx and ty doesn't lead to colliding change the current
# dx and dy to them and other wise
# let pacman move in his previous direction
if game_state.pacman.check_collision(game_state, tx, ty, TILE_WIDTH, TILE_HEIGHT):
dx = tx
dy = ty
if dx < 0:
game_state.pacman.direction = DIRECTION.LEFT
elif dx > 0:
game_state.pacman.direction = DIRECTION.RIGHT
elif dy < 0:
game_state.pacman.direction = DIRECTION.UP
elif dy > 0:
game_state.pacman.direction = DIRECTION.DOWN
if game_state.pacman.check_collision(game_state, dx, dy, TILE_WIDTH, TILE_HEIGHT):
game_state.pacman.x += dx
game_state.pacman.y += dy
game_state.pacman.x %= 900 # logic for portal
# Move ghosts
game_state.blinky.move(game_state, screen)
game_state.pinky.move(game_state, screen)
game_state.inky.move(game_state, screen)
game_state.clyde.move(game_state, screen)
# Draw the map on each frame
game_state.map.draw_map(screen)
# Draw the game_state.pacman and the ghosts
game_state.pacman.draw(screen, counter)
game_state.blinky.draw(screen, game_state.pacman.powerup, counter)
game_state.pinky.draw(screen, game_state.pacman.powerup, counter)
game_state.inky.draw(screen, game_state.pacman.powerup, counter)
game_state.clyde.draw(screen, game_state.pacman.powerup, counter)
if game_state.food == 246:
self.show_wining_screen(screen, game_state, sprites)
if not game_state.is_pacman_alive:
siren_sound.stop()
pygame.mixer.music.load('../assets/sfx/death_1.wav')
if
|