Writing a Text Adventure Game in Go - Part 4
Apr 24, 2016 · 4 minute read · CommentsIntro (Putting it all together)
Welcome back to part 4 of Writing a Text Adventure in Go. In the last 3 parts we created individual systems to handle maps+events, combat, and items. In this part we will put it all together into one application that runs a Text Adventure from A to Z. In order to get this done we will create a project which will have several files to make the code more easily understood and in addition to this refactoring we will then make a few changes for the basic user interface.
Part 4 - Merging and Splitting
Intro
All the code for this tutorial can be found on github - https://github.com/p4tin/GoTextAdv. Open that code in another tab to reference as it will help you follow along with this article.
We now have 3 distinct systems with 3 main applications that play separate parts of the code but not a unified application. Here are the steps that we followed to create the project that you see now on github.
To start with the main.go file was stripped down to the bare minimum and is now nothing more that the function main and all the related User Input and Screen Output methods that have been renamed.
Second to separate the concerns of each file we created a data.go which contains all the preset game info that was created in the first three parts of the tutotials in addition we added Items to the Locations struct so that our player upon entering a location could see the treasure boxes and other things that were there that he/she could interact with.
To make this a more unified system we merged the game data into one location. In Part 1 we created a game structure to run the map and in part 2 we created characters that fought together, here we need to put that together so I opted to created a special character called player and that character keeps track not only of his own stats but of some game info like the current location, wether he/she is human or Npc, the items he/she is carrying and the welcome text.
type Character struct { Name string Health int Evasion int Alive bool Speed int Weap int Npc bool Items []int Welcome string CurrentLocation string }
The struct above and all the related function (i.e.: Equip, Attack, etc,…) were placed in a the ‘characters.go’ file. Also in that file, you will find the “Play” function whic is the main loop of the application since everything revolves around that player. As well you can see where we create the slice of Players (and sorting)to run the combat system.
Also we created a commands.go file that processes all the commands received from the user and interprets what he/she wanted. Note: that text imput was implemented everywhere except the combat system for now. Also the text input was now in case insensitive and you can use only the first 3 lettters of the location or item to denote what you are doing so: ‘open Chest” is equivalent to “OpEn Che”, same for go and goto. A ‘help’ and ‘quit’ commands were also created here.
To incoporate combat we had to add an event ‘type’ that we either set to “Story” or “Combat” to determine of the combat system had to be triggered or not. And then events were also modified to generate an opponent each time a combat event is detected to happen.
type Event struct { Type string Chance int Description string Health int Evt string } ... func (e *Event) ProcessEvent(player *Character) int { s1 := rand.NewSource(time.Now().UnixNano()) r1 := rand.New(s1) if e.Chance >= r1.Intn(100) { if e.Type == "Combat" { ...
opp := new(Character) *opp = *Enemies[1+rand.Intn(len(Enemies)-1)] opp.Npc = true opp.Speed = 1 + rand.Intn(100)
We also improved the user interface by putting color output using the excellent github.com/fatih/color project.
Final words
As you can see not much of the actual code we wrote in the first 3 tutorials has changed to incorporate the 3 systems into one, but the code is now cleaner and we can start making this into a more robust system. I will do one more tutorial in the next little bit replacing the console code with a basic web interface.
Have a great week and remember to GO GO GO…
*** Sign up for my email list to keep in touch with all the interesting new happenings in the go community with the GolangNewsFeed