-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLib.py
190 lines (155 loc) · 5.1 KB
/
Lib.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from random import randint
# This file contains the library of things that exist within the game.
# There are several spells detailed below. These spells are referenced by
# special items. They may potentially be referenced by creatures as well.
def magic_missile(target):
damage = randint(1, 4) + 1
target.hit_points = target.hit_points - damage
print """
A short bolt of pure force springs forth from your arm. It strikes {0} in the chest
and deals {1} points of damage.
{0} has {2} hit points left.
""".format(target.name, damage, target.hit_points)
def bolster(target):
hp = randint(1, 8) + 2
target.hit_points += hp
print "{0} has {1} hit points.".format(target.name, target.hit_points)
target.att += 1
target.damage += 1
target.buff += 1
print "{0} has been bolstered.".format(target.name)
def heal(target):
healing = randint(1, 8) + 1
if target.maxhp < target.hit_points + healing:
print "{0} gains {1} hit points.".format(
target.name, target.maxhp - target.hit_points
)
target.hit_points = target.maxhp
else:
print "{0} gains {1} hit points.".format(target.name, healing)
target.hit_points = target.hit_points + healing
# This is the list of classes of loot.
class Weapon(object):
def __init__(self, name, die, bonus):
self.name = name
self.die = die
self.bonus = bonus
class Armor(object):
# The slots are 0: chest, 1: head, 2: hands, 3: feet, 4: back
def __init__(self, name, action, bonus, slot):
self.name = name
self.action = action
self.bonus = bonus
self.slot = slot
class Shield(Armor):
def __init__(self, name, bonus):
self.name = name
self.bonus = bonus
class SpecialItem(object):
def special(self, target):
self.spell_name(target)
class Wand(SpecialItem):
def __init__(self, name, spell_name, num_targets):
self.name = name
self.spell_name = spell_name
self.num_targets = num_targets
class Potion(SpecialItem):
def __init__(self, name, spell_name):
self.name = name
self.spell_name = spell_name
def action(self):
self.spell_name(player)
# This is the complete list of items that exist in the game.
club = Weapon('Club', 4, 0)
dagger = Weapon('Dagger', 4, 1)
ssword = Weapon('Short Sword', 6, 1)
cloth = Armor('Cloth Armor', None, 1, 0)
leather = Armor('Leather Armor', None, 2, 0)
mail = Armor('Chain Mail', None, 4, 0)
mmwand = Wand('Wand of Magic Missile', magic_missile, 2)
buckler = Shield('Buckler', 1)
hpot1 = Potion('Blue Healing Potion', heal)
hpot2 = Potion('Green Healing Potion', heal)
# The creature class and its subclasses are rather bare bones. The only action
# handled in this class is the attack. Subclasses all have an __init__ that
# spells out combat statistics.
class Creature(object):
# The attack action is the most obvious. This is how one creature attempts
# to kill another creature. This action requires that target is another creature.
# All creatures must have the attributes:
# att, ac, damage, name, and hit_points
def attack(self, target):
if self.die == None:
print "That is impossible."
else:
result = randint(1, 20) + self.att
if result >= target.ac:
damage = randint(1, self.die) + self.damage
if damage <= 1:
damage = 1
pt = 'point'
else:
pt = 'points'
print "{0} hits {1}. The attack deals {2} {3} of damage.".format(
self.name, target.name, damage, pt)
target.hit_points = target.hit_points - damage
print "{0} has {1} hit points left".format(
target.name, target.hit_points
)
else:
print "{0} attacks {1} and misses.".format(self.name, target.name)
print "{0} has {1} hit points left".format(
target.name, target.hit_points
)
class PlayerCharacter(Creature):
armor = [cloth, None, None, None, None]
held = [club, None]
belt = [hpot1]
bag = []
def __init__(self, name, maxhp, combat, athletic):
self.name = name
self.maxhp = maxhp
self.hit_points = maxhp
self.ac = 10 + athletic + self.armor[0].bonus
self.att = 1 + combat + self.held[0].bonus
self.damage = combat + self.held[0].bonus
self.initiative = athletic
self.die = self.held[0].die
self.buff = 0
if self.held[1] != None:
if isinstance(self.held[1], Shield):
self.ac = self.ac + self.held[1].bonus
def stuff_names(self):
armor_names = []
for thing in self.armor:
if thing != None:
armor_names.append(thing.name)
held_names = []
for thing in self.held:
if thing != None:
held_names.append(thing.name)
belt_names = []
for thing in self.belt:
belt_names.append(thing.name)
bag_names = []
for thing in self.bag:
bag_names.append(thing.name)
return [bag_names, held_names, armor_names, belt_names]
# The game defaults to making a player named Steve with 14 hit points, combat = 2, and
# athletic = 1
player = PlayerCharacter('Steve', 14, 2, 1)
class Goblin(Creature):
def __init__(self, name):
self.name = name
self.maxhp = 4
self.hit_points = 4
self.ac = 10
self.att = 0
self.damage = -1
self.initiative = 2
self.die = 3
self.buff = 0
gob1 = Goblin('The Yellow Goblin')
gob2 = Goblin('The Green Goblin')
gob3 = Goblin('The Left Guard')
gob4 = Goblin('The Right Guard')