-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaxman.py
668 lines (551 loc) · 19.1 KB
/
vaxman.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# Pacman in Python with PyGame
# https://github.com/hbokmann/Pacman
import pygame
# defining colors
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
purple = (255, 0, 255)
yellow = (255, 255, 0)
Trollicon = pygame.image.load('images/Trollman.png')
pygame.display.set_icon(Trollicon)
# Add music
pygame.mixer.init()
pygame.mixer.music.load('pacman.mp3')
pygame.mixer.music.play(-1, 0.0)
class Wall(pygame.sprite.Sprite): # This class represents walls
# Constructor function
def __init__(self, x, y, width, height, color):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Make a blue wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
class Player(pygame.sprite.Sprite): # This class represents our vaxman
# Set speed vector
change_x = 0
change_y = 0
# Constructor function
def __init__(self, x, y, filename):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Set height, width
self.image = pygame.image.load(filename).convert()
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
self.prev_x = x
self.prev_y = y
# Clear the speed of the player
def prevdirection(self):
self.prev_x = self.change_x
self.prev_y = self.change_y
# Change the speed of the player
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
# Find a new position for the player
def update(self, walls, gate):
# Get the old position, in case we need to go back to it
old_x = self.rect.left
new_x = old_x + self.change_x
prev_x = old_x + self.prev_x
self.rect.left = new_x
old_y = self.rect.top
new_y = old_y + self.change_y
prev_y = old_y + self.prev_y
# Did this update cause us to hit a wall?
x_collide = pygame.sprite.spritecollide(self, walls, False)
if x_collide:
# Whoops, hit a wall. Go back to the old position
self.rect.left = old_x
else:
self.rect.top = new_y
# Did this update cause us to hit a wall?
y_collide = pygame.sprite.spritecollide(self, walls, False)
if y_collide:
# Whoops, hit a wall. Go back to the old position
self.rect.top = old_y
if gate != False:
gate_hit = pygame.sprite.spritecollide(self, gate, False)
if gate_hit:
self.rect.left = old_x
self.rect.top = old_y
class Ghost(Player): # This class represents the ghosts. It is inherited from the player class
# Change the speed of the ghost
def changespeed(self, list, ghost, turn, steps, l):
try:
z = list[turn][2]
if steps < z:
self.change_x = list[turn][0]
self.change_y = list[turn][1]
steps += 1
else:
if turn < l:
turn += 1
elif ghost == "clyde":
turn = 2
else:
turn = 0
self.change_x = list[turn][0]
self.change_y = list[turn][1]
steps = 0
return [turn, steps]
except IndexError:
return [0, 0]
class Dot(pygame.sprite.Sprite): # This class represents the dots
# It derives from the "Sprite" class in Pygame
# Constructor. Pass in the color of the dots,
# and its x and y position
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the dots, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(white)
self.image.set_colorkey(white)
pygame.draw.ellipse(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
# This class represents the bar at the bottom that the player controls
Pinky_directions = [
[0, -30, 4],
[15, 0, 9],
[0, 15, 11],
[-15, 0, 23],
[0, 15, 7],
[15, 0, 3],
[0, -15, 3],
[15, 0, 19],
[0, 15, 3],
[15, 0, 3],
[0, 15, 3],
[15, 0, 3],
[0, -15, 15],
[-15, 0, 7],
[0, 15, 3],
[-15, 0, 19],
[0, -15, 11],
[15, 0, 9]
]
Blinky_directions = [
[0, -15, 4],
[15, 0, 9],
[0, 15, 11],
[15, 0, 3],
[0, 15, 7],
[-15, 0, 11],
[0, 15, 3],
[15, 0, 15],
[0, -15, 15],
[15, 0, 3],
[0, -15, 11],
[-15, 0, 3],
[0, -15, 11],
[-15, 0, 3],
[0, -15, 3],
[-15, 0, 7],
[0, -15, 3],
[15, 0, 15],
[0, 15, 15],
[-15, 0, 3],
[0, 15, 3],
[-15, 0, 3],
[0, -15, 7],
[-15, 0, 3],
[0, 15, 7],
[-15, 0, 11],
[0, -15, 7],
[15, 0, 5]
]
Inky_directions = [
[30, 0, 2],
[0, -15, 4],
[15, 0, 10],
[0, 15, 7],
[15, 0, 3],
[0, -15, 3],
[15, 0, 3],
[0, -15, 15],
[-15, 0, 15],
[0, 15, 3],
[15, 0, 15],
[0, 15, 11],
[-15, 0, 3],
[0, -15, 7],
[-15, 0, 11],
[0, 15, 3],
[-15, 0, 11],
[0, 15, 7],
[-15, 0, 3],
[0, -15, 3],
[-15, 0, 3],
[0, -15, 15],
[15, 0, 15],
[0, 15, 3],
[-15, 0, 15],
[0, 15, 11],
[15, 0, 3],
[0, -15, 11],
[15, 0, 11],
[0, 15, 3],
[15, 0, 1],
]
Clyde_directions = [
[-30, 0, 2],
[0, -15, 4],
[15, 0, 5],
[0, 15, 7],
[-15, 0, 11],
[0, -15, 7],
[-15, 0, 3],
[0, 15, 7],
[-15, 0, 7],
[0, 15, 15],
[15, 0, 15],
[0, -15, 3],
[-15, 0, 11],
[0, -15, 7],
[15, 0, 3],
[0, -15, 11],
[15, 0, 9],
]
pl = len(Pinky_directions) - 1
bl = len(Blinky_directions) - 1
il = len(Inky_directions) - 1
cl = len(Clyde_directions) - 1
def duplicate_ghost(gi, monster_list, asl):
if 0 < len(gi[0]) < 32:
# create X blinkys (X = the number of existing blinkys)
for k in range(len(gi[0])):
b = Ghost(w, b_h, "images/Blinky.png")
gi[0].append(b)
monster_list.add(b)
asl.add(b)
if 0 < len(gi[1]) < 32:
# create X pinkys (X = the number of existing pinkys)
for k in range(len(gi[1])):
p = Ghost(w, b_h, "images/Pinky.png")
gi[1].append(p)
monster_list.add(p)
asl.add(p)
if 0 < len(gi[2]) < 32:
# create X inkys (X = the number of existing inkys)
for k in range(len(gi[2])):
i = Ghost(w, b_h, "images/Inky.png")
gi[2].append(i)
monster_list.add(i)
asl.add(i)
if 0 < len(gi[3]) < 32:
# create X clydes (X = the number of existing clydes)
for k in range(len(gi[3])):
c = Ghost(w, b_h, "images/Clyde.png")
gi[3].append(c)
monster_list.add(c)
asl.add(c)
pygame.time.set_timer(pygame.USEREVENT + 1,
1000 * 30) # reset the timer
# This creates all the walls in room 1
def setupRoomOne(all_sprites_list):
# Make the walls. (x_pos, y_pos, width, height)
wall_list = pygame.sprite.RenderPlain()
# This is a list of walls. Each is in the form [x, y, width, height]
walls = [[0, 0, 6, 600],
[0, 0, 600, 6],
[0, 600, 606, 6],
[600, 0, 6, 606],
[300, 0, 6, 66],
[60, 60, 186, 6],
[360, 60, 186, 6],
[60, 120, 66, 6],
[60, 120, 6, 126],
[180, 120, 246, 6],
[300, 120, 6, 66],
[480, 120, 66, 6],
[540, 120, 6, 126],
[120, 180, 126, 6],
[120, 180, 6, 126],
[360, 180, 126, 6],
[480, 180, 6, 126],
[180, 240, 6, 126],
[180, 360, 246, 6],
[420, 240, 6, 126],
[240, 240, 42, 6],
[324, 240, 42, 6],
[240, 240, 6, 66],
[240, 300, 126, 6],
[360, 240, 6, 66],
[0, 300, 66, 6],
[540, 300, 66, 6],
[60, 360, 66, 6],
[60, 360, 6, 186],
[480, 360, 66, 6],
[540, 360, 6, 186],
[120, 420, 366, 6],
[120, 420, 6, 66],
[480, 420, 6, 66],
[180, 480, 246, 6],
[300, 480, 6, 66],
[120, 540, 126, 6],
[360, 540, 126, 6]
]
# Loop through the list. Create the wall, add it to the list
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], blue)
wall_list.add(wall)
all_sprites_list.add(wall)
# return our new list
return wall_list
# This is where the gate that allows ghosts to come out of
def setupGate(all_sprites_list):
gate = pygame.sprite.RenderPlain()
gate.add(Wall(282, 242, 42, 2, white))
all_sprites_list.add(gate)
return gate
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 606x606 sized screen
screen = pygame.display.set_mode([606, 606])
# Set the title of the window
pygame.display.set_caption('Vaxman')
# Create a surface we can draw on
background = pygame.Surface(screen.get_size())
# Used for converting color maps and such
background = background.convert()
# Fill the screen with a black background
background.fill(black)
# Initiate a pygame clock to control the frame rate of our game
clock = pygame.time.Clock()
# setting font
pygame.font.init()
font = pygame.font.Font("freesansbold.ttf", 24)
# default locations for Pacman and monsters
w = 303 - 16 # Width
p_h = (7 * 60) + 19 # Pacman height
m_h = (4 * 60) + 19 # Monster height
b_h = (3 * 60) + 19 # Binky height
i_w = 303 - 16 - 32 # Inky width
c_w = 303 + (32 - 16) # Clyde width
def startGame():
# render groups
all_sprites_list = pygame.sprite.RenderPlain()
dot_list = pygame.sprite.RenderPlain()
monster_list = pygame.sprite.RenderPlain()
pacman_collide = pygame.sprite.RenderPlain()
wall_list = setupRoomOne(all_sprites_list)
# initialise gate
gate = setupGate(all_sprites_list)
p_turn = 0
p_steps = 0
b_turn = 0
b_steps = 0
i_turn = 0
i_steps = 0
c_turn = 0
c_steps = 0
# Initialising the objects of our game
Pacman = Player(w, p_h, "images/Trollman.png")
all_sprites_list.add(Pacman)
pacman_collide.add(Pacman)
Blinky = Ghost(w, b_h, "images/Blinky.png")
monster_list.add(Blinky)
all_sprites_list.add(Blinky)
Pinky = Ghost(w, m_h, "images/Pinky.png")
monster_list.add(Pinky)
all_sprites_list.add(Pinky)
Inky = Ghost(i_w, m_h, "images/Inky.png")
monster_list.add(Inky)
all_sprites_list.add(Inky)
Clyde = Ghost(c_w, m_h, "images/Clyde.png")
monster_list.add(Clyde)
all_sprites_list.add(Clyde)
blinky_instances = [Blinky]
pinky_instances = [Pinky]
inky_instances = [Inky]
clyde_instances = [Clyde]
# list of all ghosts
ghost_instances = [blinky_instances,
pinky_instances,
inky_instances,
clyde_instances]
# set the timer before the game loop
pygame.time.set_timer(pygame.USEREVENT + 1, 1000 * 30)
# instantiate dots
for row in range(19):
for column in range(19):
if (row == 7 or row == 8) and (column == 8 or column == 9 or column == 10):
continue
else:
dot = Dot(yellow, 4, 4)
# Set a random location for the dot
dot.rect.x = (30 * column + 6) + 26
dot.rect.y = (30 * row + 6) + 26
b_collide = pygame.sprite.spritecollide(
dot, wall_list, False)
p_collide = pygame.sprite.spritecollide(
dot, pacman_collide, False)
if b_collide:
continue
elif p_collide:
continue
else:
# Add the dot to the list of objects
dot_list.add(dot)
all_sprites_list.add(dot)
bll = len(dot_list)
score = 0
running = True
i = 0
while running:
# EVENT PROCESSING
for event in pygame.event.get():
# CHANGE - add timer for doubling the ghosts
if event.type == pygame.USEREVENT + 1: # if the timer is up
duplicate_ghost(ghost_instances, monster_list,
all_sprites_list)
# CHANGE - quit game
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Pacman.changespeed(-30, 0)
if event.key == pygame.K_RIGHT:
Pacman.changespeed(30, 0)
if event.key == pygame.K_UP:
Pacman.changespeed(0, -30)
if event.key == pygame.K_DOWN:
Pacman.changespeed(0, 30)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
Pacman.changespeed(30, 0)
if event.key == pygame.K_RIGHT:
Pacman.changespeed(-30, 0)
if event.key == pygame.K_UP:
Pacman.changespeed(0, 30)
if event.key == pygame.K_DOWN:
Pacman.changespeed(0, -30)
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
Pacman.update(wall_list, gate)
# Updating all pinkys
for i in range(len(pinky_instances)):
returned = pinky_instances[i].changespeed(
Pinky_directions, False, p_turn, p_steps, pl)
p_turn = returned[0]
p_steps = returned[1]
pinky_instances[i].changespeed(
Pinky_directions, False, p_turn, p_steps, pl)
pinky_instances[i].update(wall_list, False)
# Updating all blinkys
for i in range(len(blinky_instances)):
returned = blinky_instances[i].changespeed(
Blinky_directions, False, b_turn, b_steps, bl)
b_turn = returned[0]
b_steps = returned[1]
blinky_instances[i].changespeed(
Blinky_directions, False, b_turn, b_steps, bl)
blinky_instances[i].update(wall_list, False)
# Updating all inkys
for i in range(len(inky_instances)):
returned = inky_instances[i].changespeed(
Inky_directions, False, i_turn, i_steps, il)
i_turn = returned[0]
i_steps = returned[1]
inky_instances[i].changespeed(
Inky_directions, False, i_turn, i_steps, il)
inky_instances[i].update(wall_list, False)
# Updating all clydes
for i in range(len(clyde_instances)):
returned = clyde_instances[i].changespeed(
Clyde_directions, False, c_turn, c_steps, cl)
c_turn = returned[0]
c_steps = returned[1]
clyde_instances[i].changespeed(
Clyde_directions, False, c_turn, c_steps, cl)
clyde_instances[i].update(wall_list, False)
# See if the Pacman dot has collided with anything.
dots_hit_list = pygame.sprite.spritecollide(Pacman, dot_list, True)
# Check the list of collisions.
if len(dots_hit_list) > 0:
score += len(dots_hit_list)
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
screen.fill(black)
wall_list.draw(screen)
gate.draw(screen)
all_sprites_list.draw(screen)
monster_list.draw(screen)
text = font.render("Score: " + str(score) + "/" + str(bll), True, red)
screen.blit(text, [10, 10])
textg = font.render("Ghosts: " + str(len(monster_list)), True, red)
screen.blit(textg, [450, 10])
if score == bll:
runningxt("Congratulations, you won!", 145, all_sprites_list, dot_list, monster_list,
pacman_collide, wall_list, gate)
monster_hit_list = pygame.sprite.spritecollide(
Pacman, monster_list, False)
if monster_hit_list:
for monster in monster_hit_list:
# remove all instances from monster_list that are in monster_hit_list
monster_list.remove(monster)
# remove from instance lists so only the ghosts remaining can multiply
if monster is Blinky:
blinky_instances.remove(monster)
elif monster is Pinky:
pinky_instances.remove(monster)
elif monster is Clyde:
clyde_instances.remove(monster)
elif monster is Inky:
inky_instances.remove(monster)
# remove all instances from all_sprites_list that are in monster_hit_list
all_sprites_list.remove(monster)
if len(monster_list) >= 128:
runningxt("Game Over", 235, all_sprites_list, dot_list, monster_list, pacman_collide,
wall_list, gate)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(10)
def runningxt(message, left, all_sprites_list, dot_list, monster_list, pacman_collide, wall_list, gate):
while True:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_RETURN:
del all_sprites_list
del dot_list
del monster_list
del pacman_collide
del wall_list
del gate
startGame()
# Grey background
w = pygame.Surface((400, 200)) # the size of your rect
w.set_alpha(10) # alpha level
w.fill((128, 128, 128)) # this fills the entire surface
screen.blit(w, (100, 200)) # (0,0) are the top-left coordinates
# Won or lost
text1 = font.render(message, True, white)
screen.blit(text1, [left, 233])
text2 = font.render("To play again, press ENTER.", True, white)
screen.blit(text2, [135, 303])
text3 = font.render("To quit, press ESCAPE.", True, white)
screen.blit(text3, [165, 333])
pygame.display.flip()
clock.tick(10)
startGame()
pygame.quit()