Pet Lab #
๐ 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 YOUR-GITHUB-USERNAME
to your actual Github username.
git clone https://github.com/the-isf-academy/lab_pet_YOUR-GITHUB-USERNAME
cd lab_pet_YOUR-GITHUB-USERNAME
poetry shell
poetry install
๐ป
cd
into the lab
cd lab_pet_YOUR-GITHUB-USERNAME
๐ป Enter the Poetry Shell and install the required packages. Each line should go one at a time.
poetry shell
poetry install
This lab includes the following files:
pet.py
game_interface.py
helpers.py
[1] Building your Pet #
๐ป
Open the folder in VSCode and open pet.py
. We will first focus on the the Pet()
Python class.
code .
classDiagram class SpellingBee { + name: str + bored: [] boolean + introduce() + play() }
The Pet
has the following attributes:
name
bored
and the following methods:
introduce()
play()
๐ป
Let’s start by running pet.py
Do not copy
$
, simply type the command after the$
. It is to distinguish between Terminal commands v. Terminal output.
$ python pet.py
-- testing Pet --
Peanut
๐ป
Test the other attributes
and methods
code at the bottom of the file in the if __name == "__main__":
section and run python pet.py
. This section is only called when file is ran, not when the file is imported.
if __name__ == "__main__":
print('-- testing Pet -- ')
pet1 = Pet() # create an instance of a Pet
pet1.set_name("Peanut") # call `set_name()` method
print(pet1.name)
๐ป
Test each of the attributes and methods in pet.py
. When you run python _pet.py
it should look something like this:
$ python test_pet.py
-- testing Pet --
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
class
in 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
Pet
currently two attributes,name
andbored
.
๐ป
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 thename
attribute 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
[3] Pet Simulator #
๐พ Now that you have experienced the backend of the Pet
, let’s play the game! The Pet
now has a nice Terminal interface where you can interact with it through a menu system, just like a lo-fi text-based video game.
python game_interface.py
-----------------------------------
---- Welcome to Pet Simulator ----
-----------------------------------
What would you like to name your pet?
> Peanut
-------------------------
Your pet is ready!
-------------------------
> Introduce
Quit
Add play() #
๐ป
Edit game_interface.py
so you can play()
with your Pet
! Start by reading through the code to make sure you understand how it works. Then make small edits to add in play()
. Be sure to look for:
- how are menu options being displayed?
- how are the different functions being ran based on the user selection?
๐ป
Play test it! python game_interface.py
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 game_interface.py
so you can tell your Pet
to nap()
!
๐ฎ Play test it! python game_interface.py
[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 drawing and your process here”
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
Pet
however you would like!But if you’re unsure where to start, there are a few 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
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
Inheritance #
๐ป
Create subclasses of your Pet
using inheritance
. For example,
what features do dogs have that cats do not have?
๐ You’ll need to learn how to incorporate inheritance: 12.6 Inheritance