如何更改函数中调用的对象信息

时间:2016-01-12 04:25:10

标签: python python-2.7

我正在制作一个基于文本的RPG。我面临的问题是我想要个别敌人的价值观,即地精,兽人等,但我不知道如何允许一个函数从个别情况中获取特定类对象的信息。我可能写得这么糟糕;我很抱歉。举个例子:如果我每次用户进入一个有敌人的房间时都会调用一个单独的战斗功能,我该如何根据房间区分功能应该调用的敌人信息。

我可以使用敌人变量吗?比如说,当一个特定的房间功能被调用,并且房间里面有一个敌人时,一个名为敌人的全局变量可能会改变,并以某种方式允许战斗功能根据变量信息检索信息。换句话说,如果玩家进入带有地精的房间,该功能会将全局变量更改为“地精”。有没有办法让python查看该变量并说'哦!他想加载地精对象的数据!“?

以下是我的代码的基本版本:

enemy = ' '
class Enemy(object):
    def __init__(self, name, health, damage):
        self.name = name
        self.health = health
        self.damage = damage

def room():
    print("You walk into a room and see a goblin.  You enter combat. ")
    enemy = goblin.name
    combat()

def combat():
    dieroll = random.randint(1, 20)
    attack = raw_input("Press [A] to attack.: ").lower()
    while attack:
        if attack == 'a':

            #Here is where I want the function to retrieve information from an
            #enemy object, to determine its name, health, and damage.  

            print("You did", dieroll, "damage to" #enemy name)
            #lower enemy health



goblin = enemy('goblin', 100, 15)            

我希望其中一些是有意义的,即使代码有点草率,即使我对解决方案的想法可能是一个愚蠢的。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

不是定义一堆函数并在前一个函数的末尾调用每个函数,而是使用函数来构造代码。你现在可以硬编码一些东西,但最终你会想要某种循环来推动游戏直到它结束。

函数应该尽可能少地了解它们的周围环境 - 通常,您将外部数据传递到函数中,进行某种计算并返回值。由于room()依赖于goblin的创建,如果您决定创建不同类型的敌人,或者稍后创建敌人,或者只是拥有一个空房间,该功能将会中断。相反,传入任何敌人和对象以供使用的功能。

如果

combat()接受战斗员的参数,修改它们然后将一些结果返回给调用函数,那么

class Enemy(object): def __init__(self, name, health, damage): self.name = name self.health = health self.damage = damage def room(player, enemy=None): print "You walk into a room...", if enemy: print " and see a {}. You enter combat.".format(enemy.name) if combat(enemy, player): print 'You gain 3 experience.' player.xp += 3 # just for example else: print "there's nothing here." def combat(enemy, player): attack = raw_input("Press [A] to attack.: ").lower() while attack and player.health: if attack == 'a': dieroll = random.randint(1, 20) player.health = max(player.health-dieroll, 0) print enemy.name, 'did', dieroll, 'damage to you!' dieroll = random.randint(1, 20) enemy.health -= dieroll print "You did", dieroll, "damage to", enemy.name) return player.health # just for example player = Player('jim', 90, 18) # maybe make a Player class goblin = enemy('goblin', 100, 15) room(player, goblin) 会更有意义。这是一个例子:

print

另请注意,我已将print语句更正为Python 2样式print()语句。除非您要导入Python 3 print("You did", dieroll, "damage to" #enemy name)函数,否则tuple之类的内容会打印('You did', 14, 'damage to', 'goblin') ,从而生成如下内容:

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    try
    {
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => Xceed.Wpf.Toolkit.MessageBox.Show(e.Exception.ToString(), "Error",
          MessageBoxButton.OK, MessageBoxImage.Error)));
        e.Handled = true;
        InformedWorkerDataService.Common.Shared.RecordMessage(e.Exception.ToString(), true);
    }
    finally { }
}