Wednesday, 22 January 2014

Trying to connect OOP to the design of a game...

As I'm quite interested in game design and development (but am also a newbie in programming), each time I hear a new concept I almost automatically start to think about how it can be applied to a game program.

Each type of game element can be defined as a class. For example, a monster can be initialized in a class:

class Monster:
    """A monster in the game."""

    def __init__(self: 'Monster', hp: int, sp: int, str: int, def: int):
        """A monster which has the given hp, sp, str and def values."""
        self.hp = hp
        self.sp = sp
        self.str = str
        self.def = def

While applying object orientation it makes the code easier to understand. One can define a class of Monster and just create Monster objects which are more apparent than functions explaining what quality each monster has.

Speaking of inheritance of classes, e.g. one can have a class Monster and a subclass BeastMonster which is a Monster which is more vulnerable against some specific spells and tough against some others.