LogFAQs > #957567308

LurkerFAQs, Active DB, DB1, DB2, DB3, DB4, DB5, DB6, DB7, Database 8 ( 02.18.2021-09-28-2021 ), DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicITT: Learning Python
SomeLikeItHoth
08/30/21 1:15:06 AM
#192:


SpiritSephiroth posted...
I need to pick up again and maybe just learn more about them and how to use them before trying to implement them in my code.
Show us your full code. I can go over it and give a few suggestions.

If you need some more help understanding how classes work, watch that video series I posted. But basically, think of classes as a way to trim down your code. Say you are making an RPG. You aren't going to just have a single character, you're going to have multiple. You could make the character like this:

def character_one(char, health, attack, defense, strength, m_atk, mana):
char = archer,
health = 100,
attack = 20,
defense = 20,
str = 50,
m_attack = 15,
mana = 150,

That's 8 lines of code. Now pretend you want 8 characters. That's 64 lines of code + spacing so a bit over 70 lines of code just for the characters. Using classes cuts down on that because each character will have the same stats no matter what.

class Entity:
def __init__(self, char, health, attack, defense, str, m_atk, mana):
self.char = char,
self.health = health,
self.attack = attack,
self.defense = defense,
self.str = str,
self.m_atk = m_atk,
self.mana = mana,

For every class, the very first method (you call functions inside of classes, methods) will always be the __init__ method, followed by what stats each character will have. Afterwards you simply create a new variable with the amount for each stat.

a = Entity(char=archer, health=100, attack=20, def=15, str=50, m_atk=15, mana=200)
t = Entity(char=thief, health=100, attack=30, def=25, str=45, m_atk=5, mana=0)

And you can call the class as many times as you want, so 8 characters will be in 8 lines of code, rather than 64 lines. Does that make more sense?

1337toothbrush posted...
Alright, I made a quick and awful battle system with poor OOP abstraction
Nah fam. I really like that code. It's very neat. Where did you learn to program? And what other languages do you know?

---
FAM FOREVER.
... Copied to Clipboard!
Topic List
Page List: 1