You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.3 KiB
34 lines
1.3 KiB
from csv import reader
|
|
from os import walk
|
|
import pygame
|
|
|
|
# 定义一个函数,用于导入 CSV 布局文件并将其转换为二维列表
|
|
def import_csv_layout(path):
|
|
# 初始化一个空列表,用于存储布局数据
|
|
terrain_map = []
|
|
# 打开指定路径的 CSV 文件
|
|
with open(path) as level_map:
|
|
# 使用 `reader` 解析 CSV 文件
|
|
layout = reader(level_map,delimiter = ',')
|
|
# 遍历解析后的 CSV 数据的每一行
|
|
for row in layout:
|
|
# 将每行转换为列表并添加到 `terrain_map` 中
|
|
terrain_map.append(list(row))
|
|
# 返回表示 CSV 布局的二维列表
|
|
return terrain_map
|
|
# 定义一个函数,用于从文件夹中导入图像并返回加载后的表面列表
|
|
def import_folder(path):
|
|
# 初始化一个空列表,用于存储加载后的表面
|
|
surface_list = []
|
|
# 遍历从 `path` 开始的目录树
|
|
for _,__,img_files in walk(path):
|
|
# 遍历目录中的每个文件
|
|
for image in img_files:
|
|
# 构建图像文件的完整路径
|
|
full_path = path + '/' + image
|
|
# 加载图像作为带 alpha 通道的表面,并将其添加到 `surface_list` 中
|
|
image_surf = pygame.image.load(full_path).convert_alpha()
|
|
surface_list.append(image_surf)
|
|
# 返回包含加载后的表面的列表
|
|
return surface_list
|