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