Pet game programming

I need a little help with feeding my pets in programming

Page 1 of 1

7 Replies - 810 Views - Last Post: 17 June 2009 - 02:04 PM

#1 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Pet game programming

Post icon  Posted 13 June 2009 - 07:25 PM

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
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
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
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
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
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);
			}
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pet game programming

#2 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Pet game programming

Posted 14 June 2009 - 10:43 AM

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:

var f:Food;



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

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.
Was This Post Helpful? 0
  • +
  • -

#3 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Pet game programming

Posted 14 June 2009 - 12:57 PM

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 June 2009 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Pet game programming

Posted 14 June 2009 - 01:25 PM

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

   var f:Food;



to

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 June 2009 - 01:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Pet game programming

Posted 14 June 2009 - 04:17 PM

Thx that worked. is there a way to make the attributes off that food item random everytime i run the eat function?
Was This Post Helpful? 0
  • +
  • -

#6 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Pet game programming

Posted 14 June 2009 - 04:50 PM

sure,

add this static function in your Food class:


  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:


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



Let me know how it works out.
Was This Post Helpful? 1
  • +
  • -

#7 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Pet game programming

Posted 16 June 2009 - 11:19 AM

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------------
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 June 2009 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#8 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Pet game programming

Posted 17 June 2009 - 02:04 PM

Hey never mind i got the time thing working alright.

Thanks for all the help!

This post has been edited by MrKarazy: 17 June 2009 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1