Welcome to the virtual pet game tutorial! In this program, you’ll create a simple virtual pet where you can feed and take care of your pet. This game will involve interacting with your pet through basic actions like feeding, playing, and checking on its mood.

Objective:

The goal of this program is to simulate a simple pet care system where the player can:

  • Feed the pet to keep it happy.
  • Take care of its health and mood.
  • Check the pet’s current status (hunger, health, mood).

Program Code:

#include 
#include 
#include 

struct Pet {
    int hunger;  // Pet's hunger level (1-100)
    int health;  // Pet's health level (1-100)
    int mood;    // Pet's mood (1-100)
};

void feedPet(struct Pet *pet) {
    pet->hunger -= 20;  // Feeding reduces hunger
    if (pet->hunger < 0) pet->hunger = 0;
    pet->health += 10;  // Feeding increases health
    if (pet->health > 100) pet->health = 100;
    printf("You fed your pet! Hunger: %d, Health: %d\n", pet->hunger, pet->health);
}

void playWithPet(struct Pet *pet) {
    pet->mood += 15;  // Playing increases mood
    if (pet->mood > 100) pet->mood = 100;
    printf("You played with your pet! Mood: %d\n", pet->mood);
}

void checkPetStatus(struct Pet *pet) {
    printf("Pet's current status:\n");
    printf("Hunger: %d\n", pet->hunger);
    printf("Health: %d\n", pet->health);
    printf("Mood: %d\n", pet->mood);
}

void petAction(struct Pet *pet) {
    int choice;
    printf("\nWhat would you like to do?\n");
    printf("1. Feed your pet\n");
    printf("2. Play with your pet\n");
    printf("3. Check pet's status\n");
    printf("4. Exit game\n");
    printf("Enter choice: ");
    scanf("%d", &choice);

    switch(choice) {
        case 1:
            feedPet(pet);
            break;
        case 2:
            playWithPet(pet);
            break;
        case 3:
            checkPetStatus(pet);
            break;
        case 4:
            printf("Thank you for playing! Bye!\n");
            exit(0);
        default:
            printf("Invalid choice! Try again.\n");
    }
}

int main() {
    struct Pet myPet = {100, 100, 50};  // Initial values: hunger=100, health=100, mood=50
    printf("Welcome to the Virtual Pet Game!\n");

    while (1) {
        petAction(&myPet);
    }

    return 0;
}

Explanation of the Program:

The program defines a simple structure `struct Pet` to hold the pet’s status, such as hunger, health, and mood. Here’s a breakdown of the code:

  • Struct Definition: The `struct Pet` is used to store the pet’s characteristics: hunger, health, and mood.
  • Functions: We define functions to feed the pet, play with it, check its status, and manage its actions in the game loop.
  • Game Loop: The game continuously prompts the player to interact with the pet by choosing actions such as feeding, playing, or checking its status.

How to Run the Program:

  1. Copy and paste the code into a C programming file (e.g., virtual_pet.c).
  2. Compile the program using a C compiler like gcc virtual_pet.c -o virtual_pet.
  3. Run the program by executing ./virtual_pet.
  4. Follow the on-screen prompts to interact with your virtual pet!
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)