Changeset 348:f0e8970ab804


Ignore:
Timestamp:
Sep 6, 2013, 1:18:40 PM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
cdef045667f8713abad3989d5b9c8ffd982d9f69
Message:

Split out tiling into utility function

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/level.py

    r346 r348  
    55from nagslang import enemies
    66from nagslang import puzzle
     7from nagslang.utils import tile_surface
    78from nagslang.resources import resources
    89from nagslang.yamlish import load, dump
     
    220221            # We assume we don't change
    221222            return self._surface
    222         self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
    223         self._surface.fill(pygame.color.THECOLORS['black'])
    224         x_step = self._tile_image.get_rect().width
    225         y_step = self._tile_image.get_rect().height
    226         x_count = self.x // x_step + 1
    227         y_count = self.y / y_step + 1
    228         for x in range(x_count):
    229             for y in range(y_count):
    230                 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
    231                                              x_step, y_step)
    232                 self._surface.blit(self._tile_image, tile_rect)
     223        self._surface = tile_surface((self.x, self.y), self._tile_image)
    233224        return self._surface
  • nagslang/utils.py

    r343 r348  
    11import pygame
     2import pygame.locals as pgl
     3
    24from pymunk.vec2d import Vec2d
    35
     
    2527        vec.length = length
    2628    return vec
     29
     30
     31def tile_surface(size, tile_image):
     32    # create a surface, approriately tiled
     33    surface = pygame.surface.Surface(size, pgl.SRCALPHA)
     34    surface.fill(pygame.color.THECOLORS['black'])
     35    x_step = tile_image.get_rect().width
     36    y_step = tile_image.get_rect().height
     37    x_count = size[0] // x_step + 1
     38    y_count = size[1] / y_step + 1
     39    tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
     40    for x in range(x_count):
     41        tile_rect.x = x * x_step
     42        for y in range(y_count):
     43            tile_rect.y = y * y_step
     44            surface.blit(tile_image, tile_rect)
     45    return surface
Note: See TracChangeset for help on using the changeset viewer.