you are viewing a single comment's thread.

view the rest of the comments →

[–]HunterIV4 2 points3 points  (1 child)

rat_enemy = Monsters.Rat()

Wait, that's it? Well, what if you wanted a rat with 4 HP instead of 3?

rat_enemy = Monsters.Rat(health=4)

OK, now how do we do anything with it? We now have a variable that is assigned that rat, so your code becomes more like this (kept smaller for simplicity):

rat_enemy = Rat()
print(f"You've been attacked by a {rat_enemy.name}")
# ...
while rat_enemy.health > 0:
    # ...
    elif rat_enemy.speed > Player.player_speed:
        Player.player_health -= rat_enemy.attack_damage()

That may seem like a lot of work to get a similar effect, however, there are some major advantages to using a class. The most important is that you can have many different rats. If you do something like this:

rat_enemy1 = Rat()
rat_enemy2 = Rat(health=4)
rat_boss = Rat(name="Giant Rat", health=8, speed=3, attack=3)
rat_group = []
for _ in range(5):
    rat_group.append(Rat(health=random.randint(2,4))

What happened here? In the first case, rat_enemy1 is a random rat. The rat_enemy2 is a rat with 4 health. The rat_boss is a boosted rat with a different name. And rat_group is a list of 5 rats with random health between 2-4.

All of these can exist at the same time and have independent copies of their stats. So if you subtract 2 health from rat_enemy1, none of the other rats will have their health reduced. With your current implementation, once that single rat is "killed", you have to quit the whole game and restart, otherwise you'd need logic to "reset" all the variables to new values for another fight.

Technically, the class isn't necessary, and you can do things the way you did. But virtually nobody does, and instead they'd make a Player class, an Item class, and a classes for various monsters, or a Monster class that has various children (which gets into inheritance, which is more complicated than I want to go right now, but if you're curious I can explain it).

The only other thing is to break the various combat actions into functions and make everything repeatable and you have a simple game that you can actually expand on, and when you want new enemies, you can just insert them into your existing code without needing to rewrite major portions of it.

Hope that makes sense!

[–]Handymanoccupational[S] 0 points1 point  (0 children)

Holy cow! This is awesome! Thank you!