Skip to content

Instantly share code, notes, and snippets.

@gentimouton
Created May 26, 2019 19:59
Show Gist options
  • Save gentimouton/220022b3d501839e553643c2fff5559f to your computer and use it in GitHub Desktop.
Save gentimouton/220022b3d501839e553643c2fff5559f to your computer and use it in GitHub Desktop.
generate items for an RPG
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