我是一个比较新的有相当多经验的人,我正在尝试做一个基于文本的冒险,我正在做一个战斗系统,希望有不同能力的敌人。我不是每次都为不同的敌人重现战斗,而是尝试为每个敌人使用可互换的字典。我的目标是创建一个函数调用,该函数调用根据敌方在战斗中的位置而变化,而不必进入对象。我下面有一个例子,想知道是否有一种方法可以做类似的事情。你知道吗
wolf = {'ability': 'bite'}
bear = {'ability': 'claw'}
enemy = {}
def claw():
print('stuff')
def bite():
print('different stuff')
def use_ability():
enemy = wolf
enemy['ability']()
use_ability()
你可以做到:
但这并不意味着你应该这样做。你知道吗
使用面向对象编程。如果您只想使用dict和函数,那么您可能应该改为编写Javascript。你知道吗
我忍不住要做一个小程序,解释如何用面向对象的语言来做:
相关问题 更多 >
编程相关推荐