Loading A Map In Pygame / Python
Solution 1:
Though some people have helped open a file, I understand you are actually looking to import a text file as a map: I did not write this, but have been using it as an example for my game:
# This code is in the Public Domain# -- richard@mechanicalcat.netclassMap:
def__init__(self, map, tiles):
self.tiles = pygame.image.load(tiles)
l = [line.strip() for line inopen(map).readlines()]
self.map = [[None]*len(l[0]) for j inrange(len(l))]
for i inrange(len(l[0])):
for j inrange(len(l)):
tile = l[j][i]
tile = tile_coords[tile]
if tile isNone:
continueelifisinstance(tile, type([])):
tile = random.choice(tile)
cx, cy = tile
if random.choice((0,1)):
cx += 192if random.choice((0,1)):
cy += 192
self.map[j][i] = (cx, cy)
defdraw(self, view, viewpos):
'''Draw the map to the "view" with the top-left of "view" being at
"viewpos" in the map.
'''
sx, sy = view.get_size()
bx = viewpos[0]/64
by = viewpos[1]/64for x inrange(0, sx+64, 64):
i = x/64 + bx
for y inrange(0, sy+64, 64):
j = y/64 + by
try:
tile = self.map[j][i]
except IndexError:
# too close to the edgecontinueif tile isNone:
continue
cx, cy = tile
view.blit(self.tiles, (x, y), (cx, cy, 64, 64))
deflimit(self, view, pos):
'''Limit the "viewpos" variable such that it defines a valid top-left
rectangle of "view"'s size over the map.
'''
x, y = pos
# easy
x = max(x, 0)
y = max(y, 0)
# figure number of tiles in a view, hence max x and y viewpos
sx, sy = view.get_size()
nx, ny = sx/64, sy/64
mx = (len(self.map[0]) - nx) * 64
my = (len(self.map) - ny) * 64print y, my
return (min(x, mx), min(y, my))
defmain():
pygame.init()
win = pygame.display.set_mode((640, 480))
map = Map('map.txt', 'tiles.png')
viewpos = (0,0)
move = False
clock = pygame.time.Clock()
sx, sy = win.get_size()
while1:
event = pygame.event.poll()
while event.type != NOEVENT:
if event.typein (QUIT, KEYDOWN):
sys.exit(0)
elif event.type == MOUSEBUTTONDOWN:
x, y = viewpos
dx, dy = event.pos
x += dx - sx/2
y += dy - sy/2
viewpos = map.limit(win, (x, y))
move = True
event = pygame.event.poll()
win.fill((0,0,0))
map.draw(win, viewpos)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
Solution 2:
If you're literally asking how to load a file in Python -- disregarding the pygame
side of the question -- then it's very simple.
>>>withopen('a.map', 'r') as f:...for line in f:...print line,...
x g g g x
x g g g x
x g x g x
x g g g x
x x x x x
KEY:
x = wall
g = grass / floor
Now instead of printing each line, you can simply read through it and store it in whatever data structure you're using.
I don't know anything about pygame
though -- if it has some custom function for this, I can't help with that.
Solution 3:
Since a map is just a 2d array, it's two loops to get through the whole thing.
self.map = [[None]*len(l[0]) for j inrange(len(l))]
for i inrange(len(l[0])):
for j inrange(len(l)):
Details can be found many places. Here's one: http://www.mechanicalcat.net/richard/log/Python/PyGame_sample__drawing_a_map__and_moving_around_it
Inside you'd determine what to draw. In your case: wall, grass, or floor, which would be sprites.
Post a Comment for "Loading A Map In Pygame / Python"