Text-Based RPG (In Development)

Text-Based RPG (In Development)

Postby Velcrode » Sat Jul 20, 2013 1:36 am

I've been putting energy into a new text-based RPG, which so far has many bugs I can't figure out how to work around. Regardless, I'm going to post the code, so if anyone wants to help or just to play the game, it's available. I'll update it as I work out the kinks. So far it has no title.

Code: Select all
import random
import sys
from random import randint

class herb:
   def __init__(self, name, effect):
      self.name = name
      self.effect = effect

class Race:
   def __init__(self, name):
      self.name = name

class area:
   def __init__(self, name):
      self.name = name

class weapon:
   def __init__(self, name, value, damage):
      self.name = name
      self.value = value
      self.damage = damage

class armor:
   def __init__(self, name, value, defense):
      self.name = armor_dict.keys()
      self.value = value
      self.defense = defense

class Character:
   def __init__(self):
      self.name = ""
      self.health = 1
      self.health_max = 1
   def do_damage(self, enemy):
      damage = min(
         max(randint(0, self.health) - randint(0, self.enemy.health), 0), self.enemy.name)
      self.enemy.health = self.enemy.health - damage
      if damage == 0:
         print "The %s evades %s's attack." % (self.enemy.name, self.name)
      else:
         print "%s hurts the %s! -%dhp" % (self.name, self.enemy.name, damage)
      return enemy.health <= 0

class Enemy(Character):
   def __init__(self, name, health):
      self.name = enemy_dict[name]
      self.health = enemy_dict[health]

class Player(Character):
   def __init__(self):
      Character.__init__(self)
      self.state = 'normal'
      self.merchstate = 'normal'
      self.level = 1
      self.health = 10
      self.health_max = 10
      self.loot = 10
      self.stim = 2
      self.weapon = ["None"]
      self.armor = ["Tattered Clothes"]
      self.weaponinv = []
      self.armorinv = []
      self.herb = []
      self.xp = 0
      self.time = 0
      self.xp_to_next_level = (self.level * 100) - self.xp
      if self.loot >= 10000:
         print 'You win!!'
         sys.exit()
   def quit(self):
      print "%s gets overworked and commits seppuku. Sucks for you." % self.name
      self.health = 0
   def help(self):
      print Commands.keys()
   def levelUp(self):
      if self.xp == self.level * 100:
         self.level = self.level + 1
         self.health_max = self.health_max + 1
         self.health = self.health_max
         print"%s feels stronger!" % self.name
   def status(self):
      print "\n  ~~~      \n%s, Level %d\nExperience to Next Level: %d\nHealth: %d/%d\nGold: %d\nPotions: %d\nWeapon: %s\nArmor: %s\n  ~~~      \n" % (self.name, self.level, self.xp_to_next_level, self.health, self.health_max, self.loot, self.stim, self.weapon, self.armor)
   def checkInventory(self):
      print "\nWeapon: %s\nArmor: %s\nHerbs: %s\n" % (self.weapon, self.armor, self.herb)
   def buyPotion(self):
      if self.merchstate == 'buy':
         if self.merchstim > 0:
            if self.loot >= 10:
               self.stim = self.stim + 1
               self.loot = self.loot - 10
               self.merchstim = self.merchstim - 1
               print "You buy a potion from the merchant."
            else: print "You don't have enough gold to buy a potion (need 10 gold)."
         else: print "The merchant is out of potions. Come back later."
      else: print "There's no merchant here."
   def sellPotion(self):
      if self.merchstate == 'buy':
         if self.stim > 0:
            self.stim = self.stim - 1
            self.loot = self.loot + 5
            self.merchstim = self.merchstim + 1
            print "The merchant chuckles. 'Potions to spare, eh?' He tosses you a light bag of coin."
         else: print "You don't have any potions to sell."
      else: print "There's no merchant here."
   def buyWeapon(self):
      pass
      if self.merchstate == 'buy':
         if self.loot >= self.level * 30:
            self.loot = self.loot - (self.level * 30)
   def buyArmor(self):
      pass
   def equipWeapon(self):
      toBeEquippedWeapon = raw_input("Equip: ")
      if self.weaponinv != toBeEquippedWeapon:
         print "You don't have this weapon."
      else:
         print "%s equips a %s" % (self.name, toBeEquippedWeapon)
         self.weapon = toBeEquippedWeapon
   def equipArmor(self):
      toBeEquippedArmor =  raw_input("Equip: ")
      if self.armorinv != toBeEquippedArmor:
         print "You don't have this armor."
      else:
         print "%s equips a %s" % (self.name, toBeEquippedArmor)
         self.armor = toBeEquippedArmor
   def stim(self):
      if self.stim > 0:
         if self.health < self.health_max:
            self.health = self.health + 5
            self.stim = self.stim - 1
            print "You drink a potion and feel rejuvenated. (+5 health)"
            if self.health > self.health_max:
               self.health = self.health_max
               print "%s is at full health." % self.name
         else: print "%s is already at full health." % self.name
      else: print "%s has no potions. Go buy some from a merchant." % self.name
   def merch_name(self):
      return random.choice(merchname_dict)
   def merch(self):
      self.merchstate == 'buy'
      self.merchstim = randint(0,10)
      print "You come across a merchant, who smiles and says jovially, 'Hello, I am %s.\nWhat can I do for you today?' Type 'mhelp' for help with trading." % self.merch_name()
   def merchHelp(self):
      self.merchstate = 'buy'
      print "Type 'buystim' to buy a potion (-10 gold). Type 'sellstim' to sell a potion (+5 gold)."
   def explore(self):
      self.merchstate == 'normal'
      if self.state != 'normal':
         print "You can't explore while fighting. Try the 'flee' command to get away."
         self.enemy_attacks()
      else:
         print "%s explores %s." % (self.name, self.area())
         if randint(0,1):
            self.enemy = random.choice(enemy_dict)
            print"%s encounters a %s!\nEnemy hp: %d" % (self.name, self.enemy, self.enemy)
            self.state = 'fight'
   def area(self):
      return random.choice(area_dict)
   def flee(self):
      if self.state != 'fight': print "%s looks around furtively, searching for the opponent he must flee from. He sees a squirrel eating an acorn by the trunk of an oak thirty feet away, and promptly turns tail away from the beast and runs screaming for half a mile." % self.name
      else:
         if randint(1, self.health + 5) > randint(1, self.enemy.health):
            print"%s flees from the %s." % (self.name, self.enemy.name)
            self.enemy = None
            self.state = 'normal'
   def goHome(self):
      if self.state != 'normal':
         print "You can't go home in the middle of a fight. Try the 'flee' command to get away."
      else:
         self.health = self.health_max
         day = day + 1
         print "After a long day of adventure, it's nice to return to your comfortable hut on the forest's edge. You sleep soundly, dreams of battle and treasure constant in your unconscious mind."
         print "It is now day %d of your... quest?" % (day)
   def collectPlants(self):
      if self.state == 'normal':
         print"%s spends an hour looking for medicinal plants." % self.name
         if random.choice([0,1]):
            foundHerb = random.choice(herb_dict)
            print "You find some %s." % foundHerb[0]
            self.herb.append(foundHerb[0])
            return foundHerb
         else: print"%s doesn't find anything useful." % self.name
   def useHerb(self):
      if self.state == 'normal':
         print'What herb would you like to use?'
         herbChoice = raw_input('')
         if herbChoice == self.herb:
            self.herb = self.herb - herbChoice
            print "You use %s." % herbChoice
            herb_dict.effects()
         else: print"You don't have this herb."
   def high(self):
      print "%s laughs at nothing for a while, wanders aimlessly, eats half his rations, then passes out, awakening later in a mental haze." % self.name
   def tripping(self):
      if randint(0,1) == 1:
         print"%s starts to feel strange after eating the fungus; soon he is plagued by demonic visions, and runs screaming throughout the forest as he's chased by imagined monsters. Eventually it wears off and he takes a long nap." % self.name
      else:
         print"%s starts to feel strange after eating the fungus; patterns begin to form in the sky and the trees seem to be smiling. He walks around for hours in this waking dream, then grows tired and rests, awakening with a peculiar sense of gained wisdom." % self.name
   def attack(self):
      if self.state != 'fight':
         print"%s swings wildly at nothing." % self.name
      else:
         if self.do_damage(self.enemy):
            print "%s slaughters %s!" % (self.name, self.enemy[0])
            self.enemy = None
            self.state = 'normal'
            eloot = randint(0,25)
            self.loot = self.loot + eloot
            qxp = randint(0,30)
            self.xp = qxp + self.xp
            print "You find %d gold on the corpse." %(eloot)
            stimp = randint(0,4)
            if stimp == 0:
               self.stim = self.stim + 1
               print "You find a potion!"
         else:
            self.enemy_attacks()
   def enemy_attacks(self):
      if self.enemy[0].do_damage(self): print "%s was disemboweled by %s!" % (self.name, self.enemy[0])
      print """

 ____    __    _    _   ____     ____   _    _  ____  ___
/    \  /  \  | \  / | |        /    \  |    | |     |   \
|      |    | |  \/  | |       |      | |    | |     |___/
|   __ |____| |      | |----   |      | |    | |---- |  \
|    | |    | |      | |       |      |  \  /  |     |   \
\____/ |    | |      | |____    \____/    \/   |____ |    \
"""

enemy_dict = {
   "sprite"    : 5,
   "thief"     : 10,
}

area_dict = [
   ("a cave"),
   ("a sparsely wooded copse"),
   ("a dense patch of undergrowth"),
   ("the edge of the mapped forest"),
   ("an abandoned cottage"),
   ("a dilapidated turret"),
   ("a human-sized rabbit warren"),
   ("around a lake"),
   ("a cliff above the sea")
]

merchname_dict = [
   ("Hjall"),
   ("Piotr"),
   ("Grallse"),
   ("Vaeaer"),
   ("Persey"),
   ("Frarwelk"),
   ("Wylm")
]

weapon_dict = [
   ("None", 0, 0),
   ("Stick", 1, 1),
   ("Dagger", 5, 2),
   ("Shortsword", 20, 5)
]

armor_dict = [
   ("Tattered Clothes", 0, 0),
   ("Woolen Cloak", 2, 1),
   ("Leather Jerkin", 5, 3)
]

herb_dict = [
   ("Aloe Vera", Player().stim),
   ("Cannabis", Player().high),
   ("Ergot", Player().tripping)
]

race_dict = [
   ("Human"),
   ("Dwarf"),
   ("Elf"),
   ("Falquon"),
   ("Dreigh")
]

Commands = {
   'quit': Player.quit,
   'help': Player.help,
   'stat': Player.status,
   'inv': Player.checkInventory,
   'home': Player.goHome,
   'exp': Player.explore,
   'merch': Player.merch,
   'mhelp': Player.merchHelp,
   'buystim': Player.buyPotion,
   'sellstim': Player.sellPotion,
   'herb': Player.useHerb,
   'forage': Player.collectPlants,
   'flee': Player.flee,
   'a': Player.attack,
   'stim': Player.stim
}

day = 0

p = Player()
print"\nA vast expanse of forest and wilderness stretchs for countless miles before you."
print"Somewhere in these woods, you will find fortune and glory. It is up to you to"
print"figure out how. Go, adventure!\n"
p.name = raw_input("What is your name, traveler? ")
print"\nType 'help' for a list of commands. 'exp' means explore, 'a' means attack,\n'stim' means use a potion."
print"\n%s enters the woods, in search of adventure.\n" % p.name
while(p.health > 0):
   line = raw_input(">>> ")
   args = line.split()
   if len(args) > 0:
      commandFound = False
      for c in Commands.keys():
         if args[0] == c[:len(args[0])]:
            Commands[c](p)
            commandFound = True
            break
      if not commandFound:
         print"That command doesn't exist. Type help if you're a noob and try again."
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am

Re: Text-Based RPG (In Development)

Postby micseydel » Sat Jul 20, 2013 3:38 am

You should put this on Github or Bitbucket, and then use the bug tracker on whichever one to help us to help you fix them.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 923
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Text-Based RPG (In Development)

Postby metulburr » Sat Jul 20, 2013 9:55 am

got this after a few 'exp'

Code: Select all
Traceback (most recent call last):
  File "test3.py", line 321, in <module>
    Commands[c](p)
  File "test3.py", line 157, in explore
    self.enemy = random.choice(enemy_dict)
  File "/usr/lib/python2.7/random.py", line 274, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
KeyError: 0
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1092
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests