Reputation: 15
Need help to make the two dimensional map. I am trying to make a mario map. Where I've the picture. But just need to set them in. I just need help to define what Y is, so I can referre it to an image. I've the dictionary PICTURES.
NAMING = { # Naming from map to full name
'M': "mario",
'P': "peach",
'B': "bowser",
'G': "goomba",
'K': "koopa",
'C': "cap",
'L': "mushroom",
'V': "wall",
'Y': "wall",
'T': "gate"
}
PICTURES = { # Naming from map to image path
'M' : "gameImages/mario.png", "P" : "gameImages/peach.png",
"B" : "gameImages/bowser.png", "G" : "gameImages/goomba.png",
"K" : "gameImages/koopa.png", "C" : "gameImages/cap.png",
"L" : "gameImages/mushroom.png", "V" : "gameImages/wall.png",
"Y" : "gameImages/wall.png" , "T" : "gameImages/gate.png"
}
def readFile(filename):
''' Read map file to dictionary '''
pass
The TXT file
YYYYYYYYYYYYYYYYYYYYYY
YM Y
Y Y
YVVVVVV VVVVVVVVVVY
Y K G K G CY
Y K K KVVVVVVVVVVY
Y LY
YV VVVVVVVVVVVVVVVVVVY
YV VL G KLY
YV V VVVV G KKY
YVGV V VVVV Y
YV V V G V G VVVV Y
YV V V V V GY
YVKV V VG V Y
YV VGGG V V Y
YV G V Y
YVVVVVVVVVVVVVVVVVVKVY
YL K G Y
Y G Y
Y K G VBVY
Y K VTPY
YYYYYYYYYYYYYYYYYYYY Y
Y Y Y
Y YTY
Upvotes: 0
Views: 827
Reputation: 169338
Use enumerate()
while you're iterating over rows and columns.
The resulting dict will
if not c.isspace():
condition if you need them too.def read_map_file(filename):
char_positions = {}
with open(filename, 'r') as f:
for y, line in enumerate(f):
for x, c in enumerate(line):
if not c.isspace():
char_positions[(x, y)] = c
return char_positions
Upvotes: 1
Reputation: 1432
def readFile(filename):
map = []
with open(filename, 'r') as file:
for line in file.readlines():
row = []
for char in line.strip('\n\r'):
row.append(char)
map.append(row)
return map
Upvotes: 0