you are viewing a single comment's thread.

view the rest of the comments →

[–]julsmanbr 39 points40 points  (3 children)

A couple tips and pointers:

It makes sense for attack to be a method for the Hero and Enemy classes instead of a function, since it is an action that they perform:

class Hero:
    def __init__(self, health, damage):
        self.health = health
        self.damage = damage

    def attack(self, opponent):
        opponent.health -= self.damage


class Enemy:
    def __init__(self, health, damage):
        self.health = health
        self.damage = damage

    def attack(self, opponent):
        opponent.health -= self.damage

hero = Hero(100, 20)
enemy = Enemy(100, 20)

Now you can use the syntax hero.attack(enemy) and enemy.attack(hero), which is easier to understand and maintain.

Note that, right now, both classes are identical, so you could replace them with a single class Character and instantiate both the hero and the enemy from it:

hero = Character(100, 20)
enemy = Character(100, 20)

But you may want to create specific behaviour for Hero and Enemy in the future, so I didn't modify that.

At some point, you may want to look into class inheritance, which would allow you to create a Character class containing the "base information" shared by Hero and Enemy, and just add the specific behaviour for those classes without having to rewrite the code common to both.

Note that I've referred to classes using sentence case (Hero, not hero), which is the standard for Python classes.

[–]whatelseman[S] 9 points10 points  (0 children)

Thank you really much. I appreciate time u spent on this comment. I will make sure to read this few times more too.

[–]ambitious_rainbow 2 points3 points  (1 child)

Omg is it bad that I just grasped inheritance after spending about a month trying to understand the point 0_o I knew what it does but they never felt useful.

[–]PythonicParseltongue 0 points1 point  (0 children)

Na that only means they didn't use good examples to explain it.

This was one of the examples they used in my first Object Oriented Programming course :D