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

Re: [pygame] sprite groups and tilesheets



its returning a list so you cant have self.image = load_sprite('player.png').

you need something like: 

self.images = load_sprite('player.png')
self.image = self.images[0] #for first frame

or

self.frame = 0
self.image = self.images[self.frame] 
#if you want to update it later with the different animation frames.

Here's a basic sprite class with a looping animation:

class Bob(pygame.sprite.Sprite):
  def __init__(self):
    self.frame = 0
    self.images = load_sprite('bob.png')
    self.image = self.images[self.frame]
    self.rect = self.image.get_rect()
    self.animcounter = 0
    self.animspeed = 7
  def update(self):
    self.animcounter = (self.animcounter + 1)%self.animspeed
    if self.animcounter == 0:
      self.frame = (self.frame + 1)%len(self.images)
    self.image = self.images[self.frame]



bob = Bob()
where bob.update() #would be called in the main loop


Devon

--- On Mon, 11/10/08, Michael Fiano <michael.fiano@xxxxxxxxx> wrote:

> From: Michael Fiano <michael.fiano@xxxxxxxxx>
> Subject: [pygame] sprite groups and tilesheets
> To: pygame-users@xxxxxxxx
> Date: Monday, November 10, 2008, 10:52 PM
> I am having problems adding my player sprite to a sprite
> group since
> self.image needs a surface, though it is a list of
> animation frames for the player in my code and I can't
> figure out what I need to do. Can anybody give me some
> pointers? Thanks.
> 
> self.image = load_sprite('player.png')
> 
> class load_sprite:
> 	def __init__(self, filename):
> 		self.sheet =
> pygame.image.load(os.path.join('data',
> 'graphics', filename))
> 	def imgat(self, rect, colorkey = None):
> 		rect = Rect(rect)
> 		image = pygame.Surface(rect.size).convert()
> 		image.blit(self.sheet, (0, 0), rect)
> 		if colorkey is not None:
> 			if colorkey is -1:
> 				colorkey = image.get_at((0, 0))
> 			image.set_colorkey(colorkey, RLEACCEL)
> 		return image
> 	def imgsat(self, rects, colorkey = None):
> 		imgs = []
> 		for rect in rects:
> 			imgs.append(self.imgat(rect, colorkey))
> 		return imgs