[Tutor] Help

eryksun eryksun at gmail.com
Fri Sep 21 21:24:00 CEST 2012

On Fri, Sep 21, 2012 at 2:46 PM, Brett Dailey
<bardockarngrim at hotmail.com> wrote:
> It just needs to be scaled to fit into each tile. You can just use one of your
> choice as a place holder for now and I'll just put my own in later.

I wrote up a version last night. Here's an image of the output:


It scales the image (pygame.transform.smoothscale) by the largest of
the two scale factors. Thus one dimension fits and the other is
potentially oversized. This preserves the aspect ratio.  It then crops
an area the size of the cell out of the center by blit'ing to a new
surface with the pygame.SRCALPHA flag set.

    mark_image = pygame.Surface(cell_size, flags=pygame.SRCALPHA)
    mark_image.blit(image, (0,0), areatl + areabr)

This preserves transparency in the source image when you blit to a cell.

I chose the randomly marked cells like this:

    num_marks = int(round(0.1 * num_cells))
    marked_cells = set(random.sample(xrange(num_cells), num_marks))

To draw the grid, iterate over the rows and columns with a nested for
loop. Pick the background color out of a list of 2 colors. The index
is simply (row + col) % 2. As col increments in the inner loop, the
value toggles between 0 and 1. Then when it gets to the next row (i.e.
row += 1), the pattern shifts by 1. If the current cell number is in
marked_cells, blit the image also.

My implementation uses a Board class with the following methods:
__init__, update, update_image, set_marks, and draw. It also has two
class attributes: bgcolor and cell_colors.

The image I chose is a standard Gnome emoticon. Here's the icon's path
in my Debian installation:


More information about the Tutor mailing list