Fat Old Yeti

Fat Old Yeti

Being a blog of thoughts and tutorials from a hobby game developer.

11 Feb 2021

Roguelike Tutorial 13

Roguelike in Go - Part 13 (Refactor)

All code for this tutorial can be found here.

This will just be a few minor refactors.

Let’s start with dice.go. I was preoccupied and didn’t have the brain cycles to think of a simple solution for getting a number between two numbers. Open GetRandomBetween and replace the entire body with this:

return GetDiceRoll(high-low) + high

Yep. That’s a HECK of a lot better and much better performance.

Go into monster_systems.go and find the following line:

mon := result.Components[monster].(*Monster)

Comment it out for now. The compiler will go crazy if you try to build (instead of just run) the program because since we aren’t logging “Skeleton shivers to it’s bones” anymore, it’s unused. We WILL be reusing it very soon, so don’t bother deleting it.

The last thing we will address is a result of me being lazy. I knew this was terrible, but didn’t really want to have to worry about it until now. We are literally loading the wall and floor image every time we add it to a tile. Since we have 4000 tiles and we add all walls, then carve out floors, we are loading 6000ish images instead of 2. Yep…the suck factor is HIGH. Open level.go and at the top, outside of all functions and right around where we declare TileType, add the following lines:

var floor *ebiten.Image
var wall *ebiten.Image

Let’s create a new function to only load them once:

func loadTileImages() {
	if floor != nil && wall != nil {
	var err error

	floor, _, err = ebitenutil.NewImageFromFile("assets/floor.png")
	if err != nil {

	wall, _, err = ebitenutil.NewImageFromFile("assets/wall.png")
	if err != nil {

Now find all the spots in the file where we load floor and wall images and remove them. They will be the same blocks of code as above.

Lastly, go into the constructor function for Level, and right under this line:

l := Level{}

add this line:


That is all we need to do for this refactor. Note that while the checked in code does the same job of this refactor, this version is slightly different because if thought of a better way while typing this. Both are functionally the same.

If you have any questions, feel free to ask me at fatoldyeti@gmail.com or @idiotcoder on the gophers slack. Also there is the discord.