from random import randint

from pygame import Rect, draw

from .entities import Falling

SMALL = (32, 32)
MEDIUM = (128, 128)
LARGE = (512, 512)
TILE_SIZE = 16

class SquareException(Exception):
    pass

class Tile(Rect):
    """City tile, containing various data
    """
    colour = (0, 0, 0)

    def __init__(self, x, y, size):
        super(Tile, self).__init__(x*size, y*size, size, size)

    def __setattr__(self, attr, value):
        """Make sure we're square :)"""
        old_value = self.__getattribute__(attr)
        super(Tile, self).__setattr__(attr, value)
        if self.size[0] != self.size[1]:
            super(Tile, self).__setattr__(attr, old_value)
            raise SquareException("I need to be a square, reverting...")


class Grid(object):
    """Contains tiles"""
    def __init__(self, grid_size, tile_size=TILE_SIZE):
        self.sizes = (grid_size[0], tile_size)
        self._grid = []
        self._falling = None

        for y in xrange(grid_size[0]):
            row = []
            self._grid.append(row)
            for x in xrange(grid_size[1]):
                tile = Tile(x, y, tile_size)
                row.append(tile)

        self._falling = Falling()
        self._falling.rel_move((self.sizes[0]/2, 1))
        self.colourise(self._falling.coordinates, (255, 255, 255))

    def colourise(self, coords, colour):
        for x, y in coords:
            if y < 0:
                continue
            elif x < 0:
                raise IndexError("list index out of range")
            tile = self.get_tile(x, y)
            tile.colour = colour

    def get_tile(self, x, y):
        return self._grid[y][x]

    def move_down(self):
        self.colourise(self._falling.coordinates, (0, 0, 0))
        self._falling.rel_move((0, 1))
        self.colourise(self._falling.coordinates, (255, 255, 255))

    def move_left(self):
        self.colourise(self._falling.coordinates, (0, 0, 0))
        self._falling.rel_move((-1, 0))
        self.colourise(self._falling.coordinates, (255, 255, 255))

    def move_right(self):
        self.colourise(self._falling.coordinates, (0, 0, 0))
        self._falling.rel_move((1, 0))
        self.colourise(self._falling.coordinates, (255, 255, 255))

    def rotate_block(self):
        self.colourise(self._falling.coordinates, (0, 0, 0))
        self._falling.rotate()
        self.colourise(self._falling.coordinates, (255, 255, 255))

    def draw_grid(self, screen):
        for row in self._grid:
            for tile in row:
                draw.rect(screen, tile.colour, tile)

@@ -228,47 +228,47 @@
  %22%22%22
-City tile, containing various data%0A    
+A simple tile on the screen, has colour
 %22%22%22%0A
@@ -776,22 +776,198 @@
  %22%22%22
-Contains tiles
+The actual game grid%0A%0A    Although we store tiles by row (and thus must fetch them by %60y%60 and then%0A    %60x%60, all public interfaces accept coordinates in the traditional (x, y)%0A    format%0A    
 %22%22%22%0A
@@ -1536,16 +1536,201 @@
 olour):%0A
+        %22%22%22Colour in a list of coordinates%0A%0A        Raises an IndexError if the tiles will be outside the grid (except the%0A        top side, which is just silently ignored)%0A        %22%22%22%0A
         
@@ -1983,16 +1983,54 @@
  x, y):%0A
+        %22%22%22Grab the tile at (x, y)%22%22%22%0A
         
@@ -2071,32 +2071,74 @@
 ove_down(self):%0A
+        %22%22%22Move the current block down%22%22%22%0A
         self.col
@@ -2306,32 +2306,74 @@
 ove_left(self):%0A
+        %22%22%22Move the current block left%22%22%22%0A
         self.col
@@ -2543,32 +2543,75 @@
 ve_right(self):%0A
+        %22%22%22Move the current block right%22%22%22%0A
         self.col
@@ -2782,32 +2782,73 @@
 te_block(self):%0A
+        %22%22%22Rotate the block clockwise%22%22%22%0A
         self.col
@@ -2983,32 +2983,32 @@
 55, 255, 255))%0A%0A
-
     def draw_gri
@@ -3020,24 +3020,145 @@
 f, screen):%0A
+        %22%22%22Draw the grid on the given display surface%0A%0A        Should be called any time the grid is updated%0A        %22%22%22%0A
         for 

Document classes and methods a bit better