Use The Class Build Pet Development Game
Use The Class Build Pet Development Game
In this project, will write a Python program to implement a virtual pet. You can decide the species, name, and gender of your pet. You can also interact with your pet by feeding your pet, playing with your pet, bathing your pet, etc. Will design a user-defined Class called ‘Pet’ to achieve that.
I think a lot of people have played this kind of game like cultivation. It may be a web version, a game console or even a mobile version. So this is my goal, just the initial implementation, without too much optimization and modification.
Refer to the Tk part of the python basic manual to program and desktop the program, but for personal reasons will temporarily learn the C++ language. If you are willing to participate or just have this idea and energy, please contact me. I am very happy to work with you.
If you want to develop Android programs you can use Qpython, which is a new supplement package for python.
Perhaps there are still undiscovered problems and errors in the program. If you are lucky enough to be discovered by you, please give me feedback and correct the error. I am very happy to accept.
A pet has the following basic attributes:
As well as some other attributes that are related to physical or mental health status:
These attributes are measured in the range from 0 to 10. For the ‘energy’ attribute, the ideal value is 10. For the other attributes (‘hunger’, ‘thirst’, ‘loneliness’, and ‘smell’), the ideal value is 0. When a pet object is created, his or her initial status is randomized within an acceptable range. For example, a newly created pet may have a hunger/thirst/loneliness/smell level in the range of (0, 5), and have an energy level in the range of (5, 10).
Therefore, in your pet class, you will have attributes related to each feature that we mentioned. Make them as private attributes (i.e., the attribute name should start with a single underscore)
Learning Objectives
The goal of this game is to take care of your pet by keeping your pet in a good status (i.e., lower hunger / thirst / loneliness / smell level, and higher energy level). You can take care of your pet by taking each of the following actions:
The health status of your pet will be updated each time you take a certain action:
After you take an action, your pet will give you feedback about the specific action you took. Next, your pet will report his/her status if some attributes are too low or too high. For example, after you play with your pet, your pet will first say ‘Thanks for your company’. Next, if the hunger level of your pet is lower than a threshold (say 5), your pet will say ‘I am hungry’. Please note that multiple attributes may need to be reported after an action. If your pet is both hungry and thirsty after playing with you, he/she will say two sentences: ‘I am hungry,’ and then ‘I am thirsty’.
The game begins by prompting you to entering the species, name, gender, and fur color of your pet, each feature separated by a space. Once those inputs are validated, a pet object will be created. Then the program will prompt you to enter a command. Your command will either lead to an action taken or lead to a table that shows you the health status of your pet. The program will repeatedly prompt you to input a command, until you enter ‘q’ and then you will quit this game.
Project Specifications
Design a class called Pet which initialize the following attributes in its __ init__() function:
To fill in appropriate values for initialization:
After initialize the attributes mentioned above, there is a call to the reply_to_master method.
def get_hunger_level(self): simply return the value of hunger.
def get_thirst_level(self): similar to previous.
def get_energy_level(self): similar to previous.
def drink(self, liquid): this method represents the action of ‘feeding water’.
this method takes an input parameter called liquid, which is an instance that belongs to a certain class type. This class type should be an element in the drinkable_items list. (For example, if liquid = 1 (or any other integer), then int must be an element in the drinkable_items list.) Otherwise, your program will output an error message. (use isinstance() function).
Call the time_pass_by method with the default time (which is complete and does not need modification).
If the liquid is valid, you will decrease the thirsty value of your pet, based on the quantity of this liquid. You must make sure that your pet’s thirst level will not go below zero. For example, if the thirst value is 3, and the quantity of the liquid is 2, then after you call the drink function, thirst will be 1. If the quantity of the liquid is 4, then thirst will become 0, not -1. If all the liquid is not drunk, print “Too much drink to finish. I will leave some for you.”
A pet will not drink if their thirst is less than 2. If they will not drink, print a message, e.g. “Your pet is satisfied, no desire for sustenance now.” if they are not hungry.
If the liquid is invalid, print “Not drinkable”
After you update the thirst value, call the reply_to_master() method.
def feed(self, food):
def shower(self):
def sleep(self):
def play_with(self):
def show_status(self):
The number of ‘#’ is proportional to the attribute value, and it will occupy 20 spaces in total. A partially formatted string is in the provided file.
After complete the Pet class, design two subclasses of Pet:
Both of the above two class definitions should be less than 6 lines of code.
Prompt your user to enter the species, name, gender, fur color of the pet, separated by spaces. If the user hits the Enter key, the program will assume that your user wants to use default values (species=‘dog’,gender=‘male’,color=‘white’). A valid pet has species that is ‘dog’ or ‘cat’ and gender that is ‘male’ or ‘female’. Otherwise, if the user inputs invalid values (say, ‘pig fluffy male white’), the program will repeat this prompt until the user input is valid. An example is given below:
Once the pet input is valid, your program will create an instance of the Dog (or Cat) class, and repeatedly prompt you to input a valid command to interact with your Dog (or Cat) instance, until you enter a ‘q’. You have six commands: [feed] or [drink] or [shower] or [sleep] or [play] or [status]. The first five commands are five actions you can take with your pet, and the [status] command will show a table of your pet’s health status. A running example of a complete program is shown below.
This is based on a simple model of the pet’s game architecture. The overall program is not complicated, but I use a class approach to make the program more readable and organized.
TianTian Created on the eve of Christmas in 2018
Thanks for reading this help document