重写这样的生存方法
当一个动物真的go to heaven
时,一个Food
对象的名字
“肉”和{Food
物体
以“熊肉”为名的产品将被生产出来。
要将对象添加到某个位置,可以使用该位置的add object方法
对象。在
预定义代码:
class LivingThing(MobileObject):
def __init__(self, name, health, threshold):
super().__init__(name, None)
self.health = health
self.threshold = threshold
def get_threshold(self):
return self.threshold
def get_health(self):
return self.health
def add_health(self, health):
self.health = min(100, self.health+health)
def reduce_health(self, health):
self.health = max(0, self.health-health)
if self.health == 0:
self.go_to_heaven()
def go_to_heaven(self):
self.get_place().del_object(self)
HEAVEN.add_object(self)
GAME_LOGGER.add_event("DEAD", self)
class Food(Thing):
def __init__(self, name, food_value):
self.name = name
self.food_value = food_value
def get_food_value(self):
return self.food_value
我的代码是:
^{pr2}$为了测试我的代码:
BASE.add_object(bear)
print(named_col(BASE.get_objects())) # ['bear']
print(bear.get_place().get_name()) # Base
bear.go_to_heaven() # bear went to heaven!
print(named_col(BASE.get_objects())) #['bear meat']
最后一部分我没有得到“熊肉”,我得到了“熊”。我的代码怎么了?在
尝试更换
有:
^{pr2}$你想把肉放在死亡的地方,而不是动物的鬼魂。在
因为这似乎是家庭作业,我不想给你一个完整的答案,但是
你把物体放回原来的位置。相反,你想放点别的东西
^{pr2}$相关问题 更多 >
编程相关推荐