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

Re: [pygame] How to manage image source Rect()'s for units



On Nov 13, 2007 5:12 PM, Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
> Jake b wrote:
>
> > I tried using a pygame.sprite.RenderPlain(), but I couldn't make it
> > use a source rect.
>
> You could create subsurfaces from the main surface
> for all the frames.
>
> --
> Greg
>

Is creating a subsurface a slow operation? Does it increase the memory
much? Or no, it's something like a pointer to a surface, with a RectO
?

I found where to download the source to pygame.sprite.RenderPlain() ,
so I was able to create a class to do what I wanted.

How do I do a test in my .add() method to check if the sprite added
has a member named '.rect_source' ? ( Because if it does not, I'll
default it to its .image.get_rect() )

Here's the new class. It works as long as .rect_source exists in the sprites.

# file: RenderSrcRect.py
# Author: jake bolton [created: 2007/11/14]
# About: RenderSrcRect
    # child of RenderPlain, adds src rect support

VERSION = "0.1"

# todo:
    # on .add() check if .rect_source exists, else use surface.get_rect()

import pygame

class RenderSrcRect(pygame.sprite.RenderPlain):
    """custom sprite group rendering class. Uses src rects of sprite.

    the inheritance is: RenderPlain() == Group() which derives
AbstractGroup()

    members used from the sprites to blit:
        .image = the sprite's surface
        .rect = the blit dest rect
        .rect_source = the blit source rect
    """

    def __init__(self):
        """initialize Unit()"""
        pygame.sprite.RenderPlain.__init__(self)

    def draw(self, surface):
        """draw(surface). Draw all sprites onto the surface"""
        sprites = self.sprites()
        surface_blit = surface.blit
        for spr in sprites:
            # blit with src rect
            self.spritedict[spr] = surface_blit(spr.image, spr.rect,
spr.rect_source)
        self.lostsprites = []

-- 
Jake