def walk(self, direction): ''' direction (string) - direction to face (ie 'north') ''' # filename, (x, y, w, h), count, colorkey, loop, frames self.strip = SpriteStripAnim(self.filename, \ self.directions[direction], \ 3, (255,255,255), True, 10) self.strip.iter() return self
class Entity(Sprite): ''' Base class for all characters filename (string) - filename of spritesheet column (int) - get rid of this clip_width (int) - width of individual frame clip_height (int) - height of individual frame screen (pygame.Surface) - current screen (used for blitting) ''' def __init__(self, filename, clip_width, clip_height, screen): Sprite.__init__(self) self.screen = screen self.directions = {} self.clip_width = clip_width self.clip_height = clip_height for direction, clip in zip(compass, range(4)): # tuples for sprites are: # (start_x, start_y, clip_width, clip_height) self.directions[direction] = (0, clip_height*clip, clip_width, clip_height) self.filename = filename self.coords = {'x': 0, 'y': 0} self.speed = {'x': 0, 'y': 0} self.hp = 100 self.image = pygame.Surface([clip_width*foot_w_const, clip_height*foot_h_const]) self.image.set_alpha(0) self.rect = self.image.get_rect() self.update_bounds() self.controls_active = True def update_bounds(self): ''' self.rect.{x,y} are the coordinates of surface bounding the character. This surface is used for collhand and orientation. ''' self.rect.x = self.coords['x'] + self.clip_width * foot_x_const self.rect.y = self.coords['y'] + self.clip_height * foot_y_const return self def walk(self, direction): ''' direction (string) - direction to face (ie 'north') ''' # filename, (x, y, w, h), count, colorkey, loop, frames self.strip = SpriteStripAnim(self.filename, \ self.directions[direction], \ 3, (255,255,255), True, 10) self.strip.iter() return self def transform(self, x, y): ''' put the character on (x,y) point on the x,y-plane ''' self.coords['x'] = x self.coords['y'] = y return self def transport(self): ''' move the character along the respective axis with respect to the current speed. ''' self.coords['x'] += self.speed['x'] self.coords['y'] += self.speed['y'] return self def stop(self, *args): ''' stop the character ''' self.coords['x'] -= self.speed['x'] self.coords['y'] -= self.speed['y'] self.speed['x'] = 0 self.speed['y'] = 0 return self def refresh(self): ''' self.transport() - update character's current position self.current_image... - update animation self.update_bounds() - self-explanatory self.screen.blit... - put character on screen ''' self.transport() self.current_image = self.strip.next() self.update_bounds() self.screen.blit(self.current_image, (self.coords['x'], self.coords['y'])) return self def ready_for_battle(self): ''' custom callback for battles examples of use: - change position - change direction - turn off certain controls ''' raise NotImplementedError
I had to make the following addition to method spritesheet.image_at in order to provide the means to handle sprite strip cells with borders: elif type(colorkey) not in (pygame.Color,tuple,list): colorkey = image.get_at((colorkey,colorkey)) """ import sys import pygame from pygame.locals import Color, KEYUP, K_ESCAPE, K_RETURN from spritesheet import spritesheet, SpriteStripAnim surface = pygame.display.set_mode((100, 100)) FPS = 120 frames = FPS / 12 strips = [ SpriteStripAnim('Explode1.bmp', (0, 0, 24, 24), 8, 1, True, frames), SpriteStripAnim('Explode2.bmp', (0, 0, 12, 12), 7, 1, True, frames), SpriteStripAnim('Explode3.bmp', (0, 0, 48, 48), 4, 1, True, frames) + SpriteStripAnim('Explode3.bmp', (0, 48, 48, 48), 4, 1, True, frames), SpriteStripAnim('Explode4.bmp', (0, 0, 24, 24), 6, 1, True, frames), SpriteStripAnim('Explode5.bmp', (0, 0, 48, 48), 4, 1, True, frames) + SpriteStripAnim('Explode5.bmp', (0, 48, 48, 48), 4, 1, True, frames), ] black = Color('black') clock = pygame.time.Clock() n = 0 strips[n].iter() image = strips[n].next() while True: for e in pygame.event.get(): if e.type == KEYUP: