forked from senweim/JumpKingAtHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspritesheet.py
More file actions
78 lines (46 loc) · 1.79 KB
/
spritesheet.py
File metadata and controls
78 lines (46 loc) · 1.79 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
#!/usr/bin/env python
#
#
#
#
import pygame
import os
class SpriteSheet:
""" Represents spritesheets """
def __init__(self, filename):
""" Load the sheet """
pygame.init()
#try:
self.sheet = pygame.image.load(filename).convert_alpha()
#except pygame.error as e:
# print(f"Unable to load spritesheet image: {filename} ")
# raise SystemExit(e)
def image_at(self, rectangle, colorkey = None):
""" Load a specific image from a specific rectangle."""
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size, pygame.SRCALPHA).convert_alpha()
image.blit(self.sheet, (0, 0), rect)
# if colorkey is not None:
# if colorkey == -1:
# colorkey = image.get_at((0, 0))
# image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
def images_at(self, rects, colorkey = None):
""" Load a whole bunch of images and return them as a list """
return [self.image_at(rect, colorkey) for rect in rects]
def load_strip(self, rect, image_count, colorkey = None):
""" Load a whole strip of images, and return 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 load_column(self, rect, image_count, colorkey = None):
""" Load a whole column of images, and return them as a list """
tups = [(rect[0], rect[1] + rect[3] * y, rect[2], rect[3]) for y in range(image_count)]
return self.images_at(tups, colorkey)
def load_grid(self, rect, image_count, col_count, colorkey = None):
images = []
for y in range(col_count):
tups = [(rect[0] + rect[2] * x, rect[1] + rect[3] * y, rect[2], rect[3]) for x in range(image_count)]
images.extend(self.images_at(tups, colorkey))
return images
if __name__ == "__main__":
jumpking_spritesheet = SpriteSheet("test.jfif")