[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Cycling images



Chuck Paussa wrote:
Could someone point me to an example for cutting up an image into pieces?
something like

image = partOfOtherImage(32,48,16,16)

That way I can have one bmp with all my sprites in it and just cut out
the parts I want to use for the current purpose. It also would make
sprite animation a lot easier.
This is pretty easy to do, there are a couple techniques you may want. The main trick is that the Surface.blit() takes an optional third argument that is the source area of the source image to use. You can also do this by creating subsurfaces, but that may be more trouble than worth. Here is some code Solarwolf uses to chop a strip of images into frames.


def animstrip(img, width=0):
if not width:
width = img.get_height()
size = width, img.get_height()
images = []
for x in range(0, img.get_width(), width):
i = pygame.Surface(size)
i.blit(img, (0, 0), ((x, 0), size))
images.append(i)
return images

I've simplified this code a little. If you are dealing with colorkeys or surface alphas you will need some extra work.