School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,459 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,626 people online right now. Registration is fast and FREE... Join Now!




Pet game programming

 

Pet game programming, I need a little help with feeding my pets in programming

MrKarazy

13 Jun, 2009 - 06:25 PM
Post #1

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 11


My Contributions
Hey guys, got another problem i need help with.

Im making a game that is dealing with pets, ive gotten all my other functions working somewhat.
Now i need to get the eat function working.

here's a little code, theres a lot!

Critter
CODE

package {

    public class Critter {

        protected var hunger:int=0;
        protected var entertained:int=100;
        protected var energy:int=0;
        protected var vegetarian:Boolean;
        protected var sleepSchedule:int;
        protected var time:int=0;
        protected var food:Food;

        //constructor
        public function Critter(h:int, e:int, s:int, c:int, v:Boolean = false) {
            this.hunger=h;
            this.entertained=e;
            this.energy=s;
            this.sleepSchedule=c;
            this.vegetarian=v;
        }

        public function eat(f:Food):void {
            if (vegetarian==false&&f.getMeat()==false) {
                hunger=hunger+10;
                energy=energy-5;
                time=time+10;
            } else if (vegetarian==false && f.getMeat()==true) {
                hunger=hunger-10;
                energy=energy+10;
                time=time+10;
            } else if (vegetarian==true && f.getMeat()==false) {
                hunger=hunger-10;
                energy=energy+10;
                time=time+10;
            } else if (vegetarian==true && f.getMeat()==true) {
                hunger=hunger-10;
                energy=energy-5;
                time=time+10;
            }
        }

        public function sleep():void {
            energy=energy+10;
            hunger=hunger+5;
            entertained=entertained-5;
            time=time+10;
        }

        public function play():void {
            entertained=entertained+10;
            hunger=hunger+5;
            energy=energy-5;
            time=time+10;
        }

        public function passTime():void {
            time=time+10;
            hunger=hunger+5;
            energy=energy-5;
            entertained=entertained-5;
        }

        //getters
        public function getHunger():int {
            return this.hunger;
        }
        public function getEntertained():int {
            return this.entertained;
        }
        public function getEnergy():int {
            return this.energy;
        }
        public function getVegetarian():Boolean {
            return this.vegetarian;
        }
        public function getSleepschedule():int {
            return this.hunger;
        }
        public function getTime():int {
            return this.time;
        }
        


    }


}


Iguana
CODE

package {

    public class Iguana extends Critter {

        //constructor
        public function Iguana() {
            super(50,50,50,2,true);
            this.vegetarian=true;
        }

        /*override public function eat(f):void {
            entertained=entertained+10;
            hunger=hunger-10;
            energy=energy+10;
            time=time+10;
        }*/


        override public function play():void {
            energy=energy-5;
            hunger=hunger+5;
            entertained=entertained+10;
            time=time+10;
        }

        override public function sleep():void {
            energy=energy+10;
            hunger=hunger+5;
            entertained=entertained+5;
            time=time+10;
        }


    }


}


Tarantula
CODE

package {

    public class Tarantula extends Critter {

        //constructor
        public function Tarantula() {
            super(50,50,50,2,false);
            this.vegetarian=false;
        }

        /*override public function eat(f):void {
            entertained=entertained+10;
            hunger=hunger-10;
            energy=energy+10;
            time=time+10;
        }*/

        override public function play():void {
            entertained=entertained+5;
            hunger=hunger+5;
            energy=energy-5;
            time=time+10;
        }

        override public function sleep():void {
            energy=energy+10;
            hunger=hunger+5;
            entertained=entertained+5;
            time=time+10;
        }


    }


}


Food
CODE

package {

    public class Food {

        protected var isMeat:Boolean;
        protected var name:String;
        protected var satisfaction:int;

        //constructor
        public function Food(c:int,m:Boolean,n:String) {
            this.satisfaction=c;
            this.isMeat=m;
            this.name=n;
        }

        public function toString() {
            if (isMeat==false) {
                name="Lettuce";
            } else {
                name="Beef";
            }
        }


        //getters
        public function getMeat():Boolean {
            return this.isMeat;
        }
        public function getName():String {
            return this.name;
        }


    }


}


CritterCareTaker
CODE

package {

    import flash.events.*;
    import flash.display.*;
    import flash.ui.*;

    public class CritterCaretaker extends MovieClip {

        var input:String="";
        var pet:Critter;
        var f:Food;

        public function CritterCaretaker() {
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,readKey);
            this.startMenu();
        }

        public function petChosen():void {
            this.stage.removeEventListener(KeyboardEvent.KEY_DOWN,readKey);
            input="";
            this.actionMenu();

        }

        public function startMenu():void {
            trace("---------------------");
            trace("Please Choose a pet.");
            trace("1. Dog.");
            trace("2. Cat.");
            trace("3. Iguana.");
            trace("4. Tarantula.");
            trace("---------------------");
        }

        public function actionMenu():void {
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,readKeyd);
            trace("---------------------");
            trace("Enter a number for a desired action.");
            trace("1. Feed pet.");
            trace("2. Make pet sleep.");
            trace("3. Play with pet.");
            trace("4. Pass some time.");
            trace("---------------------");
        }

        public function printStats():void {
            trace("Your pet's hunger is " + pet.getHunger());
            trace("Your pet's entertainment is " + pet.getEntertained());
            trace("Your pet's energy is " + pet.getEnergy());
            trace("Current Time is "+pet.getTime());


        }

        public function readKey(e:KeyboardEvent):void {
            if (e.keyCode==13) {
                var inputNum=parseInt(input);
                switch (inputNum) {
                    case 1 :
                        trace("You have chosen a Dog.");
                        pet = new Dog();
                        break;
                    case 2 :
                        trace("You have chosen a Cat.");
                        pet = new Cat();
                        break;
                    case 3 :
                        trace("You have chosen a Iguana.");
                        pet = new Iguana();
                        break;
                    case 4 :
                        trace("You have chosen a Tarantula.");
                        pet = new Tarantula();
                        break;
                    default :
                        trace("Please enter an option 1-4!");
                        break;
                }
                petChosen();
            } else {
                input+=String.fromCharCode(e.charCode);
            }
        }
        public function readKeyd(e:KeyboardEvent):void {
            if (e.keyCode==13) {
                var inputNum=parseInt(input);
                switch (inputNum) {
                    case 1 :
                        trace("You pet is eating.");
                        pet.eat(f);
                        break;
                    case 2 :
                        trace("Your pet is sleeping.");
                        pet.sleep();
                        break;
                    case 3 :
                        trace("You're playing with your pet.");
                        pet.play();
                        break;
                    case 4 :
                        trace("An hour has passed.");
                        pet.passTime();
                        break;
                    default :
                        trace("Please enter an option 1-4!");
                        break;
                }
                petChosen();
                printStats();
            } else {
                input+=String.fromCharCode(e.charCode);
            }
        }
    }
}


User is offlineProfile CardPM
+Quote Post


SoLi

RE: Pet Game Programming

14 Jun, 2009 - 09:43 AM
Post #2

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
Do you mean you are encountering a bug when choosing the 'Eat' menu option from the CritterCareTaker class?
I notice that you don't actually instantiate a new food object in this class, but just define the variable:

CODE

var f:Food;


even though you later pass this variable (which value remains undefined) to the Pet.eat() method:

CODE

case 1 :
                        trace("You pet is eating.");
                        pet.eat(f);
                        break;


Or do you mean you need guidance on how to extend the program to include a Menu class which would offer a similar keypad interface of different food items - which would presumably need to be invoked before pet.eat(f) is called.
User is offlineProfile CardPM
+Quote Post

MrKarazy

RE: Pet Game Programming

14 Jun, 2009 - 11:57 AM
Post #3

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 11


My Contributions
Ya when i choose the eat function in my menu it does give me an error.

Im not sure how to make pass Things from Food around to the other files.

So when i fun the game, i can choose all of the options and everyone works except for eat().


Thanks for helping!

This post has been edited by MrKarazy: 14 Jun, 2009 - 11:58 AM
User is offlineProfile CardPM
+Quote Post

SoLi

RE: Pet Game Programming

14 Jun, 2009 - 12:25 PM
Post #4

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
If you only want one type of food for your pets to eat, change this line at the top of CritterCareTaker:

CODE

   var f:Food;


to

CODE

var f:Food = new Food(10, true, 'Bacon');


That will fix the error for now.

However given that some of your pets are vegetarian, they are going to become increasingly hungry no matter how much bacon you send their way, as per the Critter eat method. So you should probably write a Menu class that can instantiate a number of different food items, some of which are vegetables.

This post has been edited by SoLi: 14 Jun, 2009 - 12:27 PM
User is offlineProfile CardPM
+Quote Post

MrKarazy

RE: Pet Game Programming

14 Jun, 2009 - 03:17 PM
Post #5

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 11


My Contributions
Thx that worked. is there a way to make the attributes off that food item random everytime i run the eat function?


User is offlineProfile CardPM
+Quote Post

SoLi

RE: Pet Game Programming

14 Jun, 2009 - 03:50 PM
Post #6

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
sure,

add this static function in your Food class:

CODE


  public class Food {

   /** BEGIN NEW CODE **/
    public static function getRandom():Food {
      var foods:Array = [{ name: "Bacon",    isMeat: true },
                                  { name: "Sausage", isMeat: true },
                                  { name: "Carrot",    isMeat: false  },
                                  { name: "Lettuce",   isMeat: false  }];

      var r:int = Math.floor(Math.random()*(foods.length));
      return new Food((r+1)*10, foods[r].isMeat, foods[r].name);
    };
  /** END NEW CODE**/

      protected var isMeat:Boolean;
      protected var name:String;
      protected var satisfaction:int;

       //constructor
        public function Food(c:int,m:Boolean,n:String) {

... etc


then in your CritterCareTaker, when you call eat, use this:

CODE


case 1 :
                        trace("You pet is eating.");
                        pet.eat(f = Food.getRandom());
                        break;


Let me know how it works out.
User is offlineProfile CardPM
+Quote Post

MrKarazy

RE: Pet Game Programming

16 Jun, 2009 - 10:19 AM
Post #7

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 11


My Contributions
Wow, thanks man!

Ya i did work, now im curious on how to print the string of food while the animal is eating
so it will say your dog has eaten salad, or bacon. Which ever has come up from the random.

Also, I'm implementing a telltime function, to make time not just increments of time just traced out.

Can u help me to get it working?
Right now im not quite sure where to put this function, its in the CRITTER FILE now.
Im also not sure how to call it or pass it around.

-----------------Telltime------------
CODE

public function telltime():void{
            if(time==10){
                trace("Time is 7 in the AM!");
            }else if(time==20){
                trace("Time is 8 AM.");
            }else if(time==30){
                trace("Time is 9 AM.");
                }else if(time==40){
                trace("Time is 10 AM.");
                }else if(time==50){
                trace("Time is 11 AM.");
                }else if(time==60){
                trace("It's THE CRACK OF NOON!.");
                }else if(time==70){
                trace("Time is 1 PM.");
                }else if(time==80){
                trace("Time is 2 PM.");
                }else if(time==90){
                trace("Time is 3 PM.");
                }else if(time==100){
                trace("Time is 4 PM.");
                }else if(time==110){
                trace("Time is 5 PM.");
                }else if(time==120){
                trace("Time is 6 PM.");
                }else if(time==130){
                trace("Time is 7 PM.");
                }else if(time==140){
                trace("Time is 8 PM.");
                }else if(time==150){
                trace("Time is 9 PM.");
                }else if(time==160){
                trace("Time is 10 PM.");
                }else if(time==170){
                trace("Time is 11 PM.");
                }else if(time==180){
                trace("It's 2 MINUTES TO MIDNIGHT!!.");
                }else if(time==190){
                trace("Time is 1 AM.");
                }else if(time==200){
                trace("Time is 2 AM.");
                }else if(time==210){
                trace("Time is 3 AM.");
                }else if(time==220){
                trace("Time is 4 AM.");
                }else if(time==230){
                trace("Time is 5 AM.");
                }else if(time==240){
                trace("Time is 6 AM");
                time==0;
            }
        }






Thanks again!

This post has been edited by MrKarazy: 16 Jun, 2009 - 10:59 AM
User is offlineProfile CardPM
+Quote Post

MrKarazy

RE: Pet Game Programming

17 Jun, 2009 - 01:04 PM
Post #8

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 11


My Contributions
Hey never mind i got the time thing working alright.

Thanks for all the help!

This post has been edited by MrKarazy: 17 Jun, 2009 - 01:21 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:13AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month