0

So, I've been doing some googling, and haven't found a good solution to my problem. My problem is that I'm using PyGame, and I want to use a Sprite Sheet for my player.

This is all well and good, and it would be too, if I wasn't using a Sprite Sheet strip. Basically, if you don't understand, I have a strip of 32x32 'frames'. These frames are all in an image, along side each other. So, I have 3 frames in 1 image. I'd like to be able to use them as my sprite sheet, and not have to crop them up.

I have used an awesome, popular and easy-to-use game framework for Lua called LÖVE. LÖVE has these things called "Quads". They are similar to texture regions in LibGDX, if you know what they are. Basically, quads allow you to get parts of an image. You define how large a quad is, and you define parts of an image that way, or 'regions' of an image.

I would like to do something similar to this in PyGame, and use a "for" loop to go through the entire image width and height and mark each 32x32 area (or whatever the user defines as their desired frame width and height) and store that in a list or something for use later on. I'd define an animation speed and stuff, but that's for later on.

I've been looking around on the web, and I can't find anything that will do this. I found 1 script on the PyGame website, but it crashed PyGame when I tried to run it. I tried for hours trying to fix it, but no luck. So, is there a way to do this? Is there a way to get regions of an image? Am I going about this the wrong way? Is there a simpler way to do this?

Thanks! :-)

asked Oct 4, 2013 at 23:05
1
  • Is this post what you're looking for? Commented Oct 5, 2013 at 5:16

2 Answers 2

2

Since you are obviously programming your game in Python, you can use the Pillow module (doc/tutorial here) to use sprite strips (or grids, as is common practice) exactly as you describe.

Specifically, check the section about cutting, pasting, and merging images.

You can open your sprite strip image file
strip = Image.open("sprites")
then crop it to retrieve the Nth sprite (assuming 32x32 size, starting at 0) with
sprite = strip.crop( (N*32, 0, 32, 32) )

Of course the sprite strip image remains intact -- crop() only extracts a copy of the specified region. (It is the paste() function allows modifying the original, by pasting into a region precisely.)

If you want an array of sprites (a list, strictly speaking) then you can do something like
[strip.crop( (N*32, 0, 32, 32) ) for n in range(nb_of_sprites)]

Pretty easy, isn't it?

answered Apr 3, 2014 at 19:42
0

without pillow example here:

N = 8; width = 60; height = 114; x_offset = 5
self.images = []
walk_all = pygame.image.load('walk_s.jpg')
for n in range(N):
 surf = pygame.Surface((width, height))
 surf.blit(walk_all, (0, 0), (x_offset+n*width, 0, width, height))
 self.images.append(surf)
answered Oct 14, 2019 at 7:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.