當前位置: 首頁>>代碼示例>>Python>>正文


Python Schedule.add_action方法代碼示例

本文整理匯總了Python中schedule.Schedule.add_action方法的典型用法代碼示例。如果您正苦於以下問題:Python Schedule.add_action方法的具體用法?Python Schedule.add_action怎麽用?Python Schedule.add_action使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在schedule.Schedule的用法示例。


在下文中一共展示了Schedule.add_action方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Npc

# 需要導入模塊: from schedule import Schedule [as 別名]
# 或者: from schedule.Schedule import add_action [as 別名]
class Npc(object):
    SLEEP_DURATION = 7 * 60
    DEFAULT_FOOD_CONSUMPTION = 1 #amount of food needed per unit of time (now minute...)

    def __init__(self, occupation, name="Unknown"):
        self.occupation = occupation
        self.occupation.npc = self
        self.schedule = Schedule()
        #TODO: Cannot think... just advance schedule so that it is already done when Advance is called the first time...
        self.schedule.advance(Schedule.MAX_TIME)
        self.possession = Possession()
        self.hungerLevel = 24 * 60 #enough for one day
        self.food_consumption = Npc.DEFAULT_FOOD_CONSUMPTION
        self.alive = True
        self.strategy = None
        self.city = None
        self.x = 100
        self.y = 100
        self.home_x = 0
        self.home_y = 0
        self.name = name

    def print_status(self):
        if not self.alive:
            print "Npc is DEAD!"
        print "Npc (" + str(self.occupation) + ") has " + str(self.possession.money) + " money, owns:"
        for pos in self.possession.resources:
            print pos

    #TODO: How to handle time advancing. Now advancing goes fine if the time interval is smalles possible.
    #If the interval is increased, first schedule is advanced and then food. This leads to not wanted scenarios
    #where npc can do work without food.
    def advance(self, time):
        while (time > 0):
            if not self.alive:
                return

            #Day is completed, create new schedule
            if (self.schedule.is_done()):
                self.create_schedule()
            
            timeLeft = self.schedule.advance(time) 
            self._consume_food(time - timeLeft)
            time = timeLeft
 
    def create_schedule(self):
        self.schedule = Schedule()
        if self.strategy == None:
            self._add_mandatory_actions()
            self.occupation.add_default_schedule(self, self.possession)
        else:
            self._add_mandatory_actions()
            self.strategy.create_schedule()

    def _consume_food(self, time):
        while (time > 0):
            if (self.hungerLevel <= 0):
                foods = self.possession.get_foods()
                if len(foods) == 0:
                    self.alive = False
                    print "NPC (" + str(self.occupation) + ") died from hunger!"
                    return
                #just eat the first thing from the inventory...
                self.possession.destroy_resource(foods[0])
                self.hungerLevel += foods[0].NUTRITIONAL_VALUE
            consumedAmount = min(time, self.hungerLevel)
            self.hungerLevel -= consumedAmount * self.food_consumption            
            time -= consumedAmount

    def _add_mandatory_actions(self):
        self.schedule.add_action(Action(self, "Sleep", Npc.SLEEP_DURATION))
開發者ID:timoruokonen,項目名稱:latrappe,代碼行數:73,代碼來源:npc.py


注:本文中的schedule.Schedule.add_action方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。