6
\$\begingroup\$

A friend and I watched most of the thenewboston syntax videos for Python 3 and spent 2 hours attempting to piece together our first program / generic text adventure. First experience with coding.

I'm looking for feedback on silly code and poor conventions.

Pastebin

class Erdrick():
    exp = 0
    level = 1
    levelupthresholds = [10, 30, 60, 100]
    #some sort of xp system here    #where we left off

    equipment = {'Copper Sword': 3, 'Leather Tunic': 3}             # currently equipped
    weapon = equipment['Copper Sword']
    armor = equipment['Leather Tunic']
    damage = 2 + 1 * (level - 1) + weapon
    health = 20 + 10 * (level - 1) + armor

class Enemy():
    def __init__(self, name, health, damage, exp):
        self.name = name
        self.health = health
        self.damage = damage
        self.exp = exp

hero = Erdrick() 
slime1 = Enemy('Slime', 7, 2, 2)

Kind of stuck on integrating character levels, xp, and level up thresholds.

The math for damage and health is probably stupid

Doubtful that our implementation of classes and creating objects (hero/enemies) is better than bad

print(slime1.name, str(slime1.health))
print("You encounter a %s!,"
      "\n\t[%s] Health: %s\n" % (slime1.name, slime1.name, str(slime1.health)))

while hero.health > 0 and slime1.health > 0:
    print("\tYou deal %s damage!" % hero.damage)
    slime1.health -= hero.damage
    if slime1.health <= 0:
        print("\tSlime is defeated!!")
    else:
        print("\t[Slime] Health: %s" % slime1.health)
        print("\tSlime deals %s damage!" % slime1.damage)
        hero.health -= slime1.damage
        if hero.health <= 0:
            print("YOU DIED")
        else:
            print("\t[Hero] Health: %s" % hero.health)

Combat will be revised with prompts and options. It's currently (poorly?) automated just to have things work.

Hero and Enemy health will be formatted to print together in more of a typical horizontal information layout you'd find in a traditional RPG.

\$\endgroup\$
1
  • \$\begingroup\$ The code is quite good, and my review would mostly be about unused code... Keep it up! Working on text adventures games is how I learned to program: continue to make things more complex and don't worry if you feel like you need to start over to make things better. \$\endgroup\$ – Quentin Pradet Jun 9 '16 at 12:12
1
\$\begingroup\$

Actually, I agree with Quentin. For a first effort, your code looks pretty good. You have a hero class called Erdrick and an Enemy class instantiated as a Slime.

A class structure like what I show below would allow you more flexibility with less repetition. See what you think...

You might want to work out some way for specialized weapons to increase the damage a hero deals and for armor to reduce the damage suffered.

class Character():
    def __init__(self, name, health, damage, 
                 level=None, experience=None, equipment=None):
        self.name = name
        self.health = health
        self.damage = damage
        self.level = level or 1
        self.experience = experience or 0
        self.equipment = equipment or {}

    def strikes(self, other):
        other.health -= self.damage

    def alive(self):
        return 0 < self.health

class Hero(Character):
    level_thresholds = {10: 2, 30: 3, 60: 4, 100: 5}
    def grows(self):
        self.experience += 1
        curlev = self.level
        for thresh in level_thresholds:
            if thresh <= self.experience and self.level < level_thresholds[thresh]:
                self.level = level_thresholds[thresh]

        if curlev < self.level:
            print("{0} leveled up!".format(self.name))


class Monster(Character):
    ...

erdrick = Hero('Erdrick', 100, 2, equipment = {'Copper Sword': 3, 
                                               'Leather Tunic': 3})
slime = Monster('Slimebot', 50, 3)
while erdrick.alive() and slime.alive():
    erdrick.strikes(slime)
    slime.strikes(erdrick)

if erdrick.alive():
    print("{0} has defeated the {1}!".format(erdrick.name, slime.name))
    erdrick.grows()
else:
    print("The {0} has killed {1}... );".format(slime.name, erdrick.name)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.