Search code examples
python2dtile

2D-Array to tiled map PyGame, Python


I have looked for similar questions, but I haven't found anything concrete enough to actually apply to my situation.

Ok, so I've come up with something that will display the right amount of images, in the right places, except for the Y axis. Basically, if I have array:

[1,1,1,1,
 0,0,0,0]

I would want it to display as something like:

####
@@@@

However, it display as this:

####@@@@

How do I iterate down in the rows, much more importantly, how do I detect a row-continuation?


Solution

  • Posting on behalf of the OP who is new to the site and found the answer on their own.

    tileX = 0
    tileY = 0
    tile_dict = {0: pygame.image.load("C:\Users\Software Development\Desktop\Tile00.png"),    1: pygame.image.load("C:\Users\Software Development\Desktop\Tile01.png")}
    
    map00 = [[0,0,0,0],
        [1,1,1,1], [1,0,1,0]]
    
    for x in map00:
    for x in x:
        if x == 0:
            screen.blit(tile_dict[0], (tileX, tileY))
            tileX = tileX+16
        if x == 1:
            screen.blit(tile_dict[1], (tileX, tileY))
            tileX = tileX+16
    tileX = 0
    tileY += 16