Golang is slowly capturing the attention of developers across the world. As per the Stack Overflow Developer Survey, around 9.55% of the developers have been using Go and it is increasing gradually. Although Golang is widely used for systems programming, backend development, microservices and building of servers, developers have started all sorts of experiments with it. In this blog, we will look at one such aspect, i.e, game development in Golang.
Github Repository: All of the code that is written down in this tutorial blog is accessible on https://github.com/GolangCompany/2dGame.
In order to continue, let us create a new directory inside the go-workspace. Let us name this directory ‘2DGame’. Inside this directory, let us create another folder named ‘setup’. Since I’m using Windows, I’ll go to Command Prompt, and I’ll type:
cd go-workspace
mkdir 2DGame
cd 2DGame
mkdir setup
cd setup
code .
go mod init github.com/golang-company/2DGame
Following the creation of the go.mod file, we will install the library and associated samples
go get github.com/gonutz/prototype/…
Then, we will go to the programming section. For this, we will copy the example from the https://github.com/gonutz/prototype.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"math"
"github.com/gonutz/prototype/draw"
)
func main() {
draw.RunWindow("Title", 640, 480, update)
}
func update(window draw.Window) {
// find the screen center
w, h := window.Size()
centerX, centerY := w/2, h/2
// draw a button in the center of the screen
mouseX, mouseY := window.MousePosition()
mouseInCircle := math.Hypot(float64(mouseX-centerX), float64(mouseY-centerY)) < 20
color := draw.DarkRed
if mouseInCircle {
color = draw.Red
}
window.FillEllipse(centerX-20, centerY-20, 40, 40, color)
window.DrawEllipse(centerX-20, centerY-20, 40, 40, draw.White)
if mouseInCircle {
window.DrawScaledText("Close!", centerX-40, centerY+25, 1.6, draw.Green)
}
// check all mouse clicks that happened during this frame
for _, click := range window.Clicks() {
dx, dy := click.X-centerX, click.Y-centerY
squareDist := dx*dx + dy*dy
if squareDist <= 20*20 {
// close the window and end the application
window.Close()
}
}
}
Next, we will take a look at the output. For this, we will type go run main.go. And we get the output:
With this, the setup process is complete. Next, we will move on to other aspects of game development in Golang.
go mod init github.com/golang-company/2DGame
go get github.com/gonutz/prototype/…
Now, we will focus on the main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import (
"github.com/gonutz/prototype/draw"
)
func main() {
height := 350
width := 550
draw.RunWindow("Hello everyone ", width, height,
func(window draw.Window) {
window.DrawText("Hello everyone", 0, 0, draw.White)
},
)
}
Output:
Now, if you face any issues while running the program by typing go run main.go
, you have to go to the go.mod file and you have to upgrade transitive dependencies and direct dependencies available above require github.com/gonutz/prototype v1.1.1
. This will resolve your issue.
In this section, we will delve deep into game development in Golang. For this, we will create another folder in the ‘2DGame’ directory. And we will name this ‘motion’. Next, we will follow the same process as before, create a go.mod file and install the gonutz/prototype library. Following this, we will move on to the main.go source file. We will type the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main
import "github.com/gonutz/prototype/draw"
func main() {
title := "Text in motion"
height := 350
width := 350
i := 0
draw.RunWindow(title, height, width, func(window draw.Window) {
i++
window.FillEllipse(i%height, i%width, i%width, i%height, draw.Color{float32(i % 7),
float32(i % 11),
float32(i % 17),
1,
})
})
}
In this section, we will create another folder named ‘keyboards’ inside the ‘2DGame’ and following this, we will create the go.mod file and install the gonutz/prototype library. Next, we will move on to the main.go file.
And we will write the program below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
import "github.com/gonutz/prototype/draw"
func main() {
title := "Text in motion"
height := 300
width := 300
CurrentX := width / 2
CurrentY := height / 2
draw.RunWindow(title, height, width, func(window draw.Window) {
if window.IsKeyDown(draw.KeyDown) {
CurrentY += 1
} else if window.IsKeyDown(draw.KeyUp) {
CurrentY -= 1
} else if window.IsKeyDown(draw.KeyLeft) {
CurrentX -= 1
} else if window.IsKeyDown(draw.KeyRight) {
CurrentX += 1
}
window.FillRect(CurrentX, CurrentY, 50, 60, draw.Blue)
})
}
if window.IsKeyDown(draw.KeyDown) {
CurrentY += 1
And we will decrease the value of Y which will make the rectangle go up.
For the right side motion, we will use
else if window.IsKeyDown(draw.KeyRight) {
CurrentX += 1
And we will increase the value of X which will make the rectangle go right.
For the left side motion, we will use
else if window.IsKeyDown(draw.KeyLeft) {
CurrentX -= 1
And we will decrease the value of X which will make the rectangle go left.
Here, the window.IsKeyDown is used to determine the condition/state of the specific key. In this case, the Return key is transferred to the IsKeyDown method.
We will be following the same steps as before, only this time we will name the folder ‘mouse’. The program that we will type in the main.go file is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main
import "github.com/gonutz/prototype/draw"
func main() {
title := "Capture mouse input"
height := 300
width := 300
rectXCord := 0
rectYCord := 0
rectHeight := 30
rectWidth := 60
draw.RunWindow(title, height, width, func(window draw.Window) {
rectXCord, rectYCord = window.MousePosition()
rectXCord = rectXCord - 30
rectYCord = rectYCord - 15
window.FillRect(rectXCord, rectYCord, rectWidth, rectHeight, draw.Cyan)
})
}
rectXCord = rectXCord - 30
rectYCord = rectYCord - 15
Now, let us check out the output.
Output:
We will create a folder called ‘sound’ and write the program in main.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main
import "github.com/gonutz/prototype/draw"
func main() {
title := "Text in motion"
height := 350
width := 350
draw.RunWindow(title, height, width, func(window draw.Window) {
window.DrawText("Hit the spacebar to play sound",
10,
height/2,
draw.Cyan)
if window.IsKeyDown(draw.KeySpace) {
window.PlaySoundFile("./sound.wav")
}
})
}
Hopefully, you were able to understand the basic concepts of the game development using the Golang programming language. Once you get a hold of the preliminary concepts, you will be able to work on intricate projects. And soon, you will learn how to add png images, scale and rotate it and use sprite sheets. Until then, master the initial concepts and keep on programming.