Pygame Integration

Pictures speak a thousand words. Horton comes with an optional module for rendering grids using Pygame. The goal is to make it super-easy to start getting something on the screen and scale up as your project gets a little more sophisticated.

The main function you should be aware of is pygame.render.pg.render_grid().

render_grid(surface, grid, x, y, width, height[, padding=0, render_cell=draw_cell])

Render a horton.grid.Grid instance to the given surface.

Parameters:
  • surface – A pygame.Surface object
  • grid – A horton.grid.Grid instance
  • x – The surface x-coordinate to place the grid at
  • y – The surface y-coordinate to place the grid at
  • width – The desired width of the rendered grid
  • height – The desired height of the rendered grid
  • padding – The optional padding to apply to the cells of the grid
  • render_cell – The function to call when rendering an individual cell.

All you need is this function and a horton.grid.Grid instance to draw a grid to the screen. The default horton.render.pg.draw_cell() will simply draw a filled-in black box if the cell evaluates to True. The minimal amount of code you need to get a grid on the screen is:

import pygame
import sys

from horton.grid import Grid
from horton.render.pg import render_grid
from pygame.locals import *

g = Grid.from_array([1, 0, 1,
                     1, 1, 1,
                     1, 0, 1])
pygame.init()

screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
render_grid(screen, g, 10, 10, 100, 300)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

    pygame.display.flip()

The padding parameter behaves much like padding in the CSS box-model. The position of the cell is relative to its position in the grid and the padding is applied to the content. In other words, it will reduce the size of the content of your cell.

If you want to customize your grid beyond the defaults provided you will have to supply your own draw_cell() function with the following signature:

draw_cell(surface, cell, x, y, width, height)

Render a cell from a horton.grid.Grid instance to the given surface.

Parameters:
  • surface – A pygame.Surface instance
  • cell – The value of the cell to draw
  • x – The screen x-coordinate of the cell to draw
  • y – The screen y-coordinate of the cell to draw
  • width – The calculated width of the cell
  • height – The calculated height of the cell

You then pass your function to the render_cell parameter of the horton.render.pg.render_grid() function and let it do the rest:

import pygame
import random
import sys

from horton.grid import Grid
from horton.render.pg import render_grid
from pygame.locals import *

def random_colour_cell(surf, cell, x, y, w, h):
    if cell:
        colour = (random.randint(0, 255),
                  random.randint(0, 255),
                  random.randint(0, 255))
    else:
        colour = (255, 255, 255)

    pygame.draw.rect(surf, colour, pygame.Rect(x, y, w, h))

g = Grid.from_array(3, 3,
                    [1, 0, 1,
                    1, 1, 1,
                    1, 0, 1])
pygame.init()

screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

    render_grid(screen, g, 10, 10, 100, 300,
                padding=2,
                render_cell=random_colour_cell)

    pygame.display.flip()

These two functions alone can get you pretty far. Just check out the examples/ folder in your horton distribution to see what is possible.