我试图使用Python的cmd模块来解释玩家的命令,但在“游戏世界”受到影响的情况下,我无法让它工作。在
从测试来看player.cmdloop()只是在player实例的作用域中运行;在下面的代码中,game()像通常那样运行,然后到达player.cmdloop()在哪一点player.cmdloop()次跑步。输入运行后,cmdloop()结束,返回顶部。在
我尝试过许多不同的方法,所以我认为这是我的设计/实现的一个问题,因为缺乏理解。在
任何帮助将不胜感激!在
import cmd
who_dict = {}
class MyConsole(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
def do_who(self, args):
game().who()
class Human(object, MyConsole):
"""The human player
"""
prompt = ">> "
def __init__(self, name, health, gold):
MyConsole.__init__(self)
self.name = name
self.health = health
self.gold = gold
def do_swing(self, args):
game().swing()
class Monster(object):
"""Item Class
"""
def __init__(self, name, hp):
self.name = name
self.hp = hp
def game():
global who_dict
""" Main game function """
# when game is started.
# load the game_state file
# initialize the console
# Sample game_state. Will be loaded from file.
game_state = {
'players' : [
Human('Joe', 100, 10),
Human('Frank', 100, 20)
],
'monsters' : [
Monster('goblin',1)
]
}
for player in game_state['players']:
who_dict.update({player.name:player})
def who():
for name in who_dict.keys():
print name
def swing():
print "You briefly flail your arms like a mad man, attempting to hit something."
player.cmdloop()
if __name__ == '__main__':
player = Human('Ben', 100, 10)
game()
结果:
^{pr2}$
目前没有回答
相关问题 更多 >
编程相关推荐