Welcome to the Virtual Pet Game!

 

Introduction

In this virtual pet game, you will take care of a pet by feeding it, playing with it, and monitoring its health. This project demonstrates the use of basic programming concepts like classes, loops, and conditionals in Python.

The objective of this game is to create an interactive virtual pet, where the player can manage the pet’s hunger, happiness, and health. The player will interact with the pet through a simple console interface.

Program Code


# Virtual Pet Game in Python

class Pet:
    def __init__(self, name):
        self.name = name
        self.hunger = 5
        self.happiness = 5
        self.health = 5

    def feed(self):
        if self.hunger < 10:
            self.hunger += 1
            print(f"{self.name} is eating... Hunger level: {self.hunger}")
        else:
            print(f"{self.name} is full!")

    def play(self):
        if self.happiness < 10:
            self.happiness += 1
            print(f"{self.name} is playing... Happiness level: {self.happiness}")
        else:
            print(f"{self.name} is already very happy!")

    def check_health(self):
        print(f"{self.name}'s health: {self.health}")
        if self.hunger == 0 or self.happiness == 0:
            self.health -= 1
            print(f"{self.name}'s health has decreased! Current health: {self.health}")
        elif self.hunger == 10 or self.happiness == 10:
            self.health += 1
            print(f"{self.name}'s health has increased! Current health: {self.health}")

    def status(self):
        print(f"\n{self.name}'s Current Status:")
        print(f"Hunger: {self.hunger}")
        print(f"Happiness: {self.happiness}")
        print(f"Health: {self.health}")
        
# Main function to play the game
def main():
    pet_name = input("What would you like to name your pet? ")
    pet = Pet(pet_name)
    
    while True:
        print("\n1. Feed your pet")
        print("2. Play with your pet")
        print("3. Check your pet's health")
        print("4. Check pet's status")
        print("5. Exit the game")
        
        choice = input("Enter your choice: ")
        
        if choice == '1':
            pet.feed()
        elif choice == '2':
            pet.play()
        elif choice == '3':
            pet.check_health()
        elif choice == '4':
            pet.status()
        elif choice == '5':
            print("Goodbye!")
            break
        else:
            print("Invalid choice, please try again.")

if __name__ == "__main__":
    main()
            

Program Explanation and Structure

This Python program uses object-oriented programming (OOP) to simulate a virtual pet. Here’s a breakdown of the key components:

  • Pet Class: The Pet class is used to define the virtual pet’s properties such as hunger, happiness, and health. It includes methods to feed, play with, and check the pet’s health and status.
  • Main Function: The main() function is where the game runs. It takes user input to interact with the pet and offers the player different actions like feeding or playing with the pet.
  • Game Loop: The game runs in a loop, continuously prompting the user for actions until they choose to exit the game.

How to Run the Program

To run the program:

  1. Install Python from the official Python website.
  2. Save the code above in a file with a .py extension (e.g., virtual_pet.py).
  3. Open a terminal or command prompt and navigate to the folder where your file is saved.
  4. Run the program by typing: python virtual_pet.py
  5. Follow the on-screen prompts to interact with your virtual pet!
© 2025 Learn Programming. All rights reserved.

 

One Reply to “Welcome to the Virtual Pet Game!”

Leave a Reply

Your email address will not be published. Required fields are marked *