在Pygame中重叠CSV文件中的平铺层

mfuanj7w  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(126)

我尝试在pygame中创建一个显示界面,其中包含多个瓦片层。我有一个背景、附件、brown_dirt、yellow_dirt和water csv文件,我正在从中阅读这些文件。然后,我将每个文件与我解析为32 x32瓦片的瓦片集中相应的瓦片进行匹配。
我需要帮助的是如何根据特定的顺序分层瓷砖层。Youtube视频上的最后一个链接从分钟50-1:10他走过添加一个y索引,以允许这一工作,但我不相信这会为我的问题工作,因为我有一个表面为每个给定的瓷砖我想把。
下面是这个问题的所有相关代码,但最重要的部分可以在YSortCameraGroup中找到。

def import_cut_graphics(image_path):
    surface = pygame.image.load(image_path).convert_alpha()
    tile_num_x = int(surface.get_size()[0] / 32)
    tile_num_y = int(surface.get_size()[1] / 32)

    cut_tiles = []

    for row in range(tile_num_y):
        for col in range(tile_num_x):
            x = col * 32
            y = row * 32
            new_surface = pygame.Surface((32, 32))
            new_surface.blit(surface, (0, 0), pygame.Rect(x, y, 32, 32))

            cut_tiles.append(new_surface)
            
    return cut_tiles

tile_list = import_cut_graphics('./images/tiles/tiles-original.png')

class Tile(pygame.sprite.Sprite):
    """class to read and create individual tiles and place in display"""
    def __init__(self, x, y, image_number, main_group, sub_group=""):
        super().__init__()

        if image_number == -1:
            pass
        else:
            self.image = tile_list[image_number]

            main_group.add(self)

            # rect and positioning
            self.rect = self.image.get_rect()
            self.rect.topleft = (x, y)   

class YSortCameraGroup(pygame.sprite.Group):
    def __init__(self):
        super().__init__()

        
    def custom_draw(self):
        for sprite in self.sprites():
            display_surface.blit(sprite.image, sprite.rect)
            

# create sprite groups
visible_sprites = YSortCameraGroup()

main_tile_group = pygame.sprite.Group()

background_tiles = []
brown_dirt_tiles = []
accessories_tiles = []
water_tiles = []
yellow_dirt_tiles = []

with open("./csv/Level One/Background.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        background_tiles.append(row)
with open("./csv/Level One/Accessories.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        accessories_tiles.append(row)
with open("./csv/Level One/Brown Dirt.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        brown_dirt_tiles.append(row)
with open("./csv/Level One/Water.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        water_tiles.append(row)
with open("./csv/Level One/Yellow Dirt.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        yellow_dirt_tiles.append(row)

# i have all the csv files in arrays and the tile_list, now I need to match them 

for i in range(len(background_tiles)):
    for j in range(len(background_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(background_tiles[i][j]), main_tile_group)

for i in range(len(brown_dirt_tiles)):
    for j in range(len(brown_dirt_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(brown_dirt_tiles[i][j]), visible_sprites)

for i in range(len(accessories_tiles)):
    for j in range(len(accessories_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(accessories_tiles[i][j]), visible_sprites)

for i in range(len(water_tiles)):
    for j in range(len(water_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(water_tiles[i][j]), visible_sprites)

for i in range(len(yellow_dirt_tiles)):
    for j in range(len(yellow_dirt_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(yellow_dirt_tiles[i][j]), visible_sprites)

我尝试过的相关问题和链接:Overlay tiles onto sprite in pygameHow do I read a .csv file for a tile map in pygame?https://www.youtube.com/watch?v=QU1pPzEGrqw&ab_channel=ClearCode
每一个都解决了不同的问题,或者涉及的主题太浅。我相当肯定这些更改需要在我编写的custom_draw方法内部进行(来自YouTube视频,但不确定要添加什么)。
代码生成以下显示:

有人知道我可以使用的任何资源或我可能错过的此过程的方法吗?

zpf6vheq

zpf6vheq1#

平铺图像的背景不透明。使用pygame.SRCALPHA标志创建RGBA格式的平铺表面:
new_surface = pygame.Surface((32, 32))

new_surface = pygame.Surface((32, 32), pygame.SRCALPHA)

相关问题