Can anyone give an example of how to make a virtual object such as these
Thank You.
Expand|Select|Wrap|Line Numbers
- namespace critter_menu
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool keepgoing = true;
- int choice;
- critter mycritter=new critter();
- mycritter.name = "George";
- while (keepgoing)
- {
- mycritter.age();
- choice = showmenu();
- switch (choice)
- {
- case 0:
- keepgoing = false;
- break;
- case 1:
- mycritter.talk();
- break;
- case 2:
- mycritter.eat();
- Console.WriteLine("You have fed the critter");
- break;
- case 3:
- mycritter.play();
- Console.WriteLine("You have played with the critter");
- break;
- case 4:
- Console.WriteLine("Current name: {0}",mycritter.name);
- Console.WriteLine("Change name to: ");
- mycritter.name = Console.ReadLine();
- break;
- default:
- Console.WriteLine("That was not a valid input");
- break;
- }
- }
- }
- static int showmenu()
- {
- int input = 1;
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("0) Exit");
- Console.WriteLine("1) Listen to Critter");
- Console.WriteLine("2) Feed Critter");
- Console.WriteLine("3) Play with Critter");
- Console.WriteLine("4) Rename Critter");
- try
- {
- input = Convert.ToInt32(Console.ReadLine());
- }
- catch (FormatException)
- {
- Console.WriteLine("Incorrect Input");
- input = 1;
- }
- return input;
- }
- }
- class critter
- {
- int phappy = 10;
- int pfull = 10;
- int page = 0;
- string pName;
- public string name
- {
- get
- {
- return pName;
- }
- set
- {
- if (value.Length > 8)
- {
- value = value.Substring(0, 8);
- pName = value;
- Console.WriteLine("The name can't be longer than 8 chacters");
- Console.WriteLine("The critter name has been set to:{0}", pName);
- }
- else
- pName = value;
- }
- }
- public void talk()
- {
- Console.WriteLine("The critter says \"My name is {0}\"", pName);
- {
- if (pfull < 1)
- Console.WriteLine("Feed me or i'll die");
- else if (pfull < 4)
- Console.WriteLine("I'm kinda getting hungry");
- else
- Console.WriteLine("I'm not hungry right now");
- }
- if (phappy < 5)
- Console.WriteLine("Play with me.");
- }
- public void age()
- {
- page++;
- pfull--;
- phappy--;
- //if hungry gets un happy faster
- if (pfull < 3)
- phappy--;
- }
- public void eat()
- {
- pfull += 4;
- }
- public void play()
- {
- phappy += 3;
- }
- }
- }