Created
May 21, 2011 15:43
-
-
Save fanzeyi/984632 to your computer and use it in GitHub Desktop.
血量记录器... TRPG用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[fanzeyi@localhost TRPG]$ python blood.py | |
Status Maker Version 0.1 alpha | |
Copyright (C) 2011 fanhe.org | |
Status > people Zeray 30 | |
Status > print | |
+----------------------------+ | |
| Status | | |
+----------------------------+ | |
| Zeray | | |
|[===================](30/30)| | |
+----------------------------+ | |
Status > hit Zeray 13 | |
Status > print | |
+----------------------------+ | |
| Status | | |
+----------------------------+ | |
| Zeray | | |
|[========== ](17/30)| | |
+----------------------------+ | |
Status > hit Zeray 18 | |
Status > print | |
+----------------------------+ | |
| Status | | |
+----------------------------+ | |
| Zeray | | |
| DEAD | | |
+----------------------------+ | |
Status > recover Zeray 20 | |
Status > print | |
+----------------------------+ | |
| Status | | |
+----------------------------+ | |
| Zeray | | |
|[============ ](19/30)| | |
+----------------------------+ | |
Status > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import cmd | |
WIDTH = 30 | |
VERSION = "0.1 alpha" | |
AUTHOR = "Zeray Rice <fanzeyi1994@gmail.com>" | |
INTRO = '''Status Maker Version %s | |
Copyright (C) 2011 fanhe.org | |
''' % (VERSION) | |
HELP_MSG = '''List of Commands | |
people -- Add a people Profile | |
hit -- Hit one people | |
recover -- Recover one people | |
print -- Print Status List | |
width -- Set List Width | |
title -- Set List Title | |
Type help <command> for more information about a command.''' | |
class StatusMaker(cmd.Cmd): | |
def __init__(self): | |
cmd.Cmd.__init__(self) | |
self.prompt = "Status > " | |
self.width = WIDTH | |
self.title = "Status" | |
self.line = "+%s+" % ("-" * ( WIDTH - 2 )) | |
self.header = "%s\n%s\n%s" %(self.line, | |
self.MKLine(self.title), | |
self.line) | |
self.people = {} | |
def MKLine(self, string): | |
return "|%s|" % (string.center(self.width-2)) | |
def do_title(self, args): | |
self.title = args | |
self.header = "%s\n%s\n%s" %(self.line, | |
self.MKLine(self.title), | |
self.line) | |
def help_title(self): | |
print '''Set the Title of List | |
USAGE: title [title]''' | |
def do_width(self, args): | |
try: | |
width = int(args) | |
except ValueError: | |
print "ERROR: TypeError, please use a Number!" | |
return 0 | |
if width < 10: | |
print "ERROR: Width MUST be larger than 10!" | |
return 0 | |
self.width = width | |
self.line = "+%s+" % ("-" * ( self.width - 2 )) | |
self.header = "%s\n%s\n%s" %(self.line, | |
self.MKLine(self.title), | |
self.line) | |
def help_width(self): | |
print '''Set the Width of List | |
USAGE: width [width]''' | |
def do_people(self, args): | |
try: | |
name, maxhp = args.split(" ") | |
except ValueError: | |
print "ERROR: ValueError!\nUSAGE:people [name] [maxhp]" | |
try: | |
maxhp = int(maxhp) | |
except ValueError: | |
print "ERROR: Maxhp MUST be a Integer!" | |
self.people[name] = {'maxhp':maxhp, \ | |
'hp':maxhp} | |
def help_people(self): | |
print '''Add a People Profile | |
USAGE: people [name] [maxhp]''' | |
def do_print(self, args): | |
print self.header | |
people = self.people | |
for name in people: | |
print "| %s|" % ( name + \ | |
" " * ( self.width - 3 - \ | |
len(name))) | |
if people[name]['hp'] <= 0: | |
print self.MKLine("DEAD") | |
print self.line | |
return 0 | |
hp = "(%s/%s)" % (str(people[name]['hp']), \ | |
str(people[name]['maxhp'])) | |
whp = self.width - 4 - len(hp) | |
ehp = int(people[name]['hp'] / float(people[name]['maxhp']) * whp) | |
print "|[%s%s]%s|" % ("=" * ehp, " " * (whp - ehp), hp) | |
print self.line | |
def help_print(self): | |
print '''Print the Status List | |
USAGE: print ''' | |
def do_hit(self, args): | |
try: | |
name, hit = args.split() | |
except ValueError: | |
print "ERROR: ValueError!\nUSAGE: hit [name] [int]" | |
return 0 | |
try: | |
hit = int(hit) | |
except ValueError: | |
print "ERROR: Hit Number should be a Integer!" | |
return 0 | |
try: | |
self.people[name] | |
except KeyError: | |
print "ERROR: People Not Found!" | |
self.people[name]['hp'] -= hit | |
def help_hit(self): | |
print '''Hit Someone | |
USAGE: hit [name] [hit]''' | |
def do_recover(self, args): | |
try: | |
name, recover = args.split() | |
except ValueError: | |
print "ERROR: ValueError!\nUSAGE: recover [name] [int]" | |
return 0 | |
try: | |
recover = int(recover) | |
except ValueError: | |
print "ERROR: Hit Number should be a Integer!" | |
return 0 | |
try: | |
self.people[name] | |
except KeyError: | |
print "ERROR: People Not Found!" | |
if self.people[name]['hp'] + recover > self.people[name]['maxhp']: | |
self.people[name]['hp'] = self.people[name]['maxhp'] | |
return 0 | |
self.people[name]['hp'] += recover | |
def help_recover(self): | |
print '''Recover Someone | |
USAGE: recover [name] [recover]''' | |
def do_help(self, args): | |
arg = args.split() | |
if len(arg) != 0: | |
cmd.Cmd.do_help(self, args) | |
return 0 | |
print HELP_MSG | |
def main(): | |
loop = StatusMaker() | |
try: | |
loop.cmdloop(INTRO) | |
except KeyboardInterrupt: | |
print "" | |
return 0 | |
return 0 | |
if __name__ == '__main__': | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment