Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame how to change size of a surface?

How do you change the size of a surface in pygame that has an image (not scaling). When I load an image in pygame the surface becomes the size of the image. I need to change the size of the surface to be the size of a frame (sprite sheet).

Here is code I used to solve issue (thanks to Chris Dennett):

self.surface = pygame.Surface((20, 20))
self.surface.blit(pygame.image.load(imageName).convert_alpha(), (0,0), (0, 0, frameWidth, frameHeight))
like image 723
nobody Avatar asked Oct 18 '25 23:10

nobody


1 Answers

For sprite sheets, you have 2 good options.

1) Load spritesheet as one Surface.

When blitting, use the source rect argument. This will blit just one frame, from the sheet.

2) Use subsurfaces.

Load spritesheet as one Surface. Load a subsurface, of each frame you want : http://www.pygame.org/docs/ref/surface.html#Surface.subsurface

As far as you are concerned, the subsurface is a 'new' surface, with the width and height of the cell. But, it's not duplicate memory. It's linked.

From the docs:

Returns a new Surface that shares its pixels with its new parent. The new Surface is considered a child of the original. Modifications to either Surface pixels will effect each other. Surface information like clipping area and color keys are unique to each Surface.

like image 112
ninMonkey Avatar answered Oct 22 '25 05:10

ninMonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!