-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacman.py
More file actions
389 lines (333 loc) · 12.8 KB
/
pacman.py
File metadata and controls
389 lines (333 loc) · 12.8 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
from globals import *
from map import *
from ghosts import *
from netgame import PlayerData
from copy import deepcopy
#######################
### PACMAN ###
#######################
class Pacman:
move_speed = 4/3
anim_spd = 1/2
anim_max = 4
dead_anim_spd = 1/4
dead_anim_max = 12
xoffset = TILE_SIZE
yoffset = TILE_SIZE
def __init__(self, skin=(0,0), x=14, y=23.5, pl_id=0, remote=False):
self.x = x * TILE_SIZE
self.y = y * TILE_SIZE
self.direction = 0
self.speed = 0
self.anim = 3
self.speed_factor = PACMAN_SPEEDS[0]['normal']
self.freeze = 0
self.dots = 0
self.dead = False
self.to_respawn = False
self.active = True
self.shield = 0
self.skin = skin
self.score = 0
self.prev_score = 0
self.combo = 0
self.show_points = -1
self.lives = game_settings['lives']
self.pl_id = pl_id
self.remote = remote
self.ready = True # Netgame player status
self.dots_tosend = [] # Netgame eaten dots
self.ghosts_tosend = [] # Netgame eaten ghosts
def update(self):
'''Update Pacman'''
x2, y2 = get_tile(self.x, self.y)
# Collision with ghosts
# (is checked even if pacman is frozen)
g = ghost_collide(x2,y2)
if g:
if g.state == 'scared' and not g.client_eaten:
# Eat ghost
g.eat_ghost()
self.score += POINTS_GHOST[self.combo]
self.show_points = self.combo
self.combo = min(3, self.combo+1)
if SOUND:
ch_other.play(snd_eat_ghost)
# Workaround to allow client to send eaten ghost data to host
# and prevent ghost from being eaten multiple times
if net_settings['type'] == NET_JOIN:
self.ghosts_tosend.append(g.type)
g.client_eaten = True
elif g.state in ['leave','scatter','chase'] and not self.dead and self.shield < 1:
# Die
self.die()
# Dot freeze
if self.freeze > 0 :
self.freeze -= 1
if self.freeze <= 0:
self.show_points = -1
else:
# Other actions
if self.dead :
# Respawn
if self.to_respawn:
self.respawn()
# Death animation and sound
if self.anim < 0:
if SOUND:
ch_other.play(snd_death[0])
self.anim = 0
self.anim += self.dead_anim_spd
if self.anim >= self.dead_anim_max - 1:
self.freeze = 60
self.to_respawn = True
if SOUND:
ch_other.play(snd_death[1])
else:
# Manage speed
self.manage_speed()
# Shield
self.shield = max(0, self.shield-1)
# Movement
if self.speed > 0 :
# Move pacman
self.x += self.speed * self.speed_factor * intcos(self.direction)
self.y += self.speed * self.speed_factor * intsin(self.direction)
self.x %= MAZE_WIDTH * TILE_SIZE
# Animation
self.anim = (self.anim + self.anim_spd) % self.anim_max
# Collision with walls
if not (dir_free(self.x/TILE_SIZE, self.y/TILE_SIZE, self.direction, -1/2)):
self.speed = 0
self.x, self.y = snap_grid(self.x, self.y)
# Eat dots
if is_dot(x2,y2) or is_pow(x2,y2):
# Pellet effects
if is_dot(x2,y2):
self.freeze = max(self.freeze, 1)
self.score += POINTS_DOT
else:
self.freeze = max(self.freeze, 3)
self.score += POINTS_POW
Ghost.scare_ghosts()
# Remove dot from map
write_map(x2, y2, COLL_VOID)
# Dot counters
self.dots += 1
dot_count['eaten'] += 1
# Spawn fruit
if dot_count['eaten'] in FRUIT_DOTS:
spawn_fruit()
# Sound
if SOUND:
ch_munch[self.pl_id].play(snd_munch[dot_count['sound']])
# Alternate between both munch sounds
dot_count['sound'] = 1 - dot_count['sound']
# Add dot to dot list
if net_settings['type'] != NET_DISABLED:
self.dots_tosend.append((x2,y2))
# Eat fruit
f = fruit_collide(x2,y2)
if f and f.active and not f.draw_score:
f.eat_fruit(self)
if SOUND:
ch_other.play(snd_eat_fruit)
def net_update(self, data):
'''Update player from net data'''
x2, y2 = get_tile(self.x, self.y)
self.x, self.y = data.x, data.y
self.direction = data.dir
self.speed = data.spd
self.skin = data.skin
self.dead = data.dead
self.anim = data.anim
self.score = data.score
self.lives = data.lives
self.show_points = data.show_points
self.ready = data.ready
# Remove dots
for d in data.dots:
if is_pow(d[0], d[1]):
Ghost.scare_ghosts()
if is_pow(d[0], d[1]) or is_dot(d[0], d[1]):
write_map(d[0], d[1], COLL_VOID)
dot_count['eaten'] += 1
# Spawn fruit
if dot_count['eaten'] in FRUIT_DOTS:
spawn_fruit()
# Eat ghosts
for name in data.ghosts:
for g in ghosts:
# Compare ghost name with received data
if g.type == name and g.state != 'eaten':
g.eat_ghost()
# Eat fruit
f = fruit_collide(x2,y2)
if f and f.active and not f.draw_score:
f.eat_fruit(self)
if SOUND:
ch_other.play(snd_eat_fruit)
if self.lives == 0:
# Game over
self.active = False
def net_read_ack(self, m):
'''Read acknowledged data and remove it from the lists'''
# Dots
if m.header == 'dots':
for t in m.content:
# Look for dot tuple in list
tup = (t[0],t[1])
if tup in self.dots_tosend:
self.dots_tosend.remove(tup)
# Ghosts
elif m.header == 'ghosts':
for g in m.content:
if g in self.ghosts_tosend:
self.ghosts_tosend.remove(g)
def net_write(self):
'''Return net data to be sent'''
data = PlayerData(self.x, self.y, self.speed, self.direction, self.anim,\
self.skin, self.dead, self.score, self.lives, deepcopy(self.dots_tosend), deepcopy(self.ghosts_tosend),\
self.show_points, self.ready)
return data
def steer(self, direction):
'''Change direction'''
if not self.dead and self.freeze < 1:
corner_x, corner_y = self.x + TILE_SIZE*CORNERING*intcos(self.direction), self.y + TILE_SIZE*CORNERING*intsin(self.direction)
x2, y2 = get_tile(self.x, self.y)
if dir_free(x2, y2, direction) and not dir_gate(x2, y2, direction) and \
( grid_snapped(corner_x, corner_y) or direction == (self.direction+2)%4 or self.speed == 0 ):
# Snap Pacman to the grid if he changes direction
snap = False
if direction == (self.direction+1)%4 or direction == (self.direction-1)%4:
snap = True
self.direction = direction
self.speed = self.move_speed
if snap:
self.x, self.y = snap_grid(self.x, self.y)
def manage_speed(self):
lvl_id = get_level_id(game_settings['level'])
if Ghost.scared_clock > 0:
self.speed_factor = PACMAN_SPEEDS[lvl_id]['scared']
else:
self.speed_factor = PACMAN_SPEEDS[lvl_id]['normal']
def die(self):
if game_settings['player_nb'] < 2:
# Respawn ghosts in single player mode
ghosts.clear()
self.freeze = 60
if SOUND:
ch_siren.stop()
self.dead = True
self.anim = -1
def action(self):
# Revive player
if len(player)>1 and not self.active and sum(p.lives for p in player)>1:
for p in [pl for pl in player if pl.lives>1]:
p.lives -= 1
# respawn() takes away 1 life
self.lives += 2
self.active = True
self.respawn()
def respawn(self):
self.lives = max(-1, self.lives - 1)
if self.lives == 0:
# Game over
self.active = False
else:
# Respawn
self.x, self.y = start_pos['pacman']
self.x *= TILE_SIZE
self.y *= TILE_SIZE
self.dead = False
self.to_respawn = False
self.anim = 0
if game_settings['player_nb'] > 1:
self.shield = 120
if len(ghosts) < 1:
respawn_ghosts()
self.freeze = 90
for g in ghosts :
g.freeze = 90
def draw(self, x = -1, y = -1, dest = GAME_SURF):
'''Render Pacman on the screen'''
if x < 0 and y < 0:
x, y = self.x, self.y
maze_offset = MAZE_YOFFSET
else:
maze_offset = 0
if self.show_points >= 0:
# Show points for eating ghost
col = self.show_points
x0, y0, w, h = SHEET_POINTS
area = (x0 + w * col,
y0,
w, h)
surf = sprites
else:
# Location of pacman on the sprite sheet
if self.dead:
img = max(0,self.anim) + 16
else:
img = floor(self.anim)%4 + self.direction*4
col = floor(img)%4 + 4 * self.skin[0]
row = floor(img/4) + 7 * self.skin[1]
x0, y0, w, h = SHEET_PACMAN
area = (x0 + w*col,
y0 + h*row,
w, h)
surf = characters
# Draw sprite on screen
if not (self.shield%2 > 0): # Pacman flashes if invincible
coords = (x - self.xoffset, y - self.yoffset + maze_offset, w, h)
dest.blit(surf, coords, area)
#######################
### FRUIT ###
#######################
class Fruit:
xoffset = TILE_SIZE
yoffset = TILE_SIZE
duration = 10
score_duration = 1
def __init__(self):
self.x, self.y = FRUIT_POS
self.x *= TILE_SIZE
self.y *= TILE_SIZE
self.timer = 0
self.draw_score = False
self.active = True
self.remote = False
def update(self):
self.timer += 1
if (not self.draw_score and self.timer >= self.duration*60)\
or (self.draw_score and self.timer >= self.score_duration*60):
self.active = False
def eat_fruit(self, pl):
self.timer = 0
self.draw_score = True
pl.score += FRUIT_SCORE[ get_fruit_type(game_settings['level']) ]
def draw(self):
coords = (self.x - self.xoffset, self.y - self.yoffset + MAZE_YOFFSET)
type = get_fruit_type(game_settings['level'])
if self.draw_score:
x0, y0, w, h = SHEET_POINTS2
if type < 4:
area = (x0 + type*w, y0,\
w, h)
else:
# >=1000
area = (x0 + 4*w, y0 + (type-4)*h,\
2*w, h)
else:
x0, y0, w, h = SHEET_FRUIT
area = (x0 + w*type, y0,\
w, h)
GAME_SURF.blit(sprites, coords, area)
def spawn_fruit():
fruits.append(Fruit())
def fruit_collide(x,y):
for f in fruits:
fx, fy = get_tile(f.x, f.y)
if x == fx and y == fy:
return f
return None