Making with Code

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
๐Ÿ’ป Enter the Poetry shell and install the requirements:
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.

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

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

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.

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

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

The Pet currently two attributes, name and bored.

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

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

    self.name = name

The set_name() method changes the name 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.

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

[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