Pet Lab [extended] #
๐ In this lab, you will learn about object oriented programming. You will create a pet simulator game.
[0] Setup #
๐ป Start by going into your
unit02_games folder.
cd ~/desktop/making_with_code/unit02_games
๐ป Then, clone your starter code.
Be sure to change yourGithubUsername to your actual Github username.
git clone https://github.com/the-isf-academy/lab_pet_extended_yourGithubUsername
๐ป cd into the lab
cd lab_pet_extended_yourGithubUsername
poetry shell
poetry install
This lab includes the following files:
pet.pytest_pet.pygame_interface.pyhelpers.py
[1] Testing your Pet #
๐ป Open the folder in VSCode
code .
First, we will focus on pet.py and test_pet.py
pet.pyis the definition of a Python class forPettest_pet.pyis simple file just for testing ourPet
๐ป Let’s start by running test_pet.py
Do not copy
$, simply type the command after the$. It is to distinguish between Terminal commands v. Terminal output.
$ python test_pet.py
Peanut
Test all Attributes and Methods #
The Pet has the following attributes:
namebored
and the following methods:
introduce()play()
๐ป Test each of the attributes and methods in test_pet.py. When you run python test_pet.py it should look something like this:
$ python test_pet.py
Peanut
๐ Hi, I am Peanut!
Wooooo, running!
True
[2] What type of animal is your pet? #
Now that you’ve used the Pet class, let’s delve into the code and make our Pet more complex. People can have all different types of pets, so lets’ add a species attribute to our Pet.
๐พ ๐ฌThis section of the lab walks you through how to write a
classin Python. Keep a look out for the ๐ปto ensure you add all the necessary features.
๐ Go to pet.py in VSCode
What’s a class? #
In the test_pet.py, you just successfully used an instance of a class!
If you look in the Pet class, you can see on line 1 - that we name the class Pet. A class a simply a blueprint for group of data, or information, with specific functionalities. In this class we are creating a blueprint for storing information about pets.
| |
Adding a new attribute #
The information associated with a Pet is defined on lines 5-7. Information associated with a class is called attribute and is stored in a variable. Our pet has three attributes. Attributes are variables that only belong to a specific class.
| |
The
Petcurrently two attributes,nameandbored.
๐ป Add a species attribute to the Pet class that is initially set to None. It will work just like the name attribute.
Adding a new method #
Now that we’ve added the species attribute, we need to add a method to set the attribute.
If you scroll down to lines 9-12, we see an example of a method. A method is similar to a function. The only difference is that a method belongs to a certain class, like Pet.
| |
The
set_name()method changes thenameattribute to whatever the user put as the parameter.e.g.
my_pet.set_name('Bob')
Just like the name attribute, we need to be able to set the species of our pet.
๐ป Add a new method called set_species(). This method should change the species attribute of the Pet class.
Testing your changes #
Let’s see if the species attribute and set_species() method is working by jumping back into test_pet.py.
๐ป Test your changes by using set_species() on pet1
$ python test_pet.py
Peanut
๐ Hi, I am Peanut!
Wooooo, running!
True
Dog
Using the species attribute #
Right now, the introduce() method just has the pet say their name. Let’s make it more detailed by including their species in the introduction.
| |
๐ป Edit the introduce() method so that your pet will also tell you its species.
๐ป Test the updated introduce() method in test_pet.py.
$ python test_pet.py
Peanut
๐ Hi, I am Peanut and I am a dog!
Wooooo, running!
True
Dog
Add tired feature #
๐ค Pet’s get tired, just like humans!
๐ป Add the ability to track if the Pet is tired. If it’s tired, it should take a nap().
- What attribute will you add?
- What method will you add?
๐ป Edit test_pet.py to ensure its working properly
[3] Pet Simulator #
๐พ Now that you have experienced the backend of the Pet, let’s make it into a game! Your Pet will have a nice Terminal interface where you can interact with it through a menu system, just like a lo-fi text-based video game.
โ๏ธ Draw a flow chart of how the game will work. The user should be able to access all of the features of the Pet() through a user friendly interface.
๐ป Implement your flow chart in game_interface.py so you can interact with your Pet! The finished version should look something like this:
python game_interface.py
-----------------------------------
---- Welcome to Pet Simulator ----
-----------------------------------
What would you like to name your pet?
> Peanut
-------------------------
Peanut pet is ready!
-------------------------
> Introduce
Play
Nap
Quit
[4] Deliverables #
โกโจOnce you’ve successfully completed the game be sure to fill out this Google form.
๐ป Push your work to Github:
- git status
- git add -A
- git status
- git commit -m “describe your changes”
be sure to customize this message, do not copy and paste this line
- git push
[5] Extension #
๐พ ๐ฌIf you have your own ideas, build on the
Pethowever you would like!But if you’re unsure where to start, there are some ideas below.
Add a hunger level #
At this point, you have a working Pet, but it’s pretty basic. Most pets, get hungry and need to eat.
This will require you to add a hunger attribute and eat() method.
Your hunger attribute should:
- be an numerical data type
- decreased when it plays
- increase when it uses
eat()
๐ป Test your edits with the test_pet.py or python shell.
๐ป Edit game_interface.py to include the new hunger features of the Pet
๐ป Play test it: python game_interface.py
Add a status check #
๐ป Add a feature to check the status of the Pet. You should be able to see the:
- bored, tired, and hunger levels
You will need to write a new method in the Pet() class.
Tamagotchi Features #
This lab was inspired by the Tamagotchi!

๐ป Include as many of the original Tamagotchi features as you can: Tamagotchi wiki.
For example:
- happiness
- sickness
- life cycle (baby, child, teen, adult)
Customize the look of your game #
๐ป Experiment with the Colorama Library to implement colors into your Terminal interface
Add multiple pets #
๐ป Allow the user to have multiple pets! The user should be able to customize and interact with all of their pets.