Untitled

 avatar
unknown
python
4 years ago
1.9 kB
7
Indexable
#funkcja globalna, moze byc uzywana w calym programie
def rollx():
    import random
    x = random.choice(range(1, 9))
    return x

#definicja klasy warrior. to taki szablon, klasa postaci, nie konkretny wojownik
class warrior:
    #statystyki wojownika. poczatkowe wartosci tylko pokazuja jakim typem powinny byc te zmienne. przypominam ze blok class w ktorym jestesmy to nie konkretny wojownik tylko szablon klasy postaci. ta klasa moze miec nastpujace staty
    name = ""
    health = 0
    strength = 0
    
    #konstruktor. wywoluje sie przy tworzeniu konkretnego obiektu tej klasy. zobacz kiedy wypisze sie do konsoli napis szkole wojownika. po tym poznasz kiedy wywoluje sie ten kod
    def __init__(self, name, hp, str):
        print("szkole wojownika: " + name)
        self.name = name
        self.health = hp
        self.strength = str
        
    #jesli sie zastanawiasz: w pytonie argument self w funkcji nalezacej do klasy oznacza, ze do wywolania funkcji trzeba konkretnego obiektu tej klasy. wiec to dziala tak: Seprioth.attack(Therg) - Sepe atakuje Therga. tutaj: argument otherWarrior to inny obiekt klasy warrior
    def attack(self, otherWarrior):
        damage = rollx() + self.strength
        otherWarrior.health = otherWarrior.health - damage
        print(self.name + " zaatakowal " + otherWarrior.name + " za " + str(damage))
        print("hp left: " + str(otherWarrior.health))

    def isDead(self):
        status = self.health <= 0
        if status == True:
            print(self.name + " nie zyje")
        return status

#tutaj zaczyna sie wlasxiwy program

print("start")
firstWarrior = warrior("maciek", 40, 4)
secondWarrior = warrior("franek", 32, 7)

while True:
    if secondWarrior.isDead() or firstWarrior.isDead():
        break
    firstWarrior.attack(secondWarrior)

    if firstWarrior.isDead() or secondWarrior.isDead():
        break
    secondWarrior.attack(firstWarrior)
        
Editor is loading...