Making with Code

Pet Lab #

๐Ÿ‘€ In this lab, you will learn about object oriented programming. You will create the backend of a pet simulator game.

๐Ÿ“– Here is more information on how to create a class: 12.5 Constructors


[0] Setup #

๐ŸŒ Github Repo: github.com/Making-with-Code/lab_pet

๐Ÿ’ป Enter the Poetry shell and install the requirements:
poetry shell
poetry install

๐Ÿ’ป cd into the lab

cd lab_pet_yourgithubusername

๐Ÿ’ป 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
  • 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

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        # holds the pet's name
        self.bored = True       # holds the pet's bored level

[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.

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

        self.name = None        # holds the pet's name
        self.bored = True       # holds the pet's bored level

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.

 9
10
11
12
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("Hi, I'm", 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

[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


[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 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 python shell.

๐Ÿ’ป Edit interface.py to include all of the features the Pet has.

๐Ÿ’ป 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!


[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