1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
### 06/10/2012
### 17:07


#hp = hit points
#dmg = damage
#ac = armour

class Unit:
    unitlist = []
    def __init__(self, name, hp, dmg):
        self.name = name
        self.hp = hp
        self.dmg = dmg
        print("{0} has been created.".format(self.name))
        
        self.unitlist.append(self.name) #this is appending self.name now successfully. If I wanted to append the variable name instead, how would I do that? For example, below I have "a1 = Unit("goblin", 10, 4)". Is there any way to append "a1" instead of "goblin"? Thanks!
    def __del__(self):
        print("{0} has died.".format(self.name))
        
    def attack(self, target):
        if self.dmg > target.ac:
            target.hp -= self.dmg
            target.hp += target.ac
            print("{0} attacks {1} for {2} damage.".format(self.name, target.name, self.dmg-target.ac))
            
        else:
            print("{0} attacks {1} and deals no damage.".format(self.name, target.name))

###

class Player:
    def __init__(self, name="angus", hp=100, ac=1):
        self.name = name
        self.hp = hp
        self.ac = ac
        self.location = 0
        
    def attack(self, target):
        target.hp -= 10
        if target.hp <= 0:
            print("Target has died.")
            del(target) #why doesn't this seem to work?
            
    def defend(self):
        self.hp += 5
        self.ac += 1
        print("{0} enters a defensive stance, gaining {1} hit points and {2} armour.".format(self.name, 5, 1))

a1 = Unit("goblin", 10, 4)
a2 = Unit("orc", 15, 6)
a3 = Unit("rat", 1, 1)
p1 = Player()

print(p1.hp)
a1.attack(p1)
print(p1.hp)
a2.attack(p1)
print(p1.hp)
a3.attack(p1)
print(p1.hp)
p1.defend()
print(p1.hp)
a2.attack(p1)
print(p1.hp)

print("Unit list: " + str(Unit.unitlist))

p1.attack(a3)
print(a3) #shouldn't a3 have been deleted in the last line?