Let’s Start Python Programming for Beginners to Advanced. OOP Project β Pet Simulator Game, Lectures 18
π Lecture 18: OOP Project β Pet Simulator Game
π― Goal
By the end of this lecture, you will:
- Understand how to apply Object-Oriented Programming (OOP) in a real-world project.
- Be able to create and manage multiple objects from a class.
- Build a fun Pet Simulator Game where you can feed, play with, and track your pet’s happiness and hunger.
- Learn how to organize code using classes and methods.
π§ What Are We Building?
Weβre going to build a Pet Simulator Game, where:
- You can create a virtual pet.
- Feed it, play with it, and keep it happy.
- Track its mood over time.
This project will help you practice:
- Creating classes
- Defining methods
- Using attributes
- Managing object states
π οΈ Step-by-Step Plan
Step 1: Define the Pet
Class
Weβll create a Pet
class with attributes like name, hunger, and happiness. It will also have methods like feed()
and play()
.
class Pet:
def __init__(self, name):
self.name = name
self.hunger = 50 # 0 = full, 100 = hungry
self.happiness = 50 # 0 = sad, 100 = happy
def feed(self):
if self.hunger > 10:
self.hunger -= 20
print(f"{self.name} is eating. Hunger decreased.")
else:
print(f"{self.name} is already full!")
def play(self):
if self.happiness < 90:
self.happiness += 10
print(f"{self.name} is playing. Happiness increased.")
else:
print(f"{self.name} is already super happy!")
def status(self):
print(f"\n{self.name}'s Status:")
print(f"Hunger: {self.hunger}")
print(f"Happiness: {self.happiness}")
Step 2: Create Your Pet Object
Now letβs create an instance of the Pet
class:
my_pet = Pet("Buddy")
Step 3: Interact with Your Pet
Use the methods to interact with your pet:
my_pet.status()
my_pet.feed()
my_pet.play()
my_pet.status()
Step 4: Make It Interactive!
Letβs make a simple menu so users can choose what to do:
print("Welcome to the Pet Simulator!")
pet_name = input("What is your pet's name? ")
my_pet = Pet(pet_name)
while True:
print("\nWhat would you like to do?")
print("1. Feed")
print("2. Play")
print("3. Check Status")
print("4. Quit")
choice = input("Enter your choice (1β4): ")
if choice == "1":
my_pet.feed()
elif choice == "2":
my_pet.play()
elif choice == "3":
my_pet.status()
elif choice == "4":
print("Goodbye! Take care of your pet!")
break
else:
print("Invalid choice. Please try again.")

π§ͺ Try It Yourself!
Try adding more features to your game:
- Add a method for sleeping or taking a nap.
- Add a timer that increases hunger and decreases happiness over time.
- Let users adopt more than one pet!
π‘ Sample Code for Sleeping:
def sleep(self):
if self.hunger < 90:
self.hunger += 10
print(f"{self.name} took a nap. Hunger increased a little.")
else:
print(f"{self.name} is too hungry to sleep well.")
π Challenge (Optional)
Add a mood checker that prints a message based on the pet’s current state:
def check_mood(self):
if self.happiness >= 70 and self.hunger <= 30:
print(f"{self.name} is feeling great!")
elif self.happiness >= 50 or self.hunger <= 50:
print(f"{self.name} is okay today.")
else:
print(f"{self.name} is unhappy. Try playing or feeding them.")
Call it after each action:
my_pet.check_mood()
π§ Kids Corner
π§ Imagine you’re caring for a robot pet β just like a real animal! You have to feed it, play with it, and make sure it stays happy.
π€ Robot says:
Beep boop! I'm hungry... and sad π’
Can we play?
π Summary
- We built a Pet Simulator Game using Object-Oriented Programming.
- Created a
Pet
class with attributes and methods. - Learned how to simulate pet behavior using object state changes.
- Practiced user interaction and menu-based programs.
Stay Updated
If you found this information useful, donβt forget to bookmark this page and Share and leave your feedback in the comment section below.
Python! Inheritance | Lectures 17