Mercurial > qq-pygrafix
changeset 23:71ba52633428
make tile caches global explicitly
| author | sheep@ghostwheel |
|---|---|
| date | Sun, 25 Oct 2009 14:00:08 +0100 |
| parents | 78492768ce73 |
| children | 68c14dbcf1c0 |
| files | qq.py shadow.png |
| diffstat | 2 files changed, 18 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/qq.py Sun Oct 25 13:21:29 2009 +0100 +++ b/qq.py Sun Oct 25 14:00:08 2009 +0100 @@ -28,15 +28,16 @@ DX = [0, 1, 0, -1] DY = [-1, 0, 1, 0] +# Dimensions of the map tiles +MAP_TILE_WIDTH, MAP_TILE_HEIGHT = 24, 16 class TileCache: """Load the tilesets lazily into global cache""" - cache = {} - def __init__(self, width=32, height=None): self.width = width self.height = height or width + self.cache = {} def __getitem__(self, filename): """Return a table of tiles, load it from disk if needed.""" @@ -78,14 +79,14 @@ """Sprite for shadows.""" def __init__(self, owner): - self.image = TileCache(24, 24)["shadow.png"][0][0] + pygame.sprite.Sprite.__init__(self) + self.image = SPRITE_CACHE["shadow.png"][0][0] self.image.set_alpha(64) - pygame.sprite.Sprite.__init__(self) self.rect = self.image.get_rect() self.owner = owner - def update(self): - """Move the shadow to the right place.""" + def update(self, *args): + """Make the shadow follow its owner.""" self.rect.midbottom = self.owner.rect.midbottom @@ -127,12 +128,13 @@ """The default animation.""" while True: + # Change to next frame every two ticks for frame in self.frames[0]: self.image = frame yield None yield None - def update(self): + def update(self, *args): """Run the current animation.""" self.animation.next() @@ -144,7 +146,7 @@ is_player = True def __init__(self, pos=(1, 1)): - self.frames = TileCache()["player.png"] + self.frames = SPRITE_CACHE["player.png"] Sprite.__init__(self, pos) self.direction = 2 self.animation = None @@ -153,6 +155,7 @@ def walk_animation(self): """Animation for the player walking.""" + # This animation is hardcoded for 4 frames and 16x24 map tiles for frame in range(4): self.image = self.frames[self.direction][frame] yield None @@ -160,7 +163,7 @@ yield None self.move(3*DX[self.direction], 2*DY[self.direction]) - def update(self): + def update(self, *args): """Run the current animation or just stand there if no animation set.""" if self.animation is None: @@ -205,9 +208,8 @@ """Draw the level on the surface.""" wall = self.is_wall - tile_width, tile_height = 24, 16 - tiles = TileCache(tile_width, tile_height)[self.tileset] - image = pygame.Surface((self.width*tile_width, self.height*tile_height)) + tiles = MAP_CACHE[self.tileset] + image = pygame.Surface((self.width*MAP_TILE_WIDTH, self.height*MAP_TILE_HEIGHT)) overlays = {} for map_y, line in enumerate(self.map): for map_x, c in enumerate(line): @@ -250,7 +252,8 @@ # Default to ground tile tile = 0, 3 tile_image = tiles[tile[0]][tile[1]] - image.blit(tile_image, (map_x*tile_width, map_y*tile_height)) + image.blit(tile_image, + (map_x*MAP_TILE_WIDTH, map_y*MAP_TILE_HEIGHT)) return image, overlays def get_tile(self, x, y): @@ -283,11 +286,6 @@ return True return self.get_bool(x, y, 'block') - def draw(self, surface): - """Draw the map on the provided surface.""" - - surface.blit(self.image, (0, 0)) - class Game: """The main game object.""" @@ -391,6 +389,8 @@ if __name__ == "__main__": + SPRITE_CACHE = TileCache() + MAP_CACHE = TileCache(MAP_TILE_WIDTH, MAP_TILE_HEIGHT) pygame.init() pygame.display.set_mode((424, 320)) Game().main()
