-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackman.py
304 lines (258 loc) · 8.85 KB
/
packman.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
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Game constants
CELL_SIZE = 30
MAZE_WIDTH = 19
MAZE_HEIGHT = 21
WIDTH = CELL_SIZE * MAZE_WIDTH
HEIGHT = CELL_SIZE * MAZE_HEIGHT + 60
FPS = 60
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Corrected maze layout (fixed central corridor)
MAZE = [
"###################",
"#........#........#",
"# ### ## # ## ### #",
"# ### ## # ## ### #",
"# ### ## # ## ### #",
"#.................#",
"# ### # ### # ### #",
"# ### # ### # ### #",
"# # # #",
"###################",
"# # # #", # Row 9
"# ### # ### # ### #",
"# ### # ### # ### #",
"#.................#",
"# ### ## # ## ### #",
"# ### ## # ## ### #",
"# ### ## # ## ### #",
"#.................#", # Row 17 - open corridor
"###################",
]
def reset_game():
global score, lives, dots, pacman, ghost, game_over
score = 0
lives = 3
game_over = False
# Reset dots
for y in range(len(MAZE)):
for x in range(len(MAZE[y])):
dots[y][x] = MAZE[y][x] in ('.', ' ')
# Reset characters with valid starting positions
pacman.update({
"grid_x": 9, # Center of open corridor
"grid_y": 17, # Row 17 (now open)
"progress": 0,
"direction": (0, 0),
"next_dir": (0, 0)
})
ghost.update({
"grid_x": 9,
"grid_y": 5,
"progress": 0,
"direction": (0, 0)
})
# Initialize screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man")
clock = pygame.time.Clock()
# Game state
score = 0
lives = 3
game_over = False
# Initialize dots
dots = []
for y, row in enumerate(MAZE):
dot_row = []
for x, char in enumerate(row):
dot_row.append(char in ('.', ' '))
dots.append(dot_row)
# Initialize Pac-Man with valid starting position
pacman = {
"grid_x": 9,
"grid_y": 17,
"progress": 0,
"direction": (0, 0),
"next_dir": (0, 0),
"speed": 3
}
# Initialize Ghost
ghost = {
"grid_x": 9,
"grid_y": 5,
"progress": 0,
"direction": (0, 0),
"speed": 2,
"next_dir": (0, 0)
}
def move_character(character, maze):
if character["direction"] != (0, 0):
character["progress"] += character["speed"] / CELL_SIZE
if character["progress"] >= 1:
overflow = character["progress"] - 1
new_x = character["grid_x"] + character["direction"][0]
new_y = character["grid_y"] + character["direction"][1]
# Check if new position is valid before moving
if 0 <= new_x < len(maze[0]) and 0 <= new_y < len(maze):
if maze[new_y][new_x] != "#":
character["grid_x"] = new_x
character["grid_y"] = new_y
character["progress"] = overflow
else:
character["direction"] = (0, 0)
character["progress"] = 0
else:
# Handle tunnel wrap-around
if new_x < 0:
character["grid_x"] = len(maze[0]) - 1
elif new_x >= len(maze[0]):
character["grid_x"] = 0
else:
character["direction"] = (0, 0)
character["progress"] = 0
def handle_pacman_input():
keys = pygame.key.get_pressed()
new_dir = None
if keys[pygame.K_LEFT]:
new_dir = (-1, 0)
elif keys[pygame.K_RIGHT]:
new_dir = (1, 0)
elif keys[pygame.K_UP]:
new_dir = (0, -1)
elif keys[pygame.K_DOWN]:
new_dir = (0, 1)
if keys[pygame.K_r] and game_over:
reset_game()
if new_dir and not game_over:
# Check if we can change direction immediately
if pacman["progress"] == 0:
next_x = pacman["grid_x"] + new_dir[0]
next_y = pacman["grid_y"] + new_dir[1]
if 0 <= next_x < len(MAZE[0]) and 0 <= next_y < len(MAZE):
if MAZE[next_y][next_x] != "#":
pacman["direction"] = new_dir
else:
# Queue direction change for next intersection
pacman["next_dir"] = new_dir
def move_ghost():
if game_over:
return
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
if ghost["progress"] == 0:
possible_dirs = []
current_dir = ghost["direction"]
for dx, dy in directions:
new_x = ghost["grid_x"] + dx
new_y = ghost["grid_y"] + dy
if 0 <= new_x < len(MAZE[0]) and 0 <= new_y < len(MAZE):
if MAZE[new_y][new_x] != "#" and (dx, dy) != (-current_dir[0], -current_dir[1]):
possible_dirs.append((dx, dy))
if possible_dirs:
ghost["direction"] = random.choice(possible_dirs)
else:
ghost["direction"] = (0, 0)
move_character(ghost, MAZE)
def check_collisions():
global score, lives, game_over
if game_over:
return
# Check dot collection
px = pacman["grid_x"]
py = pacman["grid_y"]
if dots[py][px]:
dots[py][px] = False
score += 10
# Ghost collision
pac_rect = pygame.Rect(
(pacman["grid_x"] + pacman["direction"][0] * pacman["progress"]) * CELL_SIZE,
(pacman["grid_y"] + pacman["direction"][1] * pacman["progress"]) * CELL_SIZE,
CELL_SIZE, CELL_SIZE
)
ghost_rect = pygame.Rect(
(ghost["grid_x"] + ghost["direction"][0] * ghost["progress"]) * CELL_SIZE,
(ghost["grid_y"] + ghost["direction"][1] * ghost["progress"]) * CELL_SIZE,
CELL_SIZE, CELL_SIZE
)
if pac_rect.colliderect(ghost_rect):
lives -= 1
if lives <= 0:
game_over = True
else:
# Reset positions
pacman.update({
"grid_x": 9, "grid_y": 17,
"progress": 0, "direction": (0, 0),
"next_dir": (0, 0)
})
ghost.update({
"grid_x": 9, "grid_y": 5,
"progress": 0, "direction": (0, 0)
})
def draw():
screen.fill(BLACK)
# Draw maze
for y in range(len(MAZE)):
for x in range(len(MAZE[y])):
if MAZE[y][x] == "#":
pygame.draw.rect(screen, BLUE,
(x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE))
if dots[y][x]:
center = (x*CELL_SIZE + CELL_SIZE//2, y*CELL_SIZE + CELL_SIZE//2)
pygame.draw.circle(screen, WHITE, center, 3)
# Draw Pac-Man
if not game_over:
px = (pacman["grid_x"] + pacman["direction"][0] * pacman["progress"]) * CELL_SIZE
py = (pacman["grid_y"] + pacman["direction"][1] * pacman["progress"]) * CELL_SIZE
pygame.draw.circle(screen, YELLOW, (px + CELL_SIZE//2, py + CELL_SIZE//2), CELL_SIZE//2 - 2)
# Draw Ghost
gx = (ghost["grid_x"] + ghost["direction"][0] * ghost["progress"]) * CELL_SIZE
gy = (ghost["grid_y"] + ghost["direction"][1] * ghost["progress"]) * CELL_SIZE
pygame.draw.rect(screen, RED, (gx, gy, CELL_SIZE, CELL_SIZE))
# Draw UI
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, WHITE)
lives_text = font.render(f"Lives: {lives}", True, WHITE)
screen.blit(score_text, (10, HEIGHT - 50))
screen.blit(lives_text, (WIDTH - 120, HEIGHT - 50))
# Draw game over screen
if game_over:
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 128))
screen.blit(overlay, (0, 0))
go_font = pygame.font.Font(None, 72)
go_text = go_font.render("GAME OVER", True, RED)
restart_text = font.render("Press R to restart", True, GREEN)
screen.blit(go_text, (WIDTH//2 - go_text.get_width()//2, HEIGHT//2 - 50))
screen.blit(restart_text, (WIDTH//2 - restart_text.get_width()//2, HEIGHT//2 + 20))
pygame.display.flip()
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
handle_pacman_input()
if not game_over:
move_character(pacman, MAZE)
move_ghost()
check_collisions()
# Handle queued direction changes
if pacman["next_dir"] != (0, 0) and pacman["progress"] == 0:
next_x = pacman["grid_x"] + pacman["next_dir"][0]
next_y = pacman["grid_y"] + pacman["next_dir"][1]
if 0 <= next_x < len(MAZE[0]) and 0 <= next_y < len(MAZE):
if MAZE[next_y][next_x] != "#":
pacman["direction"] = pacman["next_dir"]
pacman["next_dir"] = (0, 0)
draw()
clock.tick(FPS)