Created
December 15, 2014 07:00
-
-
Save marjinal1st/e40ef1bd40911932505d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- coding: utf-8 -*- | |
import pygame | |
from pygame.locals import * | |
import sys | |
from sprite_anim import * | |
pygame.init() | |
display = 800, 600 | |
FPS = 60 | |
frames = FPS / 10 | |
main_surface = pygame.display.set_mode(display, DOUBLEBUF) | |
strips = [ | |
SpriteStripAnim("plane_sheet.png", (0, 0, 88, 73), 3, 1, True, frames), | |
SpriteStripAnim("plane_sheet.png", (88, 0, 88, 73), 3, 1, True, frames), | |
SpriteStripAnim("plane_sheet.png", (176, 0, 88, 73), 3, 1, True, frames) | |
] | |
black = Color('black') | |
clock = pygame.time.Clock() | |
n = 0 | |
strips[n].iter() | |
image = strips[n].next() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == KEYUP: | |
if event.key == K_ESCAPE: | |
sys.exit() | |
elif event.key == K_RETURN: | |
n += 1 | |
if n >= len(strips): | |
n = 0 | |
strips[n].iter() | |
main_surface.fill((0, 0, 0)) | |
main_surface.blit(image, (0, 0)) | |
pygame.display.flip() | |
image = strips[n].next() | |
clock.tick(FPS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Spritesheet(object): | |
def __init__(self, filename): | |
try: | |
self.sheet = pygame.image.load(filename).convert_alpha() | |
except pygame.error, message: | |
print 'Unable to load spritesheet image:', filename | |
raise SystemExit | |
def image_at(self, rectangle, colorkey=None): | |
"""Loads image from x,y,x+offset,y+offset""" | |
rect = pygame.Rect(rectangle) | |
image = pygame.Surface(rect.size).convert() | |
image.blit(self.sheet, (0, 0), rect) | |
if colorkey is not None: | |
if colorkey is -1: | |
colorkey = image.get_at((0, 0)) | |
image.set_colorkey(colorkey, pygame.RLEACCEL) | |
return image | |
def images_at(self, rects, colorkey=None): | |
"""Loads multiple images, supply a list of coordinates""" | |
return [self.image_at(rect, colorkey) for rect in rects] | |
def load_strip(self, rect, image_count, colorkey=None): | |
"""Loads a strip of images and returns them as a list""" | |
tups = [(rect[0] + rect[2] * x, rect[1], rect[2], rect[3]) | |
for x in range(image_count)] | |
return self.images_at(tups, colorkey) | |
class SpriteStripAnim(object): | |
"""sprite strip animator | |
This class provides an iterator (iter() and next() methods), and a | |
__add__() method for joining strips which comes in handy when a | |
strip wraps to the next row. | |
""" | |
def __init__(self, filename, rect, count, colorkey=None, loop=False, frames=1): | |
"""construct a SpriteStripAnim | |
filename, rect, count, and colorkey are the same arguments used | |
by spritesheet.load_strip. | |
loop is a boolean that, when True, causes the next() method to | |
loop. If False, the terminal case raises StopIteration. | |
frames is the number of ticks to return the same image before | |
the iterator advances to the next image. | |
""" | |
self.filename = filename | |
ss = Spritesheet(filename) | |
self.images = ss.load_strip(rect, count, colorkey) | |
self.i = 0 | |
self.loop = loop | |
self.frames = frames | |
self.f = frames | |
def iter(self): | |
self.i = 0 | |
self.f = self.frames | |
return self | |
def next(self): | |
if self.i >= len(self.images): | |
if not self.loop: | |
raise StopIteration | |
else: | |
self.i = 0 | |
image = self.images[self.i] | |
self.f -= 1 | |
if self.f == 0: | |
self.i += 1 | |
self.f = self.frames | |
return image | |
def __add__(self, ss): | |
self.images.extend(ss.images) | |
return self | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment