## pygame - Python Game Library ## Copyright (C) 2000-2003 Pete Shinners ## (C) 2004 Joe Wreschnig ## Windowed Sprite Group Extension ## Copyright (C) 2008 Bryce Schroeder ## ## This library is free software; you can redistribute it and/or ## modify it under the terms of the GNU Library General Public ## License as published by the Free Software Foundation; either ## version 2 of the License, or (at your option) any later version. ## ## This library is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## Library General Public License for more details. ## ## You should have received a copy of the GNU Library General Public ## License along with this library; if not, write to the Free ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## ## Pete Shinners ## pete@shinners.org ## ## Bryce Schroeder ## bryce.schroeder@gmail.com # flashing bug is probably here import pygame from pygame.locals import * class WindowedGroup(pygame.sprite.Group): def __init__(self, *args): pygame.sprite.Group.__init__(*((self,)+args)) self.current = 0,0 def clear_window(self, screen, window, cords, background): screen.blit(background, window, cords+(window.w,window.h)) def clear_background(self, surface, window, rects, cords, background): for s in rects: source = pygame.rect.Rect(s.x+cords[0]-window.x, s.y+cords[1]-window.y, s.w, s.h) surface.blit(background, s, source) def draw(self, surface, window, background, cords=(0,0)): if cords != self.current: self.clear_window(surface, window, cords, background) reset = True self.current = cords else: reset = False spritedict = self.spritedict surface_blit = surface.blit dirty = self.lostsprites self.lostsprites = [] dirty_append = dirty.append for s in self.sprites(): r = spritedict[s] shiftedrect = pygame.rect.Rect( s.rect.x-cords[0]+window.x, s.rect.y-cords[1]+window.y, s.rect.w, s.rect.h ) if ( shiftedrect.x - window.x > window.width or shiftedrect.x - window.x < -shiftedrect.w or shiftedrect.y - window.y > window.height or shiftedrect.y - window.y < -shiftedrect.h): continue newrect = surface_blit(s.image, shiftedrect) if r is 0: dirty_append(newrect) else: if newrect.colliderect(r): dirty_append(newrect.union(r)) else: dirty_append(newrect) dirty_append(r) spritedict[s] = newrect if reset: return [window] else: return dirty