By Anurag Singh
Hi, Today we are going to build a virtual pet by using one of the best high-level programming languages, python.
First of all, we have to make a road map to build our virtual pet.
Now, I will give you a brief explanation of what we are going to build. We built a virtual pet in a prompted manner. In which we have 2 or 3 options to choose our desired type of pet like dog, cat or other. Then after it will ask the user to choose the desired name for his pet, after that we can some kinds of stuff to do with our pet like playing or feeding, etc. We can also add a quit function to quit our prompt. This is a brief explanation of our virtual pet, while coding if there will be any changes or any addition in anything I will explain on the go. I am going to use the latest version of python (when I am making this virtual pet it was python 3.9).
Let's start building our virtual pet.
First of all, I'll write out something called pseudocode, which is essentially what is the logical steps of building out a program. It is necessarily coded itself but we need to build pieces of the program and eventually put them all together.
These are some key points for making our virtual pet. We can do many things with our pet as we can play, we can get some toys for our pet and we can also feed our pet.
Now we are going to make the first version of our virtual pet.
In this version, we add pet name, type, age, toys, and hunger level in a dictionary. Hunger level is for feeding our pet. After making a pet dictionary we are going to our function to initialize our whole virtual game or pet
If we run this stack of code, our code will give the following output.
[
['fish', 'cat', 'dog']
]
Note:- I am using square brackets [ ] to bind the output
In this version, we are adding to choose the desired type of pet like fish, cat, and dog and we also handled the wrong input errors by using a while loop. If someone enters wrong input our prompt will ask again the user to choose from the following options.
If we run this stack of code, our code will give the following output.
[
]
In this version, we can assign a name to our pet.
If we run this stack of code, our code will give the following output.
[
Please input a type of pet from the following options:-
fish
cat
dog
Please input one of the pets:-
cat
What would you like to name your cat?
pussy
{'name': 'pussy', 'type': 'cat', 'age': 0, 'hunger': 0, 'toys': []}
]
In this version, We can add some more stuff like quitting the game and feeding our pets. We made 3 functions as well first is for quitting the game, the second is for feeding our pets, and the third is for the main game loop function.
If we run this stack of code, our code will give the following output.
[
Please input a type of pet from the following options:-
fish
cat
dog
Please input one of the pets:-
cat
What would you like to name your cat?
pussy
Here are your options:-
---------
Q: Quit the game
F: Feed pussy
]
In this version, We are going to handle our 2 functions first one is quitting the game, and the second is feeding our pet.
If we run this stack of code, our code will give the following output.
[
Please input a type of pet from the following options:-
fish
cat
dog
Please input one of the pets:-
cat
What would you like to name your cat?
pussy
Here are your options:-
---------
Q: Quit the game
F: Feed pussy
Which of these menu options would you like to use:-
f
Fed your pet, decreasing hunger by 10
Here are your options:-
---------
Q: Quit the game
F: Feed pussy
Which of these menu options would you like to use:-
q
Quitting the simulator, Thanks for playing!
]
In this version, we are going to add some functions and update some previous functions. We make a function to show some stats of our pet like its hunger level, age, etc.
If we run this stack of code, our code will give the following output.
[
Please input a type of pet from the following options:-
fish
cat
dog
Please input one of the pets:-
fish
What would you like to name your fish?
dory
Here are your options:-
---------
Q: Quit the game
F: Feed dory
Which of these menu options would you like to use:-
f
Fed your pet, decreasing hunger by 10
Your fish dory is doing great!
Your pet currently has 0 toys, Which are:-
Your pet is currently at a hunger of 10 of a max of 100.
Your pet is 1 days old.
Here are your options:-
---------
Q: Quit the game
F: Feed dory
Which of these menu options would you like to use:-
]
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now finally our virtual pet is ready to play with us. I also make some other changes to make our pet more interesting while we play with him, You can find those changes in my code below.
List of some additional changes:-
1. Adding 2 more actions like playing and getting a toy to our pet.
2. I also handle the negative value of hunger level
3. Minimum hunger level is set to 10.
4. I also do some other small changes to make it more interactive and interesting.
You can also add some more types of pets and give them various types of actions. You can also add more actions to your virtual pet and make it more interesting.
Enjoy the Virtual Pet and take care of it.
Thanks!
Submitted by Anurag Singh (adamaspaul99)
Download packets of source code on Coders Packet
Comments