Making with Code

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 YOUR-GITHUB-USERNAME to your actual Github username.

git clone https://github.com/the-isf-academy/lab_pet_extended_YOUR-GITHUB-USERNAME
cd lab_pet_extended_YOUR-GITHUB-USERNAME
๐Ÿ’ป Enter the Poetry shell and install the requirements:
poetry shell
poetry install

This lab includes the following files:

  • pet.py
  • test_pet.py
  • game_interface.py
  • helpers.py

[1] Testing your Pet #

๐Ÿ’ป Open the folder in VSCode

code .

First, we will focus on pet.py and test_pet.py

  • pet.py is the definition of a Python class for Pet
  • test_pet.py is simple file just for testing our Pet

๐Ÿ’ป 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 Properties and Methods #

The Pet has the following properties:

  • name
  • bored

and the following methods:

  • introduce()
  • play()

๐Ÿ’ป Test each of the properties 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 property 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.

1
2
3
4
5
6
class Pet:
    def __init__(self):
        '''This initializes the pet with its properties.'''

        self.name = None            # stores the pet's name as a string
        self.bored = False          # stores if the pet is bored

Adding a new property #

The information associated with a Pet is defined on lines 5-7. Information associated with a class is called property and is stored in a variable. Our pet has three properties. Properties are variables that only belong to a specific class.

3
4
5
6
7
8
class Pet:
    def __init__(self):
        '''This initializes the pet with its properties.'''

        self.name = None            # stores the pet's name as a string
        self.bored = False          # stores if the pet is bored

The Pet currently two properties, name and bored.

๐Ÿ’ป Add a species property to the Pet class that is initially set to None. It will work just like the name property.


Adding a new method #

Now that we’ve added the species property, we need to add a method to set the property.

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.

10
11
12
13
def set_name(self, name):
    '''This method sets the name property'''

    self.name = name

The set_name() method changes the name property to whatever the user put as the parameter.

e.g. my_pet.set_name('Bob')

Just like the name property, 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 property of the Pet class.


Testing your changes #

Let’s see if the species property 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 property #

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.

14
15
16
17
def introduce(self):
  '''This method introduces the pet with its name.'''

  print(f"๐Ÿ‘‹ Hi, I am {self.name}!")

๐Ÿ’ป 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 property 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 Pet however you would like!

But if you’re unsure where to start, there are 3 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 property and eat() method.

Your hunger property 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.


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