1

I'm trying to make a 10x10 grid for a map for a basic text-based game in python. I managed to create a MapTile class and a player class that can move about an (x, y) grid. I'm not sure however, how to create the instances of the class to have individual MapTiles. I've read that it would be arbitrary to create maptile1, maptile2... etc. for all 100 MapTiles, but I can't think of another way..

Here's what I've got!

# This class contains the x and y values for a grid
class MapTile:
    def __init__(self, x, y):
        self.x = x
        self.y = y

# basic char class containing position
class Character:
    def __init__(self, name, hp, x, y):
        self.name = name
        self.hp = hp
        self.x = x
        self.y = y

    # movement func
    def Move(self, Direction):
        if Direction.upper() == "UP":
            if self.y > 1:
                self.y -= 1
        elif Direction.upper() == "LEFT":
            if self.x > 1:
                self.x -= 1
        elif Direction.upper() == "RIGHT":
            if self.x < 10:
                self.x += 1
        elif Direction.upper() == "DOWN":
            if self.y < 10:
                self.y += 1

    def __str__(self):
        return "{}\n========\nHP = {}\nX = {}\nY = {}".format(self.name,
                                                              self.hp,
                                                              self.x,
                                                              self.y)

Let me know if I am unclear.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Put the 10x10 grid of `MapTile` instances in a `list`-of-`lists` that is indexed by `[x][y]`or a dictionary whose keys are `tuple`s of the `(x, y)` position of each. – martineau Oct 02 '17 at 02:40
  • So I should only have one instance and the (x,y) coordinates should be a list of lists? – Matthew Parnham Oct 02 '17 at 04:55

1 Answers1

0

As I said in a comment, I think you should use either a list-of-lists or a dictionary as a container for all the MapTile instances. Here's how to do both of them:

# This class contains the x and y values for a grid
class MapTile:
    def __init__(self, x, y):
        self.x = x
        self.y = y

XDIM, YDIM = 10, 10

# Create a list-of-lists.
grid = [[MapTile(x, y) for y in range(YDIM)] for x in range(XDIM)]

# Create a dictionary with tuple keys.
grid = {(x, y): MapTile(x, y) for x in range(XDIM) for y in range(YDIM)}

Doing something like this makes the x, y position information stored in the MapTile instances somewhat redundant, since it will be implied either by the indices or the keys used to reference each instance in the container. For example:

grid[1][2] contains MapTile(1, 2)

    in the first case, or

grid[(1, 2)] contains MapTile(1, 2)

in the second scenario. For that reason, you may want to leave it out of the MapTile class in your design. Otherwise, you're likely going to need update them when the location of their MapTile instance is changed within whatever type of grid container you've chosen to use.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Got it! Thank you so much! – Matthew Parnham Oct 02 '17 at 17:57
  • Matt: Another suggestion would be to consider creating yet another class, such as `class Game`, or `Board`, or something like that, which _has_ a `grid()` attribute, controls its creation, and provides other methods to manipulate the collection of `MapTile` instances it contains (like moving or drawing them). This class could also contain the instance(s) of the character(s) if that makes sense. – martineau Oct 02 '17 at 19:26
  • I like that idea! How would I go about drawing the instances? – Matthew Parnham Oct 04 '17 at 18:12
  • This is getting too far off-topic to continue much further, however that could be done by adding a `draw()` method to the `MapTile` class that accepts `x` and `y` arguments, so the class doesn't need to remember them. The `draw()` method of the `Board` class can iterate through the ones in its `grid` attribute and tell each one when to draw itself (and where to do so). It's called OOP (object-oriented programming). – martineau Oct 04 '17 at 18:23