タイルを使用してマップをレンダリングするために次のコードを作成しました。ファイルをループし、文字をタイル(長方形)に変換します。
currtile_x = 0
currtile_y = 0
singlerun = 1
if singlerun == 1:
singlerun = 0
with open('townhall.map', 'r') as f:
for line in f:
for character in line:
if character == "\n":
currtile_y += 10
else:
if character == "x":
pygame.draw.rect(screen, (1,2,3), (currtile_x, currtile_y, 10, 10), 0)
currtile_x += 10
else:
if character == "a":
pygame.draw.rect(screen, (0,255,255), (currtile_x, currtile_y, 10, 10), 0)
currtile_x += 10
これがtownhall.mapファイルです:
xxxxx
xaaax
xaaax
xaaax
xxxxx