-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteSheet.py
98 lines (89 loc) · 3.33 KB
/
SpriteSheet.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
import pygame
#http://pygame.org/wiki/Spritesheet
class SpriteSheet(object):
def __init__(self, filename):
try:
self.sheet = pygame.image.load(filename).convert()
except pygame.error, message:
print 'Unable to load spritesheet image:', filename
raise SystemExit, message
# Load a specific image from a specific rectangle
def image_at(self, rectangle, colorkey = None):
"Loads image from x,y,x+offset,y+offset"
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
# Load a whole bunch of images and return them as a list
def images_at(self, rects, colorkey = None):
"Loads multiple images, supply a list of coordinates"
return [self.image_at(rect, colorkey) for rect in rects]
# Load a whole strip of images
def load_strip(self, rect, image_count, colorkey = None):
"Loads a strip of images and returns them as a list"
tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3])
for x in range(image_count)]
return self.images_at(tups, colorkey)
def __getitem__(self,loc):
if len(loc)!=4:
return None
if type(loc) != tuple and type(loc)!=list:
return None
return self.image_at(tuple(loc))
class SpriteStripAnim(object):
"""sprite strip animator
This class provides an iterator (iter() and next() methods), and a
__add__() method for joining strips which comes in handy when a
strip wraps to the next row.
"""
def __init__(self, filename, rect, count, colorkey=None, loop=False, frames=1):
"""construct a SpriteStripAnim
filename, rect, count, and colorkey are the same arguments used
by spritesheet.load_strip.
loop is a boolean that, when True, causes the next() method to
loop. If False, the terminal case raises StopIteration.
frames is the number of ticks to return the same image before
the iterator advances to the next image.
"""
self.filename = filename
ss = SpriteSheet(filename)
self.images = ss.load_strip(rect, count, colorkey)
self.i = 0
self.loop = loop
self.frames = frames
self.f = frames
self.stopped = True
def iter(self):
if not self.stopped:
return
self.i = 0
self.f = self.frames
self.start()
return self
def next(self):
if self.stopped:#when stopped just show the first frame.
return self.images[0]
if self.i >= len(self.images):
if not self.loop:
raise StopIteration
else:
self.i = 0
image = self.images[self.i]
self.f -= 1
if self.f == 0:
self.i += 1
self.f = self.frames
return image
def stop(self):
self.stopped = True
self.i = 0
self.f = self.frames
def start(self):
self.stopped = False
def __add__(self, ss):
self.images.extend(ss.images)
return self