Can you repeatedly print an image for the entire length of a rect, in pygame?

Viewed 79

Basically what the title says. I'm trying to create a 2d platformer so I can get more into game developement, and I need platforms that have an image. So Say you draw a rect (an actual rectangle for instance, 30 high, 120 along), could you fill that rectangle with a tile who's original size is 30 by 30? E.g a 30x120 rect, filled with 4, 30x30 tile sprites, from start to finish of the rectangle. If so, any tips on how I would go about this? Thanks.

I've heard of creating lists for making a platform game but for some that method confuses me and, this way seems a lot more straight forward.

Again, any help would be appreciated. Much Thanks. Anthony.

2 Answers

This is a reasonably straightforward operation.

First load in the image you want to tile:

tile_image = pygame.image.load( 'my_tile.png' ).convert_alpha()

Then create a new PyGame Surface of the required size:

tiled_image = pygame.Surface( ( width, height ) )

Then using two loops, step down & across the surface, "stamping" a copy of the image. After each stamp, calculate the location of the next placement. Continue stamping across the X-horizontals, and down the Y-vertical until all pixels are covered:

x_cursor = 0
y_cursor = 0
while ( y_cursor < height ):
    while ( x_cursor < width ):
        tiled_image.blit( tile, ( x_cursor, y_cursor ) )
        x_cursor += tile.get_width()
    y_cursor += tile.get_height()
    x_cursor = 0

Now wrap all that into a handy-to-use function:

def makeTiledImage( image, width, height ):
    x_cursor = 0
    y_cursor = 0

    tiled_image = pygame.Surface( ( width, height ) )
    while ( y_cursor < height ):
        while ( x_cursor < width ):
            tiled_image.blit( image, ( x_cursor, y_cursor ) )
            x_cursor += image.get_width()
        y_cursor += image.get_height()
        x_cursor = 0
    return tiled_image

Which can be called:

tile_image = pygame.image.load( 'my_tile.png' ).convert_alpha()
platform = makeTiledImage( tile_image, 250, 30 )

screenshot

Reference Code:

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400
WINDOW_FPS      = 60
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE

DARK_BLUE = (   3,   5,  54)



def makeTiledImage( image, width, height ):
    """ Given an image, make another image tiled with the given image """
    x_cursor = 0
    y_cursor = 0

    tiled_image = pygame.Surface( ( width, height ) )
    while ( y_cursor < height ):
        while ( x_cursor < width ):
            tiled_image.blit( image, ( x_cursor, y_cursor ) )
            x_cursor += image.get_width()
        y_cursor += image.get_height()
        x_cursor = 0
    return tiled_image



### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Tiled Rect")


tile_image = pygame.image.load( 'rot_offcentre_1.png' ).convert_alpha()
platform = makeTiledImage( tile_image, 250, 30 )


### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            pass

    # Update the window, but not more than 60fps
    window.fill( DARK_BLUE )
    window.blit( platform, ( 50, 150 ) )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop( WINDOW_FPS )


pygame.quit()
Related