From 03b8a330947dce3731e462c305d9824acea4ee62 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 14 Mar 2021 15:02:14 +0100 Subject: [PATCH] Add texture atlas (sprite sheet) --- survival/textureatlas.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 survival/textureatlas.py diff --git a/survival/textureatlas.py b/survival/textureatlas.py new file mode 100644 index 0000000..4628c60 --- /dev/null +++ b/survival/textureatlas.py @@ -0,0 +1,24 @@ +import pygame + + +class TextureAtlas(object): + def __init__(self, filename): + self.atlas = pygame.image.load(filename).convert() + + def image_at(self, rectangle, color_key=None): + rect = pygame.Rect(rectangle) + image = pygame.Surface(rect.size).convert() + image.blit(self.atlas, (0, 0), rect) + if color_key is not None: + if color_key is -1: + color_key = image.get_at((0, 0)) + image.set_colorkey(color_key, pygame.RLEACCEL) + return image + + def images_at(self, rects, color_key=None): + return [self.image_at(rect, color_key) for rect in rects] + + def load_row(self, rect, image_count, color_key=None): + images = [(rect[0] + rect[2] * x, rect[1], rect[2], rect[3]) + for x in range(image_count)] + return self.images_at(images, color_key)