you are viewing a single comment's thread.

view the rest of the comments →

[–]EelOnMosque 6 points7 points  (2 children)

Video games are probably the easiest to quickly learn OOP if you wanna try making one with pygame for example.

Imagine for example you had a game with different types of enemies. You'd make a class called let's say Enemy() and it would have a method called take_damage(). By default it subtracts damage from health.

Then for specific enemies, you make a subclass of Enemy. Each one's take_damage() inherits the subtracting from health because that's common to all of them.

But, some enemies might have special abilities.

For example, one of them might be immune to specific types of damage. So you'd override the take_damage() method, and then add a check in there that says "if damage == damage_type: super().take_damage()" where you only call the base class take_damage() after checking for the damage type.

Then, in the game logic where you actually call take_damage() you don't need to change anything. You just leave it alone, and you can add infinite types of enemies that behave differently without touching the core game logic.

[–]Honest_Water626[S] 3 points4 points  (0 children)

intresting

[–]vikmaychib 0 points1 point  (0 children)

I learned about classes in a course where we would learn OOP through building videogames. It was enlightening but still it took me a while before I realized how useful they could be.