Created
May 26, 2019 19:59
-
-
Save gentimouton/220022b3d501839e553643c2fff5559f to your computer and use it in GitHub Desktop.
generate items for an RPG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math, random | |
# constants | |
STATS = ['int','str','vit'] | |
RARITY_MULT = { | |
'common': 1, | |
'rare': 1.25, | |
'epic': 1.5, | |
'legendary': 2 | |
} | |
class Item(): | |
def __init__(self, name, ilvl, slots, rarity): | |
self.name = name | |
self.ilvl = ilvl | |
self.rarity = rarity | |
stats_per_slot = round(math.sqrt((RARITY_MULT[rarity] * ilvl) ** 2 / slots)) | |
stats = random.sample(STATS, slots) | |
self.stats = {s: stats_per_slot for s in stats} | |
def __str__(self): | |
stats_txt = '\n'.join([ | |
stat + ' ' + str(val) for stat, val in self.stats.items() | |
]) | |
text = "{rarity} {name}\n{stats}".format( | |
rarity=self.rarity, | |
name=self.name, | |
stats=stats_txt | |
) | |
return text | |
item = Item('sword', 100, 1, 'epic') | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment