Skip to main content
1 of 3
Ian Moote
  • 214
  • 1
  • 8

You're question is a little unclear. Things move on maps, and/or maps move under things. Objects on maps are typically stored in lists or some other data structure.

For a zero-indexed matrix with (0,0) in the top left-hand corner the objects on the map can be find by y * mapWidth + x:

map = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]

for y in range(3):
    for x in range(5):
        print("({},{}) = {}".format(x, y, map[y*5+x]))

You'd probably have this in a function:

def whatsatthecoordinates(x, y):
   global map
   return map[y*5+x]

or you may actually pass the map to the function, which is a little more versatile:

def whatsatthecoordinates(map, x, y):
   return map[y*5+x]

or you could objectify map:

class Map():
    def __init__(self, xSize, ySize, listofmapcontents):
        if not type(listofmapcontents) == type(list()):
            raise RuntimeError
        self.xSize = xSize
        self.ySize = ySize
        self.array = listofmapcontents

    def whatsatthesecoordinates(self, x, y):
        if x < self.xSize and y < self.ySize:
            return self.array[y * self.xSize + x]
        else:
            print("Coordinates ({}, {}) out of range.")
            print("Should be x = 0 to {} and y = 0 to {}".format(self.xSize-1, self.ySize-1))
            return None

    def placearock(self, x, y):
        map[y * xSize + x] = 1

    def destroyarock(self, x, y):
        map[y * xSize + x] = 0

map = Map(5, 3, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0])

for y in range(3):
    for x in range(5):
        print("({},{}) = {}".format(x, y, map.whatsatthesecoordinates(x, y)))

Not fully tested. If this doesn't answer your question, please clarify.

Ian Moote
  • 214
  • 1
  • 8