1

I have generated a CSV file from Tiled showing my gam map, currently have it broken down into 4 files. At this time trying to load in one layout that is my base layer and includes grass, dirt, etc. When loading it in the map is rotated and flipped and unrecognizable. I've tried playing with the variables but I can't figure out why it's rotated. Not only that but the spritesheet is loading the wrong image.

Here is a picture of the map the way it is supposed to be:game map minus the vegetation

And here is what happens when I run the game:

enter image description here

Here is the sprite sheet that i'm usingenter image description here

Here is the code, this should be a full running example:

import pygame,sys
from pygame.locals import *
import csv

running = True
pygame.init()
screen = pygame.display.set_mode((1280, 900))

def load_sprite_sheet(x_range, y_range, sprite_w, sprite_h):

    x_coord = 16 * x_range
    y_coord = 16 * y_range
    width = sprite_w
    height = sprite_h

    sheet = pygame.image.load("assets/TileMap/tilemap_packed.png").convert_alpha()
    single_image = sheet.subsurface((x_coord, y_coord, width, height))
    print(x_coord, y_coord)

    return single_image

grass_layout = "assets/TileMap/layout_grass.csv"

grasses = {
    "0": load_sprite_sheet(0, 0, 16, 16),
    "1": load_sprite_sheet(1, 0, 16, 16),
    "3": load_sprite_sheet(2, 0, 16, 16),
    "12": load_sprite_sheet(0, 1, 16, 16),
    "13": load_sprite_sheet(1, 1, 16, 16),
    "14": load_sprite_sheet(2, 1, 16, 16),
    "24": load_sprite_sheet(0, 2, 16, 16),
    "25": load_sprite_sheet(1, 2, 16, 16),
    "26": load_sprite_sheet(2, 2, 16, 16),
    "36": load_sprite_sheet(0, 3, 16, 16),
    "37": load_sprite_sheet(1, 3, 16, 16),
    "38": load_sprite_sheet(2, 3, 16, 16),
    "39": load_sprite_sheet(3, 3, 16, 16),
    "40": load_sprite_sheet(4, 3, 16, 16),
    "41": load_sprite_sheet(5, 3, 16, 16),
    "42": load_sprite_sheet(6, 3, 16, 16),
    "43": load_sprite_sheet(7, 3, 16, 16),
}

while running:

    for event in pygame.event.get():
        # Handle a QUIT event and close the program safely
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()

        # Handle user pressing ESC and close program safely
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.quit()

    # Fill the screen
    screen.fill('BLUE')

    with open(grass_layout, 'r') as csv_file:

        grasses_map = csv.reader(csv_file)

        for x_index, row in enumerate(grasses_map, 0):
            for y_index, col in enumerate(row, 0):
                if col in grasses.keys():
                    screen.blit(grasses[col], (x_index * 16, y_index * 16))

    # Update the screen
    pygame.display.update()
AndrewRoman
  • 31
  • 1
  • 5

1 Answers1

3

You have confused the x and y coordinates (x_index and y_index). The row defines the y-coordinate and the column defines the y-coordinate:

for y_index, row in enumerate(grasses_map, 0):
    for x_index, col in enumerate(row, 0):
        if col in grasses.keys():
            screen.blit(grasses[col], (x_index * 16, y_index * 16))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174