0 Members and 1 Guest are viewing this topic.
class dImageSheet(sf.Drawable): def __init__(self, image, cols=1, rows=1): self.sprite = sf.Sprite(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 NextFrame(self): current_index = self.index + 1 if current_index >= self.len_frames: current_index = 0 self.SetFrameIndex(current_index) def Render(self, window): self.sprite.SetSubRect(self.sub_rect) window.Draw(self.sprite) def GetFrameIndex(self): return self.index