-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteLib.py
589 lines (446 loc) · 17.1 KB
/
SpriteLib.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
# import system modules
import random
import os.path
import glob
# import basic pygame modules
import pygame
# import my own modules
import Globals
import SoundUtil
#
# Using the bitmap image, create a mask for collisions
# It uses the color key to clear to clear the mask so
# transparent parts of the image don't collide
#
def maskFromSurface(surface, threshold = 127):
# get the mask from the image
mask = pygame.mask.Mask(surface.get_size())
# get the color for transparency
key = surface.get_colorkey()
# if there is a transparent color
if key:
# iterate over all the image pixels
for y in range(surface.get_height()):
for x in range(surface.get_width()):
# if the pixel is not transparent, set the pixel for collision
if surface.get_at((x + 0.1, y + 0.1)) != key:
mask.set_at((x, y), 1)
else: # there is no transparent color
for y in range(surface.get_height()):
for x in range (surface.get_width()):
if surface.get_at((x, y))[3] > threshold:
mask.set_at((x, y), 1)
return mask
# each type of game object gets an init and an
# update function. the update function is called
# once per cur_frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
def load_image(file, scale = 1):
file = os.path.join(Globals.ART_DIR, file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit, 'Could not load image "%s" %s' % (file, pygame.get_error())
if scale > 1:
surface = pygame.transform.scale(surface, (surface.get_width()*scale, surface.get_height()*scale))
return surface.convert_alpha()
def load_images(file_list, scale = 1):
image_list = []
for file in file_list:
img = load_image(file, scale)
image_list.append(img)
return image_list
def load_images_pattern(file_pattern, scale = 1):
file_pattern = os.path.join(Globals.ART_DIR, file_pattern)
file_list = glob.glob(file_pattern)
#print file_pattern
if not file_list:
print "No file list from pattern"
return
image_list = []
for file in file_list:
#print file
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit, 'Could not load image "%s" %s' % (file, pygame.get_error())
if scale > 1:
surface = pygame.transform.scale(surface, (surface.get_width()*scale, surface.get_height()*scale))
img = surface.convert_alpha()
image_list.append(img)
if 0 == len(image_list):
print "Error no image list"
#print image_list
return image_list
class GameData(pygame.sprite.Sprite):
scale = Globals.PLAYER_SCALE
@staticmethod
def loadImages(file_name_list, ObjType):
ObjType.file_name_list = file_name_list
ObjType.image_list = load_images(ObjType.file_name_list, ObjType.scale)
ObjType.num_images = len(ObjType.image_list)
def sanityCheck(self):
if len(type(self).image_list) == 0:
raise SystemExit, 'Game object of type: %s has an empty image list.' % type(self)
if type(self).num_images == 0:
raise SystemExit, 'Game object of type: %s has no viewable frames.' % type(self)
if not self.containers:
raise SystemExit, "Game object has no container, type; '%s'" % type(self)
def __init__(self):
self.sanityCheck()
super(GameData, self).__init__(self.containers)
#self.cur_frame = 0
class AnimGameData(GameData):
def __init__(self, anim_cycle):
super(AnimGameData, self).__init__()
self.cur_frame = 0
self.image = self.image_list[self.cur_frame]
self.anim_cycle = anim_cycle
@staticmethod
def loadImages(file_pattern, ObjType):
file_pattern = os.path.join(Globals.ART_DIR, file_pattern)
ObjType.file_name_list = glob.glob(file_pattern)
#print file_pattern
if not ObjType.file_name_list:
raise SystemExit, "No file list from pattern '%s'" % file_pattern
return
ObjType.image_list = []
for file in ObjType.file_name_list:
#print file
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit, 'Could not load image "%s" %s' % (file, pygame.get_error())
if ObjType.scale > 1:
surface = pygame.transform.scale(surface, (surface.get_width()*ObjType.scale, surface.get_height()*ObjType.scale))
img = surface.convert_alpha()
ObjType.image_list.append(img)
ObjType.num_images = len(ObjType.image_list)
if 0 == ObjType.num_images:
raise SystemExit, "Error no image list"
#print image_list
def nextAnimImg(self):
if self.num_images == 0:
return
self.cur_frame += 1
self.image = self.image_list[self.cur_frame / self.anim_cycle % Globals.INVADER_NUM_FRAMES]
class Ufo(GameData):
speed = Globals.SPEED_UFO
image_list = []
num_images = 0
file_name_list = []
ufo_overhead = False
score = Globals.SCORE_UFO
reload_time = 0
def __init__(self):
super(Ufo, self).__init__()
self.image = self.image_list[0]
self.rect = self.image.get_rect()
self.width = self.rect.right
self.rect.move_ip(-self.width, Globals.POS_UFO_Y)
Ufo.ufo_overhead = True
Ufo.alert_sound.play(Globals.SOUND_LOOP_FOREVER, 0, Globals.SOUND_UFO_FADEOUT_MS)
def takeHit(self):
UfoExplosion(self)
self.kill()
self.rect.move_ip(-20, -20)
Ufo.alert_sound.stop()
def update(self):
self.rect.move_ip(Ufo.speed, 0)
if self.rect[0] > Globals.SCREEN_WIDTH:
Ufo.ufo_overhead = False
Ufo.alert_sound.fadeout(Globals.SOUND_UFO_FADEOUT_MS)
self.kill()
else:
Ufo.ufo_overhead = True
class Player(GameData):
speed = Globals.SPEED_PLAYER
image_list = []
num_images = 0
file_name_list = []
explosion_sound = 0
#lives = 3
shoot_sound = 0
@staticmethod
def static_init(container_list):
Player.explosion_sound = SoundUtil.load_sound(Globals.PLAYER_EXPLOSION)
Player.loadImages([ Globals.LASER_BASE_FILE ], Player)
Player.containers = container_list
Player.shoot_sound = SoundUtil.load_sound(Globals.PLAYER_SHOT)
def __init__(self):
super(Player, self).__init__()
self.image = self.image_list[0]
self.rect = self.image.get_rect(midbottom = Globals.SCREENRECT.move(0, -20).midbottom)
self.reloading = 0
self.origtop = self.rect.top
self.lives = 3
def fire(self):
LaserShot(self.gunpos())
Player.shoot_sound.play()
def move(self, direction):
self.rect.move_ip(direction * Player.speed, 0)
self.rect = self.rect.clamp(Globals.SCREENRECT)
def gunpos(self):
pos = self.rect.centerx
return pos, self.rect.top
def hasAnotherLife(self):
if self.lives > 0:
return True
return False
def handleDeath(self):
Ufo.explosion_sound.play()
PlayerExplosion(self)
self.lives = self.lives - 1
self.kill()
class BaseShelter(GameData):
speed = Globals.SPEED_PLAYER
image_list = []
num_images = 0
file_name_list = []
def __init__(self, screen_loc):
super(BaseShelter, self).__init__()
self.image = self.image_list[0].copy()
self.mask = maskFromSurface(self.image)
self.rect = self.image.get_rect(topleft = screen_loc)
self.screen_loc = screen_loc
self.hit_count = 0
def bombHit(self, bomb, reverseY = False):
loc = pygame.sprite.collide_mask(self, bomb)
if not loc:
return
surfarray = pygame.surfarray.pixels2d(self.image)
if not len(surfarray):
return
self.hit_count += 1
BombOnShelterExplosion(bomb)
bomb.kill()
maxX = self.rect[2] - 1
maxY = self.rect[3] - 1
if reverseY:
inc = -1
start = loc[1] + 10
end = loc[1] - 8
else:
inc = 1
start = loc[1] - 8
end = loc[1] + 8
for x in range(loc[0] - 8, loc[0] + 8):
if x < 0 or x > maxX:
continue
for y in range(start, end, inc):
if y < 0 or y > maxY:
continue
surfarray[x][y] = 0
self.mask = maskFromSurface(self.image)
class BaseInvader(AnimGameData):
speed = Globals.INVADER_SPEED
image_list = []
invader_explosion = 0
num_bombs = 0
def __init__(self):
AnimGameData.__init__(self, Globals.INVADER_ANIM_CYCLE)
self.rect = self.image.get_rect()
self.rect.move_ip(0, 50)
self.facing = random.choice((-1, 1)) * BaseInvader.speed
if self.facing < 0:
self.rect.right = Globals.SCREENRECT.right
@staticmethod
def static_init():
BaseInvader.invader_explosion = SoundUtil.load_sound(Globals.INVADER_SHOT)
InvList = [ InvaderC, InvaderB, InvaderA ]
for InvType in InvList:
if 0 == len(InvType.file_list):
raise SystemExit, "BaseInvader method loadImages failed - no image files lists!"
InvType.image_list = load_images(InvType.file_list, BaseInvader.scale)
InvType.num_images = len(InvType.image_list)
if 0 == InvType.num_images:
raise SystemExit, "BaseInvader method loadImages failed - no images were loaded!"
def kill(self):
BaseInvader.invader_explosion.play()
super(BaseInvader, self).kill()
InvaderExplosion(self)
def dropBomb(self):
if BaseInvader.num_bombs < 3:
BaseInvader.num_bombs += 1
InvaderBomb(self)
print "Boms away num = %d" % BaseInvader.num_bombs
else:
print "Would have dropped a bomb num = %d" % BaseInvader.num_bombs
@staticmethod
def bombExploded():
BaseInvader.num_bombs -= 1
print "Bomb exploded num = %d" % BaseInvader.num_bombs
def update(self):
self.rect.move_ip(self.facing, 0)
if not Globals.SCREENRECT.contains(self.rect):
self.facing = -self.facing;
self.rect.top = self.rect.bottom + 1
self.rect = self.rect.clamp(Globals.SCREENRECT)
self.nextAnimImg()
if not int(random.random() * Globals.BOMB_ODDS):
self.dropBomb()
class InvaderC(BaseInvader):
image_list = []
file_list = ['invader_c_0.png', 'invader_c_1.png' ]
score = Globals.SCORE_INVADER_C
def __init__(self):
super(InvaderC, self).__init__()
class InvaderB(BaseInvader):
image_list = []
file_list = ['invader_b_0.png', 'invader_b_1.png' ]
score = Globals.SCORE_INVADER_B
def __init__(self):
super(InvaderB, self).__init__()
class InvaderA(BaseInvader):
image_list = []
file_list = ['invader_a_0.png', 'invader_a_1.png' ]
score = Globals.SCORE_INVADER_A
def __init__(self):
super(InvaderA, self).__init__()
class BaseExplosion(pygame.sprite.Sprite):
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE * 6
animcycle = Globals.EXPLOSION_ANIM_CYCLE
scale = Globals.PLAYER_SCALE
@staticmethod
def loadImages():
InvList = [ InvaderExplosion, PlayerExplosion, BombOnShelterExplosion, BombOnShelterExplosion,
BombHitsGroundExplosion, BombInSkyExplosion, BombHitsLaserExplosion, UfoExplosion ]
for InvType in InvList:
if 0 == len(InvType.file_list):
raise SystemExit, "BaseInvader method loadImages failed - no image files lists!"
InvType.image_list = load_images(InvType.file_list, BaseInvader.scale)
if 0 == len(InvType.image_list):
raise SystemExit, "BaseInvader method loadImages failed - no images were loaded!"
InvType.anim_frames = len(InvType.image_list)
InvType.containers = Globals.GameData.all
def __init__(self, actor):
super(BaseExplosion, self).__init__(self.containers)
self.image = self.image_list[0]
self.rect = self.image.get_rect(center = actor.rect.center)
self.life = self.defaultlife
def update(self):
self.life = self.life - 1
self.image = self.image_list[self.life / self.animcycle % self.anim_frames]
if self.life <= 0:
self.kill()
class InvaderExplosion(BaseExplosion):
image_list = []
file_list = [ 'invader_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_INVADER_LIFE
def __init__(self, actor):
super(InvaderExplosion, self).__init__(actor)
class PlayerExplosion(BaseExplosion):
image_list = []
file_list = [ 'laser_base_exp_0.png', 'laser_base_exp_1.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_PLAYER
def __init__(self, actor):
super(PlayerExplosion, self).__init__(actor)
class BombOnShelterExplosion(BaseExplosion):
image_list = []
file_list = [ 'bomb_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_SHELTER
def __init__(self, actor):
super(BombOnShelterExplosion, self).__init__(actor)
class BombHitsGroundExplosion(BaseExplosion):
image_list = []
file_list = [ 'bomb_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_BOMB
def __init__(self, actor):
super(BombHitsGroundExplosion, self).__init__(actor)
class BombInSkyExplosion(BaseExplosion):
image_list = []
file_list = [ 'bomb_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_BOMB
def __init__(self, actor):
super(BombInSkyExplosion, self).__init__(actor)
class BombHitsLaserExplosion(BaseExplosion):
image_list = []
file_list = [ 'bomb_air_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_BOMB
def __init__(self, actor):
super(BombHitsLaserExplosion, self).__init__(actor)
class UfoExplosion(BaseExplosion):
image_list = []
file_list = [ 'ufo_exp_0.png' ]
anim_frames = 0
defaultlife = Globals.EXPLOSION_DEFAULT_LIFE_UFO
alert_sound = 0
def __init__(self, actor):
super(UfoExplosion, self).__init__(actor)
def update(self):
super(UfoExplosion, self).update()
if not self.alive():
Ufo.ufo_overhead = False
Ufo.alert_sound.fadeout(Globals.SFX_FADEOUT_UFO)
#self.actor.rect.move_ip(-20, -20)
class LaserShot(pygame.sprite.Sprite):
speed = Globals.LASER_SHOT_SPEED
image_list = []
scale = Globals.PLAYER_SCALE
num_frames = 0
score = 0
@staticmethod
def loadImage(file_name):
LaserShot.image_list.append(load_image(file_name, LaserShot.scale))
LaserShot.num_frames = len(LaserShot.image_list)
def __init__(self, pos):
super(LaserShot, self).__init__(self.containers)
self.image = self.image_list[0]
self.rect = self.image.get_rect(midbottom = pos)
self.cur_frame = 0
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.top <= 0:
self.kill()
class InvaderBomb(pygame.sprite.Sprite):
speed = 9
image_list = []
scale = Globals.INVADER_SCALE
num_frames = 0
@staticmethod
def loadImage(file_pattern):
InvaderBomb.image_list = load_images_pattern(Globals.BOMB_FILE, InvaderBomb.scale)
InvaderBomb.num_frames = len(InvaderBomb.image_list)
def __init__(self, Invader):
super(InvaderBomb, self).__init__(self.containers)
self.cur_frame = 0
if not InvaderBomb.image_list:
print "Crap!"
self.image = InvaderBomb.image_list[self.cur_frame]
self.rect = self.image.get_rect(midbottom =
Invader.rect.move(0, 5).midbottom)
def update(self):
self.cur_frame = (self.cur_frame + 1) % InvaderBomb.num_frames
self.image = self.image_list[self.cur_frame]
self.rect.move_ip(0, self.speed)
if self.rect.bottom >= Globals.SCREEN_HEIGHT:
BombHitsGroundExplosion(self)
self.kill()
def kill(self):
BaseInvader.bombExploded()
super(InvaderBomb, self).kill()
class Score(pygame.sprite.Sprite):
def __init__(self):
super(Score, self).__init__()
self.font = pygame.font.Font(None, Globals.FONT_SIZE)
self.font.set_italic(1)
self.color = pygame.Color('white')
self.lastscore = -1
self.score = 0
self.update()
self.rect = self.image.get_rect().move(Globals.POS_SCORE_X, Globals.POS_SCORE_Y)
def addToScore(self, score_inc):
self.score += score_inc
def update(self):
if self.score != self.lastscore:
self.lastscore = self.score
msg = "Score: %d" % self.score
self.image = self.font.render(msg, 0, self.color)