Skip to content

Instantly share code, notes, and snippets.

@creiht
Created February 14, 2012 04:06
Show Gist options
  • Save creiht/1823468 to your computer and use it in GitHub Desktop.
Save creiht/1823468 to your computer and use it in GitHub Desktop.
Hacking DictMixin to give me access to a list in 2 dimensions
"""
Map stuff
Legend::
'#' = wall
'.' = ground
'+' = door
' ' = nothing
"""
from UserDict import DictMixin
class BaseMap(DictMixin):
"""
Base map class
"""
def __init__(self, width, height, fill=' '):
"""
Init the structures for the map, and fill the initial map with fill
"""
self.width = width
self.height = height
# Map is stored as a single list
self.tiles = [fill]*(width*height)
def __getitem__(self, key):
x,y = key
return self.tiles[y*self.width+x]
def __setitem__(self, key, value):
x,y = key
self.tiles[y*self.width+x] = value
def keys(self):
return [(x, y) for x in range(self.width) for y in range(self.height)]
def generate(self):
"""
Generate the map
"""
pass
def __str__(self):
rows = []
for line in range(self.height):
rows.append(''.join(
self.tiles[line*self.width:(line+1)*self.width]))
return '\n'.join(rows)
def __repr__(self):
return self.__str__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment