Golang

 

 

Learn how to create an interactive pet care game in Go programming language!

Introduction

In this project, we are going to create a simple virtual pet game where the player can interact with their pet, feed it, and keep it healthy. The game will involve managing the pet’s hunger, happiness, and health status. The player will need to take care of the pet by feeding it and keeping it happy. This game serves as a great introduction to programming concepts like loops, conditionals, and user input in the Go language.

Objective

The main objective is to design a basic virtual pet that can be fed, played with, and tracked for its health and happiness. By running the game, players will interact with their pet through a text-based interface and make decisions to keep their pet alive and healthy.

Code

package main

import (
    "fmt"
    "time"
)

type Pet struct {
    name       string
    hunger     int
    happiness  int
    health     int
}

func (p *Pet) Feed() {
    p.hunger -= 10
    if p.hunger < 0 { p.hunger = 0 } fmt.Println(p.name, "has been fed!") } func (p *Pet) Play() { p.happiness += 10 if p.happiness > 100 {
        p.happiness = 100
    }
    fmt.Println(p.name, "played and is now happier!")
}

func (p *Pet) HealthStatus() {
    fmt.Printf("\n%s's Health Status:\n", p.name)
    fmt.Printf("Hunger: %d/100\n", p.hunger)
    fmt.Printf("Happiness: %d/100\n", p.happiness)
    fmt.Printf("Health: %d/100\n", p.health)
}

func (p *Pet) Age() {
    p.hunger += 5
    if p.hunger > 100 {
        p.hunger = 100
    }

    p.happiness -= 5
    if p.happiness < 0 {
        p.happiness = 0
    }

    p.health -= 3
    if p.health < 0 {
        p.health = 0
    }
}

func main() {
    fmt.Println("Welcome to the Virtual Pet Game!")
    time.Sleep(1 * time.Second)

    var name string
    fmt.Print("Enter your pet's name: ")
    fmt.Scanln(&name)

    pet := Pet{name: name, hunger: 50, happiness: 50, health: 80}

    for {
        pet.HealthStatus()

        var choice int
        fmt.Println("\nWhat would you like to do?")
        fmt.Println("1. Feed Pet")
        fmt.Println("2. Play with Pet")
        fmt.Println("3. Exit Game")
        fmt.Print("Enter your choice: ")
        fmt.Scanln(&choice)

        switch choice {
        case 1:
            pet.Feed()
        case 2:
            pet.Play()
        case 3:
            fmt.Println("Thanks for playing!")
            return
        default:
            fmt.Println("Invalid choice. Please try again.")
        }

        pet.Age()
        time.Sleep(2 * time.Second)
    }
}

Program Explanation

The program defines a Pet structure that holds the pet’s attributes such as name, hunger, happiness, and health. It provides functions like Feed(), Play(), HealthStatus(), and Age() to interact with the pet.

In the main() function, the program asks the user to input the pet’s name, then enters a loop where it displays the pet’s current health status and asks the user for input. The user can choose to feed or play with the pet, or exit the game. Each action modifies the pet’s hunger, happiness, and health values.

How to Run the Program

  1. Make sure you have Go installed on your system. If not, download it from here.
  2. Copy the code above and save it to a file with a .go extension (e.g., virtual_pet.go).
  3. Open a terminal/command prompt and navigate to the directory where your .go file is located.
  4. Run the program by typing go run virtual_pet.go in the terminal.
  5. Follow the on-screen instructions 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 :)