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

Re: [pygame] help with subsurfaces



On Dec 12, 2007 12:37 PM, Greg <gshives@xxxxxxx> wrote:
> - new to pygame/python
> - have a .png that contains all player avatars
> - trying to load only a one avatar from that image based on a known size
> and position
>
> Question: What is best way to get a  portion of an larger image and
> display it on the screen. Do I use subsurfaces, or blit, or what is best
> way?  Thanks,
>
> Greg
>

Using a subsurface is the best way, as it shares pixel data so doesn't
use as much memory as creating another surface would.  If you plan on
modifying the pixels, now or at a later time, and don't want those
modifications to be stored on the avatar sheet surface, you can always
make a copy of the subsurface.

base = pygame.image.load("base.png")
avatar1 = base.subsurface(known_rect)

Then display avatar1 as you usually would display a surface, with
screen.blit(avatar1).

Also, if you use a colorkey, you can set the colorkey on the base and
not worry about it for the subsurfaces.