Building a Tilemap in Python/Pygame and testing for mouse position
Question: Hey i appreciate any help you can provide

I am creating a tile-map for a test of a possible project. I have found a
tutorial which produced the tile-map effectively. I then tried to implement my
own code by making it loop through each X and Y coordinate testing if the
mouse is in the position of the block. If the mouse was positioned on top of
the tile a box would be drawn on it to create a visual for where the mouse
was. The problem i have is that the grid was made to look like this:

    
    
                                        ####
                                        ####
                                        ####
                                        ####
    

But the mouse detection only works diagonally on these tiles:

    
    
                                        ####
                                         ###
                                          ##
                                           #
    

The code is below:

    
    
        from pygame.locals import *
    import pygame, sys
    
    green = (40,255,30)
    brown = (40,60,90)
    red = (155,20,30)
    yellow = (0,155,155)
    
    grass = 0
    dirt = 1
    lava = 2
    
    colours = {
        grass: green,
        dirt: brown,
        lava: red,
        }
    
    tilemap = [
            [grass,dirt,dirt,dirt, lava],
            [dirt,lava,dirt,dirt, dirt],
            [lava, grass,dirt,dirt, lava],
            [lava, grass,dirt,dirt, grass],
            [dirt,dirt,dirt,dirt,grass]
    
            ]
    
    TILESIZE = 50
    MAPWIDTH =  5
    MAPHEIGHT = 5
    
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))
    
    while True:
    
    
        mouse_x = pygame.mouse.get_pos()[0]
        mouse_y = pygame.mouse.get_pos()[1]
    
    
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit
            for row in range(MAPWIDTH):
                print
                for column in range(MAPHEIGHT):
                    pygame.draw.rect(DISPLAYSURF, colours[tilemap[row][column]], (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))
                    if mouse_x >= (row * TILESIZE) and mouse_x <= (row* TILESIZE) + TILESIZE:
                        if mouse_y >= (column * TILESIZE) and mouse_y <= (column* TILESIZE) + TILESIZE:
                            print (str(row) + " " + str(column))
                            pygame.draw.rect(DISPLAYSURF, yellow, (row * TILESIZE, column*TILESIZE, TILESIZE, TILESIZE))
    
    
    
    
    
    
    
        pygame.display.update()
    


Answer: First you're not clearing the screen. Next your draw code is wrong (wrong if
checks) The correct is x for columns and y for rows.

I hope that this could help you! :)

    
    
    from pygame.locals import *
    import pygame, sys
    
    green = (40,255,30)
    brown = (40,60,90)
    red =  (155,20,30)
    yellow = (0,155,155)
    
    grass = 0
    dirt = 1
    lava = 2
    
    colours = {
        grass: green,
        dirt: brown,
        lava: red,
        }
    
    tilemap = [
            [grass,dirt,dirt,dirt, lava],
            [dirt,lava,dirt,dirt, dirt],
            [lava, grass,dirt,dirt, lava],
            [lava, grass,dirt,dirt, grass],
            [dirt,dirt,dirt,dirt,grass]
    
            ]
    
    TILESIZE = 50
    MAPWIDTH =  5
    MAPHEIGHT = 5
    
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))
    

while True:

    
    
        mouse_x = pygame.mouse.get_pos()[0]
        mouse_y = pygame.mouse.get_pos()[1]
    
        print mouse_x, mouse_y
    
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
    
            DISPLAYSURF.fill((0,0,0));
            for row in range(MAPWIDTH):
                print            
                for column in range(MAPHEIGHT):
                    color = colours[tilemap[row][column]];
                    if mouse_x >= (column * TILESIZE) and mouse_x <= (column* TILESIZE) + TILESIZE:
                        if mouse_y >= (row * TILESIZE) and mouse_y <= (row* TILESIZE) + TILESIZE:
                            print (str(row) + " " + str(column))
                            color = yellow;                                                                                                            
    
                    pygame.draw.rect(DISPLAYSURF, color, (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))
    
    
        pygame.display.update()
    

