Advertisement
Guest User

http://www.reddit.com/r/learnpython/comments/19j8un/text_adv

a guest
Mar 7th, 2013
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ###
  3. '''
  4. http://www.reddit.com/r/learnpython/comments/19j8un/text_adventure_help/
  5. '''
  6.  
  7. import time
  8. import random
  9.  
  10. class Character:
  11.     def __init__(self, speed, health, name):
  12.         self.name = name
  13.         self.healthmax = health
  14.         self.health = health
  15.         self.speed = speed
  16.    
  17.     def damage(self, enemy, damage):
  18.         enemy.health = enemy.health - damage
  19.         if damage == 0: print "%s evades %s attck!" % (enemy.name, self.name)
  20.         else: print "%s hurts %s for %s!" % (self.name, enemy.name, damage)
  21.         return enemy.health <= 0
  22.  
  23.     def speedCheck(self, player, enemy):
  24.         if player.speed > enemy.speed:
  25.             return True
  26.         else:
  27.             return False
  28.  
  29. class Enemy(Character):
  30.     def __init__(self, player, name, speed, health):
  31.         Character.__init__(self, speed, health, name)
  32.         self.name = name
  33.         self.health = health
  34.         self.healthmax = health
  35.  
  36. class Player(Character):
  37.     def __init__(self, name, speed, health):
  38.         Character.__init__(self, speed, health, name)
  39.         self.fight = False
  40.         self.enemy = None
  41.  
  42.     def quit(self):
  43.         print "%s Exits the game." % self.name
  44.         self.health = 0
  45.  
  46.     def help(self):
  47.         global Commands
  48.         print Commands.keys()
  49.     def status(self): print "%s health: %s/%s [Combat = %s]" % (self.name, self.health, self.healthmax, self.fight)
  50.  
  51.     def startFight(self):
  52.         print 'What speed do you want the monster to have?'
  53.         speedIn = int(raw_input("> "))
  54.         enemyName = "Downvoter"
  55.         self.enemy = Enemy(self, enemyName, speedIn, 10)
  56.         print "%s encounters a %s!" % (self.name, enemyName)
  57.         self.fight = True
  58.  
  59.     def attack(self):
  60.         if self.fight == False:
  61.             print "%s bats stupidly at the air..." % self.name
  62.         else:
  63.             if self.speedCheck(self, self.enemy):
  64.                 if self.damage(self.enemy, 10):
  65.                     print "%s executes %s!" % (self.name, self.enemy.name)
  66.                     self.enemy = None
  67.                     self.fight = False
  68.             else:
  69.                 self.enemy_attacks()
  70.  
  71.     def enemy_attacks(self):
  72.         if self.enemy.damage(self, 10):
  73.             print "%s was murdered by %s" % (self.name, self.enemy.name)
  74.  
  75.  
  76. def main():
  77.     global Commands
  78.     Commands = {
  79.         'quit': Player.quit,
  80.         'help': Player.help,
  81.         'status': Player.status,
  82.         'fight': Player.startFight,
  83.         'attack': Player.attack,
  84.     }
  85.  
  86.     print "What is your name?"
  87.     userName = str(raw_input("> "))
  88.     P = Player(userName, 10, 10)
  89.  
  90.     while P.health > 0:
  91.         indicate = raw_input("> ")
  92.         args = indicate.split()
  93.         if len(args) > 0:
  94.             commandFound = False
  95.             for c in Commands.keys():
  96.                 if args[0] == c[:len(args[0])]:
  97.                     Commands[c](P)
  98.                     commandFound = True
  99.                     break
  100.             if not commandFound:
  101.                 print "%s does not understand..." % P.name
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement