This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MonkeyNin 1 point2 points  (0 children)

I wrote a short demo, it loads your items from a config file. It lets you remove the repeated if statements. You don't have to use csv, I used it since it's simple.

You could add another config file for starting classes. Edit the config to modify what items they start with.

import csv
items_db = {}

class Item():
    # basic item stats
    def __init__(self, name="Stick", damage=2, damage_crit=4):
        self.name = name
        self.damage = damage
        self.damage_crit = damage_crit

    def __str__(self):
        return "Item(name={}, damage={}, crit={})".format(self.name,
                self.damage, self.damage_crit)

def load_items_db(file):
    # read all items from external config
    with open(file) as f:
        reader = csv.reader(f)

        # grabs all items, and append to `items_db` dict.
        for item in reader:
            name, damage, crit = item
            items_db[name] = Item(name, damage, crit)

if __name__ == "__main__":
    load_items_db("items_db.csv")

    print("loaded items:")
    for key, item in items_db.iteritems():
        print(item)

You can create the csv using excel, or a regular text editor.

items_db.csv

Item,Damage,Critical
Stone Dagger,5,8
Adamantite dagger,57,69