根据经过的时间更改变量

时间:2018-03-11 14:18:23

标签: c#

我正在尝试创建一个程序,用户可以在其中与虚拟宠物进行交互。这只宠物有饥饿等属性,只存储在1-10之间的整数。有没有办法根据已经过了多长时间来减少所述变量的值(因为宠物最后一次喂食,让我们说)?

希望我能用它来创建这样的函数:

    static void hungerDecrease(CPet pet)
    {
        bool needsFeeding = false;
        // some method of checking if say 4 hours has passed and if so set needsFeeding to be true
        if (needsFeeding == true)
        {
            pet.Hunger -= 1;
        }
    }

CPet类看起来像这样:

    public class CPet 
    {
        public string Name;
        public string Gender;
        // Attributes belonging to pets, with values being defined upon creation.
        public int Hunger = 5;
        public int Boredom = 5;
        public int Happiness = 5;
        public int Intelligence = 1;
        public CPet(string name, string gender)
        {
            Name = name;
            Gender = gender;
        }
    }

1 个答案:

答案 0 :(得分:0)

public class CPet 
{
    public string Name;
    public DateTime LastFed;
    ...
}

static void hungerDecrease(CPet pet)
{
    if (DateTime.Now > pet.LastFed.AddHours(4))
    {
        pet.Hunger -= 1;
        pet.LastFed = DateTime.Now;
    }
}