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:
- Copy and paste the code into a C programming file (e.g., virtual_pet.c).
- Compile the program using a C compiler like gcc virtual_pet.c -o virtual_pet.
- Run the program by executing ./virtual_pet.
- Follow the on-screen prompts to interact with your virtual pet!