Mercurial > nagslang
view nagslang/level.py @ 54:2c1b85b6f457
Add .get_file() to resources.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 18:46:05 +0200 |
parents | 39d346467052 |
children | 5db052531510 |
line wrap: on
line source
import pygame import pygame.locals as pgl from nagslang.resources import resources POLY_COLORS = { 1: pygame.color.THECOLORS['red'], 2: pygame.color.THECOLORS['green'], 3: pygame.color.THECOLORS['yellow'], 4: pygame.color.THECOLORS['blue'], 5: pygame.color.THECOLORS['lightblue'], 6: pygame.color.THECOLORS['magenta'], } class Level(object): def __init__(self, name): self.name = name # defaults self.x = 800 self.y = 600 self.polygons = {} self.basetile = 'tiles/floor.png' self._tile_image = None self._surface = None def load(self): def add_polygon(polygon, index, num_points): self.polygons[index] = polygon if len(polygon) != num_points: print 'Error - incorrect polygon size' print 'Expected: %d, got %d' % (num_points, len(polygon)) inpoly = False polygon = [] index = 0 num_points = 0 with resources.get_file(self.name) as f: for line in f: if inpoly: if not line.startswith('Point:'): add_polygon(polygon, index, num_points) polygon = [] inpoly = False index = 0 else: point = line.split(':', 1)[1] x, y = [int(i) for i in point.split()] polygon.append((x, y)) if line.startswith('X-Size:'): self.x = int(line.split(':', 1)[1]) elif line.startswith('Y-Size:'): self.y = int(line.split(':', 1)[1]) elif line.startswith('Base tile:'): self.basetile = line.split(':', 1)[1].strip() elif line.startswith('Polygon'): rest = line.split(' ', 1)[1] index, num_points = [int(x) for x in rest.split(':', 1)] inpoly = True if index: add_polygon(polygon, index, num_points) def get_size(self): return self.x, self.y def set_base_tile(self, new_tile): self.basetile = new_tile self._tile_image = None def point_to_pygame(self, pos): # Convert a point from pymunk (which is what we store) # to pygame for drawing return (pos[0], self.y - pos[1]) def get_walls(self): return self.polygons.values() def _draw_walls(self): for index, polygon in self.polygons.items(): color = POLY_COLORS[index] if len(polygon) > 1: pointlist = [self.point_to_pygame(p) for p in polygon] pygame.draw.lines(self._surface, color, False, pointlist, 2) def get_background(self): self._draw_background() # Draw polygons self._draw_walls() return self._surface def _draw_background(self, force=False): if self._tile_image is None: self._tile_image = resources.get_image(self.basetile) if self._surface is not None and not force: # We assume we don't change return self._surface self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA) self._surface.fill(pygame.color.THECOLORS['black']) x_step = self._tile_image.get_rect().width y_step = self._tile_image.get_rect().height x_count = self.x // x_step + 1 y_count = self.y / y_step + 1 for x in range(x_count): for y in range(y_count): tile_rect = pygame.rect.Rect(x * x_step, y * y_step, x_step, y_step) self._surface.blit(self._tile_image, tile_rect) return self._surface