I am trying to generate a tile map from a text file with an arangement of -1's which mean no tile and 0's which mean there should be a tile there

Viewed 62

The program isn't displaying any of the tiles. The tile is defined as a surface and rect. The tile name is floor. The txt file is Map_Floor.txt it is attached. """

import pygame
from sys import exit

#init and screen definiton
pygame.init()
info = pygame.display.Info()
SIZE = WIDTH, HEIGHT = info.current_w, info.current_h
print(WIDTH, HEIGHT)
mainsurface = pygame.display.set_mode(SIZE)
screen = pygame.Surface((800, 600))

pygame.display.set_caption("Gme")
clock = pygame.time.Clock()

#rect stuff
x_val = 300
y_val = 300
floor_surf = pygame.image.load('map/floor.png')
floor_rect = floor_surf.get_rect(midleft = (x_val,y_val))
#screen.blit(floor_surf,floor_rect)

#floor_rect = floor_surf.get_rect(midleft = (0,0))
#screen.blit(floor_surf,floor_rect)


#Trying to create a tile map
lst_to_str = ""
with open('map/Map_Floor.txt') as f:#opens a txt file
    lines = f.readlines()#reads
    #lines = lines.join()
    #print(lines)
    x = 0
    for items in lines:#this turnes the original data into a string that can be parsed easily
        lst_to_str += items#this turnes the original data into a string that can be parsed easily

        str_to_lst = lst_to_str.split(",")#parses string at the "," to create individual elements
        #print(str_to_lst)
        
        for position in str_to_lst:#idderates though the new list. If the value of the item in the string is 0 then it will try to blit a surface
            x_val += 32

            #print(position)
            if position == '0':
                
                        
                #floor_rect = floor_surf.get_rect(midright = (x_val,y_val))        
                mainsurface.blit(floor_surf,(x_val,y_val))
                #print("full")
                pygame.display.update()
                

            elif position == '\n':#if it is /n then it will do down by 1 tile
                y_val += 32
                #print("New line")

            elif position == "-1":#if it is -1 it will skip the tile
                print("skipping")
                #print("empty")



    

#print("Out of function")

while True:
    for event in pygame.event.get():

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_ESCAPE:
                print("quitting")
                pygame.quit()
                exit()

    #mainsurface.blit(floor_surf,floor_rect)
        
    



    pygame.display.update()
    clock.tick(60)

"""

Below is the Map_Floor.txt

"""

,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1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-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,

"""

Lastly attached is the Floor.png Floor.png

This link is to a github that you can download all of the files. https://github.com/Weston714/pygame_final

1 Answers

The actual "bug" is that you're drawing to mainsurface, whereas the "window" is screen.

To be honest, I think you should give up on comma-delimited numbers, and just use a block of single characters, e.g.:

....................
...####......t......
...#..#.........t...
...#..d............. 
...####....www......
............wwww....
....................

I think your parsing and painting code need to be separated.

I wrote an example function that parses your number-CSV, and turns it into a tile-surface. (There's a few codes missing in your map too.) But this would be much easier again if it was just interpreting single characters.

gme screenshot

import pygame
from sys import exit

#init and screen definiton
pygame.init()
info = pygame.display.Info()
SIZE = WIDTH, HEIGHT = info.current_w, info.current_h
print(WIDTH, HEIGHT)
mainsurface = pygame.display.set_mode(SIZE)

# need a smaller window
WIDTH, HEIGHT = ( 400, 400 )
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )

pygame.display.set_caption("Gme")
clock = pygame.time.Clock()

#rect stuff
x_val = 300
y_val = 300
#floor_surf = pygame.image.load('map/floor.png')
#floor_rect = floor_surf.get_rect(midleft = (x_val,y_val))
#screen.blit(floor_surf,floor_rect)

#floor_rect = floor_surf.get_rect(midleft = (0,0))
#screen.blit(floor_surf,floor_rect)


#Trying to create a tile map
def loadTile( filename ):
    """ Load a Map-Tile into a PyGame Surface """

    # Dictionary to convert symbols to coloured tile-pixels:
    colour_map = { "-1":(255,255,255), \
                    "0":(0,0,0),       \
                    "1":(0,0,180),     \
                    "!":(255,0,0)  }   # ! is error-marker

    with open( filename ) as f:#opens a txt file
        # first determine the map dimensions on the fly
        lines = f.readlines()#reads
        tile_height = len( lines )                               # count of lines => length
        tile_width  = len( max( lines, key=len ).split( "," ) )  # length of longest line  => width

        # create a new surface to hold the tile at the determined size
        tile = pygame.Surface( ( tile_width, tile_height ), pygame.SRCALPHA )

        # iterate through the rows, and columns of numbers
        # drawing pixels to the Surface
        for y, row in enumerate( lines ):#this turnes the original data into a string that can be parsed easily
            for x, item in enumerate( row.split( ',' ) ):
                item = item.strip().lower()
                if ( not item in colour_map ):
                    print( "Bad Map ID \"%s\" on column [%2d], row [%2d] (ignored)" % ( item, x+1, y+1 ) )
                    item = "!"  # red pixel
                tile.set_at( ( x, y ), colour_map[item] )  # red = error

    return tile

    
    
###
### Main
###

my_tile = loadTile( "Map_Floor.txt" )

#print("Out of function")

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

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_ESCAPE:
                print("quitting")
                pygame.quit()
                exit()

    screen.fill( ( 0,0,0 ) )
    screen.blit( my_tile, my_tile.get_rect() )  # paint the tile to (0,0)

    pygame.display.update()
    clock.tick(60)
Related