Virtual Pet Simulator Python Script

A simple virtual pet game where you can feed, play with, and put your pet to sleep—keep it happy and healthy or watch things go south.

Click to view script…
import time

class VirtualPet:
    def __init__(self, name):
        self.name = name
        self.hunger = 5      # 0 means full, 10 means starving
        self.happiness = 5   # 0 means sad, 10 means happy
        self.energy = 5      # 0 means tired, 10 means full of energy
        self.alive = True

    def feed(self):
        if self.hunger > 0:
            self.hunger -= 1
            print(f"You fed {self.name}. Hunger is now {self.hunger}/10.")
        else:
            print(f"{self.name} isn't hungry right now!")

    def play(self):
        if self.energy > 0:
            self.happiness = min(10, self.happiness + 1)
            self.energy -= 1
            self.hunger = min(10, self.hunger + 1)
            print(f"You played with {self.name}. Happiness: {self.happiness}/10, Energy: {self.energy}/10, Hunger: {self.hunger}/10.")
        else:
            print(f"{self.name} is too tired to play.")

    def sleep(self):
        print(f"{self.name} is sleeping...")
        time.sleep(2)
        self.energy = min(10, self.energy + 3)
        self.hunger = min(10, self.hunger + 1)
        print(f"{self.name} woke up. Energy: {self.energy}/10, Hunger: {self.hunger}/10.")

    def status(self):
        print(f"\n{self.name}'s Status:")
        print(f"  Hunger: {self.hunger}/10")
        print(f"  Happiness: {self.happiness}/10")
        print(f"  Energy: {self.energy}/10")

    def update(self):
        # Stats change over time
        self.hunger = min(10, self.hunger + 1)
        self.happiness = max(0, self.happiness - 1)
        self.energy = max(0, self.energy - 1)
        if self.hunger == 10 or self.happiness == 0 or self.energy == 0:
            self.alive = False
            print(f"\nSadly, {self.name} has passed away due to neglect...")

def main():
    pet_name = input("Name your pet: ")
    pet = VirtualPet(pet_name)
    print(f"Welcome, {pet.name}!")
    
    while pet.alive:
        pet.status()
        print("\nActions: feed | play | sleep | exit")
        action = input("What do you want to do? ").strip().lower()
        
        if action == "feed":
            pet.feed()
        elif action == "play":
            pet.play()
        elif action == "sleep":
            pet.sleep()
        elif action == "exit":
            print("Goodbye!")
            break
        else:
            print("Invalid action. Try again.")
        
        pet.update()
    
    print("Game over.")

if __name__ == '__main__':
    main()