AI-Project/survival/textureatlas.py

29 lines
1003 B
Python
Raw Normal View History

2021-03-14 15:02:14 +01:00
import pygame
class TextureAtlas(object):
def __init__(self, filename):
self.atlas = pygame.image.load(filename).convert()
2021-03-14 18:42:40 +01:00
self.images = {}
2021-03-14 15:02:14 +01:00
def image_at(self, origin, target, color_key=None):
2021-03-14 18:42:40 +01:00
if origin in self.images:
return self.images[origin]
rect = pygame.Rect(origin, target)
2021-03-14 15:02:14 +01:00
image = pygame.Surface(rect.size).convert()
image.blit(self.atlas, (0, 0), rect)
if color_key is not None:
2021-03-14 18:42:40 +01:00
if color_key == -1:
2021-03-14 15:02:14 +01:00
color_key = image.get_at((0, 0))
image.set_colorkey(color_key, pygame.RLEACCEL)
2021-03-14 18:42:40 +01:00
self.images[origin] = image
2021-03-14 15:02:14 +01:00
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)