有意思,假期还有JS的作业,哥们假期好好看看书,争取自己做出来

解决方案 »

  1.   

    时间关系, 我只给你写出里面主要的一些方法,其余的参考这个,很容易懂的,哪里不明白等10.1回来继续
    function Pet(the_pet_name, the_form_number)//两个参数宠物名字和要显示其信息的表单号
    {
    this.age = 0;
    this.hunger = Math.random() * 5;  //给每个产生的宠物一个不同的饥饿,健康
    this.health = Math.random() * 3 +1 ;//和快乐的初始值
    this.happiness = Math.random() * 5;
    this.pet_name = the_pet_name;
    this.form_number = the_form_number; this.feed = feed;
    this.play = play;
    this.medicate = medicate;
    this.display = display;
    this.makeOlder = makeOlder; window.document.forms[the_form_number].pet_name.value = the_pet_name;
    this.display();
    }
    //这样就可以创建两个宠物
    //var pet1 = new Pet("barney",0);
    //var pet2 = new Pet("betty",1);
    function feed()//喂宠物的方法  如果调用了 pet1.feed()方法,首
                    //先它会产生一个0到2之间的数,然后把饥饿度减去这个数。
    {
                    var meal_quality = Math.random() * 2;
                    this.hunger = this.hunger - meal_quality;
                  if (this.hunger <0)
                            {
                                  this.hunger = 0;
                             }
                                  this.display();
    }
    function display()   //显示信息方法
    {
            var the_string = "";        if (this.health < min_health)
            {
                    the_string = this.pet_name + " IS DEAD!";
            } else {
                    the_string += "Happiness " + parseInt(this.happiness);
                    the_string += ".  Health: " + parseInt(this.health);
                    the_string += ".  Hunger: " + parseInt(this.hunger);
                    the_string += ".  Age: " + parseInt(this.age);
                    the_string += ".";
            }        window.document.forms[this.form_number].pet_status.value = the_string;
    }       
    function makeOlder()//宠物的年龄增加一岁,并让宠物更饿一点
                        //和更不高兴一点                                           
    {
    var max_hunger = 5;
    var good_happiness = 5;  if (this.health > min_health)
    {

    this.age +=1;
    this.happiness -= Math.random() * 2;//快乐值降低了一个随机数
    this.hunger += Math.random() * 2;//饥饿度增加了一个随机
    if (this.hunger > max_hunger)
    {
    this.health -= Math.random() * 2;
    this.happiness -= Math.random() * 2;
    } } this.display();
    }
    function moveTime() //设定让makeOlder()方法每三秒种就调用一次.
    {                       
    pet1.makeOlder();
    pet2.makeOlder();
    the_time_out = setTimeout("moveTime();", 3000);
    }