Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- ###
- '''
- http://www.reddit.com/r/learnpython/comments/19j8un/text_adventure_help/
- '''
- import time
- import random
- class Character:
- def __init__(self, speed, health, name):
- self.name = name
- self.healthmax = health
- self.health = health
- self.speed = speed
- def damage(self, enemy, damage):
- enemy.health = enemy.health - damage
- if damage == 0: print "%s evades %s attck!" % (enemy.name, self.name)
- else: print "%s hurts %s for %s!" % (self.name, enemy.name, damage)
- return enemy.health <= 0
- def speedCheck(self, player, enemy):
- if player.speed > enemy.speed:
- return True
- else:
- return False
- class Enemy(Character):
- def __init__(self, player, name, speed, health):
- Character.__init__(self, speed, health, name)
- self.name = name
- self.health = health
- self.healthmax = health
- class Player(Character):
- def __init__(self, name, speed, health):
- Character.__init__(self, speed, health, name)
- self.fight = False
- self.enemy = None
- def quit(self):
- print "%s Exits the game." % self.name
- self.health = 0
- def help(self):
- global Commands
- print Commands.keys()
- def status(self): print "%s health: %s/%s [Combat = %s]" % (self.name, self.health, self.healthmax, self.fight)
- def startFight(self):
- print 'What speed do you want the monster to have?'
- speedIn = int(raw_input("> "))
- enemyName = "Downvoter"
- self.enemy = Enemy(self, enemyName, speedIn, 10)
- print "%s encounters a %s!" % (self.name, enemyName)
- self.fight = True
- def attack(self):
- if self.fight == False:
- print "%s bats stupidly at the air..." % self.name
- else:
- if self.speedCheck(self, self.enemy):
- if self.damage(self.enemy, 10):
- print "%s executes %s!" % (self.name, self.enemy.name)
- self.enemy = None
- self.fight = False
- else:
- self.enemy_attacks()
- def enemy_attacks(self):
- if self.enemy.damage(self, 10):
- print "%s was murdered by %s" % (self.name, self.enemy.name)
- def main():
- global Commands
- Commands = {
- 'quit': Player.quit,
- 'help': Player.help,
- 'status': Player.status,
- 'fight': Player.startFight,
- 'attack': Player.attack,
- }
- print "What is your name?"
- userName = str(raw_input("> "))
- P = Player(userName, 10, 10)
- while P.health > 0:
- indicate = raw_input("> ")
- args = indicate.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 "%s does not understand..." % P.name
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement