changeset 348:f0e8970ab804

Split out tiling into utility function
author Neil Muller <drnlmuller@gmail.com>
date Fri, 06 Sep 2013 15:18:40 +0200
parents dced49dd9864
children c4285f19894c
files nagslang/level.py nagslang/utils.py
diffstat 2 files changed, 21 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/nagslang/level.py	Fri Sep 06 15:40:35 2013 +0200
+++ b/nagslang/level.py	Fri Sep 06 15:18:40 2013 +0200
@@ -4,6 +4,7 @@
 from nagslang import game_object as go
 from nagslang import enemies
 from nagslang import puzzle
+from nagslang.utils import tile_surface
 from nagslang.resources import resources
 from nagslang.yamlish import load, dump
 
@@ -219,15 +220,5 @@
         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)
+        self._surface = tile_surface((self.x, self.y), self._tile_image)
         return self._surface
--- a/nagslang/utils.py	Fri Sep 06 15:40:35 2013 +0200
+++ b/nagslang/utils.py	Fri Sep 06 15:18:40 2013 +0200
@@ -1,4 +1,6 @@
 import pygame
+import pygame.locals as pgl
+
 from pymunk.vec2d import Vec2d
 
 
@@ -24,3 +26,20 @@
     if vec.length != 0:
         vec.length = length
     return vec
+
+
+def tile_surface(size, tile_image):
+    # create a surface, approriately tiled
+    surface = pygame.surface.Surface(size, pgl.SRCALPHA)
+    surface.fill(pygame.color.THECOLORS['black'])
+    x_step = tile_image.get_rect().width
+    y_step = tile_image.get_rect().height
+    x_count = size[0] // x_step + 1
+    y_count = size[1] / y_step + 1
+    tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
+    for x in range(x_count):
+        tile_rect.x = x * x_step
+        for y in range(y_count):
+            tile_rect.y = y * y_step
+            surface.blit(tile_image, tile_rect)
+    return surface