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 MapTile
s, 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.