2D Game Development in Golang — Part 1

Chris Andrews
5 min readNov 6, 2019

--

So you might be asking yourself, why would anyone want to develop a video game in the Go programming language? Well, the answer is that programming in Go is super fun and simple! So why not try it with Game development?

Gophers also dream about games!

In this short mini tutorial I am going to demonstrate how anyone new to the Go language can get up and running with game development! Over the last few weeks I have been busy teaching myself the idioms of Go, and trying new ways of broadening my horizons and my skills. After doing some simple things like building a web server, and a simple API client I decided it was time to do something fun.

Upon searching around for some game development frameworks, I came across a really simple one called Ebiten (meaning tempura shrimp) that offered a clever engine capable of producing 2D games.

Ebiten — A dead simple 2D game library in Go

From the website, Ebiten describes itself as an open-source game library, with which you can develop 2D games with a simple API for multiple platforms in Go. It actually could not get much simpler than that! The documentation on this website is also very extensive so you should never have much issues in finding out how to do something.

Check out the docs here: https://godoc.org/github.com/hajimehoshi/ebiten

Getting Started

Step 1 — Prerequisites

So for starters, we are going to need to setup a few things in order to get ourselves going. I am going to assume that you have already installed Go on your development PC, and have at least version 1.12 or later. If you have not done this before, I highly recommend checking out the official Go documentation found here: https://golang.org/

For simplicity, we are going to use Windows as our main operating system for developing our first game in Go. For any other operating systems please check out Ebiten’s official installation guide before we continue: https://ebiten.org/install.html

Step 2 — Install Git

Git is required to get most of the Go packages including Ebiten. It is recommended that if you do not have Git installed already, to go over to the official Git page and download and install from there: https://git-scm.com/downloads

After installing Git, make sure to add the following to your PATH environment variables:

  • For 32 bit: C:\Program Files\Git\bin
  • For 64 bit: C:\Program Files (x86)\Git\Bin

Now that we have followed the previous instructions and installed Go, Git and depending on your OS, a C compiler (not needed on Windows) we should be ready to rock em and sock em.

Step 3 — Install Ebiten and Test

Open up a new command prompt and navigate to where you want to create your new game.

// Start by making a new directory for your game
mkdir yourGameName
cd yourGameName
// Now lets use go get to download Ebiten
go get github.com/hajimehoshi/ebiten
// To test that the installation was successful we will run a test // game from the Ebiten examples
go run -tags=example github.com/hajimehoshi/ebiten/examples/rotate

If all goes well, you should be greeted by a spinning gopher image!

Hello World

As is tradition, whenever embarking on a quest to learn a new programming language or framework, it is customary to create the ubiquitous “Hello World” script, which is designed to give you the very bare basics of what it takes to make a game.

main.go

Simply copy and paste the code into a new file called main.go, once you have done that, use the following command to run your program (make sure you have changed into that directory first):

go run main.go

If successful, you should see “Hello World” appear in a black screen, congratulations you have made your first “game”. Lets go over quickly what we just executed.

func update(screen *ebiten.Image) error {

The update function as seen above takes a pointer address to an object of type *ebiten.Image, which is the base image type used throughout Ebiten as our primary render target. The function is going to update by a default speed of 60 times per second or ticks per second (TPS). This update function will be passed to the main ebiten.Run function that we will see shortly.

// Update game state goes here
if ebiten.IsDrawingSkipped() {
return nil
}
// Drawing functions go here

This function is used to check whether any drawing operations were skipped, and will return nil if that is true. The update function is updating 60 times per second, so this rendering skip can sometimes happen when the final state of the screen was not adopted. The basic logic here is that you put all functions that update the state of an object on the screen above, and below it we will add the drawing functions which you will see soon.

ebitenutil.DebugPrint(screen, "Hello, World!")

This function call invokes the DebugPrint method from the ebitenutil library, simply passing it the screen object as the render target and then a message, which gets printed at the top left of the screen.

    return nil
}

Update returns an error value. In that code, update always returns nil. In general, when the updating function returns a non-nil error, the Ebiten game suspends. As that program never returns a non-nil error, the Ebiten game never stops unless the user closes the window.

func main() {
if err := ebiten.Run(update, 320, 240, 2, "Hello, World!");
err != nil {
log.Fatal(err)
}
}

This defines the main function which is the entry point to your program. ebiten. Run is the method that will run the main loop of an Ebiten game. The first argument in this function is the update function that we specified earlier. The next 2 arguments are the screen width and screen height and then followed by the scale of the screen along with the name of the window.

Now I know this basic hello world looks really boring, but in essence we have created our first game. It may look a little complicated at first, but the underlying syntax here is actually quite simple and is perfect for any beginner to get started.

In Part 2 of this tutorial we will cover some more advanced concepts like drawing images to the screen. I would advise you to take a look at the examples listed on the github account for Ebiten: https://github.com/hajimehoshi/ebiten/tree/master/examples

If there is anything here that is not clear, please leave a comment or shoot me a message!

Thank you and happy Gophering!

Useful references:

--

--

Chris Andrews

Experimenting with Python, Go and various frameworks for learning.