Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-07 14:32:54

15: Project: Adventure Game

WARNING This exercise is in DRAFT status, so there may be errors. If you find any, please email me at help@learncodethehardway.com so I can fix them.

You are now at the end of the basics portion of the course. At this point you know about the core concepts of Go, but you need to build your skill. You build your skill by creating as many tiny projects as you possibly can, which is what we'll do in the next two modules. To give you a small sample of what's to come I want you to create a "Text Adventure Game" which is also called "Interactive Fiction."

Text Adventure Games are a genre of game that uses simple narrative combined with prompts to navigate the player through a series of "rooms" or "scenes." It'll typically look something like this:

You enter a dark cavern with a small light in the ceiling.
There is a door to the south and another to the east.
> 

The > is a prompt and the player can enter commands like go north, attack spider, and more to complete the adventure.

Your job is to take my tiny sample and use it to create your own game with your own story (or no story if you are not that interested in games).

Research

If you want to become a great portrait painter then you should spend time studying and copying the great portraits. If you want to become a great Blues guitarist then you should spend time studying and copying the great Blues songs. If you want to become a great Jazz musician then you can probably just smash your hands randomly on a piano before climbing inside with a mallet to make "music maaaaan."

Therefore, if you want to make a game in a certain genre then you should spend some time playing games in that genre. Here's a list of games you can try before you start to work on your own:

You can also just watch a few Youtube videos of people playing these games to get a feel for them.

Starter Sample

I've created a small bit of code to help you get started. This uses functions, for, variables, and switch to create a series of rooms that are connected to each other. You should rename these from RoomA to more meaningful names like LavaBridge.

I also recommend that you practice your "code from memory" with this after you get it working the first time, or as your first attempt.

View Source file ex15/main.go Only

package main

import (
    "fmt"
)

// you need this for hitpoints and shared state
var test_global bool = false

func RoomA() string {
    fmt.Println("RoomA")
    return "RoomB"
}

func RoomB() string {
    fmt.Println("RoomB")
    return "End"
}

func End() {
    has_died = true
    fmt.Println("You died.")
}

func main() {
    room := "RoomA"

    for {
        switch room {
        case "RoomA":
            room = RoomA()
        case "RoomB":
            room = RoomB()
        case "End":
            End()
            return
        }
    }
}

Tips and Tricks

  1. As usual, take your time and use the process I gave you in Exercise 04.
  2. I recommend writing out your story in a plain text file so you can copy-paste it into your code.
  3. I also recommend not putting your story into your code at first. Only create the structure and print out "SceneA" or "RoomB" from your story. Then when it flows well put your the text into the game.

Basic Features

Your game should include the following:

  1. At least 8 rooms or scenes you can move around in.
  2. Ability to handle one word commands like "north", "attack", etc.
  3. Detecting that the player entered invalid input and letting them try again.
  4. A way to win and a way to fail.

Advanced Features

There's a few advanced features you can try, but if these prove to be too difficult then come back to it later:

  1. Give the player hitpoints.
  2. Keep track of whether a player has been in a room and done something to change it. Display different text if they have.
  3. Track whether the player has picked up an item in one room, that's neeeded in another room.

Choose Your Adventure

You have two possible paths with this exercise:

  1. Work for as long as you can on this game making it as awesome as possible.
  2. Create as many little text adventure games as you possibly can. Maybe give yourself a challenge of "7 games in 7 days."

Either way will improve your skills, so pick one and give it all you can. You could also do both. Maybe make 7 little games in 7 days, then make one awesome game in one week.

Next Steps

You are now done with the basics you need to learn the rest of Go. In the next two modules we will learn progressively more advanced Go techniques as well as use those techniques to create copies of various command line utilities. The goal is to make you capable enough at Go that you can create little tools, websites, network servers, or anything else you want to make.

Previous Lesson Back to Module

Register for Learn Go the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.