-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet.py
28 lines (24 loc) · 1.14 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
import pygame
import math
class Spritesheet():
def __init__(self, filename, sprite_width, sprite_height):
self.sheet = pygame.image.load(filename).convert_alpha()
self.sprite_width = sprite_width
self.sprite_height = sprite_height
self.num_cols = self.sheet.get_width() / self.sprite_width
self.num_rows = self.sheet.get_height() / self.sprite_height
self.sprites = []
for i in range(self.num_cols * self.num_rows - 1):
self.sprites.append(self.get_sprite_internal(i))
# Returns the sprite at the given frame index.
def get_sprite_internal(self, idx):
x_idx = idx % self.num_cols
y_idx = math.floor(idx / self.num_cols)
start_x = x_idx * self.sprite_width
start_y = y_idx * self.sprite_height
rect = pygame.Rect((start_x, start_y, self.sprite_width, self.sprite_height))
image = pygame.Surface((self.sprite_width, self.sprite_height), flags=pygame.SRCALPHA, depth=32).convert_alpha()
image.blit(self.sheet, (0, 0), rect)
return image
def get_sprite(self, idx):
return self.sprites[idx]