/
spritesheet_functions.py
36 lines (26 loc) · 1.12 KB
/
spritesheet_functions.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
"""
This module is used to pull individual sprites from sprite sheets.
I did not create this module. It was given to me by Keenan Knaur
"""
import pygame
import constants
class SpriteSheet(object):
""" Class used to grab images out of a sprite sheet. """
# This points to our sprite sheet image
sprite_sheet = None
def __init__(self, file_name):
""" Constructor. Pass in the file name of the sprite sheet. """
# Load the sprite sheet.
self.sprite_sheet = pygame.image.load(file_name).convert()
def get_image(self, x, y, width, height, color=constants.BLACK):
""" Grab a single image out of a larger spritesheet
Pass in the x, y location of the sprite
and the width and height of the sprite. """
# Create a new blank image
image = pygame.Surface([width, height]).convert()
# Copy the sprite from the large sheet onto the smaller image
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
# Assuming black works as the transparent color
image.set_colorkey(color)
# Return the image
return image