-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacman.py
More file actions
205 lines (173 loc) · 7.08 KB
/
pacman.py
File metadata and controls
205 lines (173 loc) · 7.08 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
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
CYAN = (0, 255, 255) # New color for vulnerable ghosts
PACMAN_RADIUS = 30
GHOST_RADIUS = 25
PELLET_RADIUS = 5
POWERUP_RADIUS = 10
POWERUP_TIME = 5000 # milliseconds
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man")
# Clock to control the frame rate
clock = pygame.time.Clock()
# Pac-Man properties
pacman_x = WIDTH // 2
pacman_y = HEIGHT // 2
pacman_direction = 0 # 0: right, 1: up, 2: left, 3: down
# Ghost properties
ghosts = [{'x': random.randint(50, WIDTH-50),
'y': random.randint(50, HEIGHT-50),
'direction': random.choice([0, 1, 2, 3]),
'vulnerable': False,
'vulnerable_timer': 0} for _ in range(3)]
# Pellet properties
pellets = [{'x': random.randint(50, WIDTH-50),
'y': random.randint(50, HEIGHT-50)} for _ in range(20)]
# Power-up properties
powerups = []
# Score
score = 0
# Game state
game_active = False
game_over = False
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and not game_active:
game_active = True
game_over = False
pacman_x = WIDTH // 2
pacman_y = HEIGHT // 2
pacman_direction = 0
ghosts = [{'x': random.randint(50, WIDTH - 50),
'y': random.randint(50, HEIGHT - 50),
'direction': random.choice([0, 1, 2, 3]),
'vulnerable': False,
'vulnerable_timer': 0} for _ in range(3)]
pellets = [{'x': random.randint(50, WIDTH - 50),
'y': random.randint(50, HEIGHT - 50)} for _ in range(20)]
powerups = []
score = 0
if game_active:
# Handle key events to change Pac-Man's direction
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
pacman_direction = 0
elif keys[pygame.K_UP]:
pacman_direction = 1
elif keys[pygame.K_LEFT]:
pacman_direction = 2
elif keys[pygame.K_DOWN]:
pacman_direction = 3
# Update Pac-Man's position based on direction
if pacman_direction == 0:
pacman_x = (pacman_x + 5) % WIDTH
elif pacman_direction == 1:
pacman_y = (pacman_y - 5) % HEIGHT
elif pacman_direction == 2:
pacman_x = (pacman_x - 5) % WIDTH
elif pacman_direction == 3:
pacman_y = (pacman_y + 5) % HEIGHT
# Check for collisions with pellets
for pellet in pellets:
distance = pygame.math.Vector2(pacman_x - pellet['x'], pacman_y - pellet['y']).length()
if distance < PACMAN_RADIUS + PELLET_RADIUS:
pellets.remove(pellet)
score += 10
# Check for collisions with power-ups
for powerup in powerups:
distance = pygame.math.Vector2(pacman_x - powerup['x'], pacman_y - powerup['y']).length()
if distance < PACMAN_RADIUS + POWERUP_RADIUS:
powerups.remove(powerup)
for ghost in ghosts:
ghost['vulnerable'] = True
ghost['vulnerable_timer'] = pygame.time.get_ticks() + POWERUP_TIME
# Update ghost positions and check for collisions with Pac-Man
for ghost in ghosts:
# Update ghost position based on direction
if ghost['direction'] == 0:
ghost['x'] = (ghost['x'] + 3) % WIDTH
elif ghost['direction'] == 1:
ghost['y'] = (ghost['y'] - 3) % HEIGHT
elif ghost['direction'] == 2:
ghost['x'] = (ghost['x'] - 3) % WIDTH
elif ghost['direction'] == 3:
ghost['y'] = (ghost['y'] + 3) % HEIGHT
# Check for collisions with Pac-Man
distance = pygame.math.Vector2(pacman_x - ghost['x'], pacman_y - ghost['y']).length()
if distance < PACMAN_RADIUS + GHOST_RADIUS:
if ghost['vulnerable']:
ghosts.remove(ghost)
score += 50
else:
game_active = False
game_over = True
# Reverse direction if hitting the screen edge
if ghost['x'] <= GHOST_RADIUS or ghost['x'] >= WIDTH - GHOST_RADIUS:
ghost['direction'] = (ghost['direction'] + 2) % 4
if ghost['y'] <= GHOST_RADIUS or ghost['y'] >= HEIGHT - GHOST_RADIUS:
ghost['direction'] = (ghost['direction'] + 2) % 4
# Check and update vulnerable state
if ghost['vulnerable'] and pygame.time.get_ticks() > ghost['vulnerable_timer']:
ghost['vulnerable'] = False
# Add new power-ups randomly, only if there are still pellets
if len(powerups) < 3 and pellets:
powerups.append({'x': random.randint(50, WIDTH-50), 'y': random.randint(50, HEIGHT-50)})
# Check if all pellets are eaten
if not pellets:
game_active = False
game_over = True
# Check if all ghosts are dead
if not ghosts:
game_active = False
game_over = True
# Draw the background
screen.fill(BLACK)
if game_active:
# Draw pellets
for pellet in pellets:
pygame.draw.circle(screen, YELLOW, (pellet['x'], pellet['y']), PELLET_RADIUS)
# Draw power-ups
for powerup in powerups:
pygame.draw.circle(screen, CYAN, (powerup['x'], powerup['y']), POWERUP_RADIUS)
# Draw ghosts
for ghost in ghosts:
color = CYAN if ghost['vulnerable'] else RED
pygame.draw.circle(screen, color, (ghost['x'], ghost['y']), GHOST_RADIUS)
# Draw Pac-Man
pygame.draw.circle(screen, YELLOW, (pacman_x, pacman_y), PACMAN_RADIUS)
# Display score
font = pygame.font.Font(None, 36)
score_text = font.render("Score: {}".format(score), True, YELLOW)
screen.blit(score_text, (10, 10))
elif game_over:
# Display game over message
font = pygame.font.Font(None, 50)
game_over_text = font.render("Game Over", True, YELLOW)
game_over_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 30))
screen.blit(game_over_text, game_over_rect)
# Display final score
final_score_text = font.render("Final Score: {}".format(score), True, YELLOW)
final_score_rect = final_score_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 30))
screen.blit(final_score_text, final_score_rect)
# Display restart instructions
restart_text = font.render("Click to Restart", True, YELLOW)
restart_rect = restart_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 90))
screen.blit(restart_text, restart_rect)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)