A sprite sheet is a common way to store various sprite's frame in one image, this can be used to produce animated sprites or to store tiles for tile based video games.
A sheet image can be used with a standard sprite object, so you don't need to change the normal behavior of the Sprite class.
This examples show takes a 4 frame sheet, and show a single frame in a Sprite:
from sheet import ImageSheet # Load image image = sf.Image() image.LoadFromFile('pingu.png') # Create image sheet sheet = ImageSheet(image, rows=1, cols=4) # Create a sprite (no changes) sprite = sf.Sprite(image) # Assign a image sheet and draw (its show frame 0) sheet.Assign(sprite) application.Draw(sprite) # Change frame, assign and show frame 2 sheet.SetFrameIndex(2) sheet.Assign(sprite)
# License: Public Domain # Author: Hugo Ruscitti (http://www.losersjuegos.com.ar) from PySFML import sf class ImageSheet: def __init__(self, image, cols=1, rows=1): self.image = image self.len_frames = cols * rows self.cols = cols self.rows = rows self.frame_width = image.GetWidth() / cols self.frame_height = image.GetHeight() / rows self.sub_rect = sf.IntRect(0, 0, self.frame_width, self.frame_height) self.SetFrameIndex(0) def SetFrameIndex(self, index): self.index = index frame_col = index % self.cols frame_row = index / self.cols dx = frame_col * self.frame_width - self.sub_rect.Left dy = frame_row * self.frame_height - self.sub_rect.Top self.sub_rect.Offset(dx, dy) def Assign(self, sprite): "Sets the sprite's image with animation state." sprite.SetImage(self.image) sprite.SetSubRect(self.sub_rect) def NextFrame(self): has_restarted = False current_index = self.index + 1 if current_index >= self.len_frames: current_index = 0 has_restarted = True self.SetFrameIndex(current_index) return has_restarted def GetFrameIndex(self): return self.index