Gary23b

Golang Turtle Graphics Introduction

A real-time updating turtle graphics system for the golang programming language

11/01/2024

The Basics

A bit ago, I looked online to find a turtle graphics system for golang to help teach golang to a student, but found that none of the existing options had real time updating of the canvas. There is one that is online only, but... So, I endeavored to create it myself. I built it on top of the Ebitengine 2D game engine for golang.

Golang Turtle Graphics

5 turtles drawing 5 different things at once

Some of the cool features are:

I am of the opinion that teaching golang to high school age students actually makes a lot of sense. Most people immediately will reach for python, but I don't agree. Python has multiple issues. What I will mention here is that there is far too much magic in python. between decorators and overloading, python is doing a huge amount of hidden work to massage the data types. If you step out of the invisible field-of-play though, boom, it blows up. Also, python is not a complete stack, and you will eventually need to learn another programming language.

Project Setup

If you don't know how to setup a golang project, first make a new directory.

Then, inside the directory run the console command:

1
2
go mod init learning

Finally, we need to get this turtle package. Again in the console run the following command:

1
2
go get github.com/gary23b/turtle

Now you are ready.

The Most Basic Turtle Program

The most basic thing you can do is just open up a turtle window with nothing in it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

// Only a single import is needed most of the time.
import (
	"github.com/gary23b/turtle"
)

func main() {
    // here we choose how large of a turtle canvas we want in pixels
	params := turtle.Params{Width: 500, Height: 500}

    // Ebitengine, the package used to create the GUI, has to run
    // as the main thread. So we have to provide a callback function
    // to be called that has the users turtle code
	turtle.Start(params, drawFunc)
}

// drawFunc is started as a goroutine.
func drawFunc(window turtle.Window) {
	
}

The Most Basic Turtle Program

It is just a blank white screen that is 500 pixels wide by 500 pixels high.

Add a Turtle

To add the turtle sprite we need 2 more lines of code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import (
	"github.com/gary23b/turtle"
)

func main() {
	params := turtle.Params{Width: 500, Height: 500}
	turtle.Start(params, drawFunc)
}

// drawFunc is started as a goroutine.
func drawFunc(window turtle.Window) {
	t := window.NewTurtle() // Create a new turtle in the window
	t.ShowTurtle() // Make the turtle sprite visible
}

Showing the turtle sprite

Move The Turtle

The turtle can move in a bunch of different ways, but let's just start with moving forward and turning. Similar to the python turtle graphics system, this turtle will move at a set speed.

Lets have the turtle draw a box. So, we need to move forward, then turn left 4 times a make a box.

I am also only going show the drawFunc code now since it is all that is changing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func drawFunc(window turtle.Window) {
	t := window.NewTurtle()
	t.ShowTurtle()

	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
}

Turtle goes in a square

But where is the line? Ah, well we didn't put the "Pen" down.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func drawFunc(window turtle.Window) {
	t := window.NewTurtle()
	t.ShowTurtle()
	t.PenDown()

	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
	t.Forward(100)
	t.Left(90)
}

Turtle draws a square

And there we have it!

Square Flower

Now we can easily make this into a sort of spirograph by putting it into a loop. We can also randomize the colors for a more interesting picture. I also increased the turtle speed to 8000 pixels per second.

 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
package main

import (
	"image/color"
	"math/rand"

	"github.com/gary23b/turtle"
)

func main() {
	params := turtle.Params{Width: 500, Height: 500}
	turtle.Start(params, drawFunc)
}

func drawFunc(window turtle.Window) {
	t := window.NewTurtle()
	t.ShowTurtle()
	t.PenDown()
	t.Speed(8000)

	for range 180 {
		red := uint8(rand.Intn(256))
		green := uint8(rand.Intn(256))
		blue := uint8(rand.Intn(256))
		t.Color(color.RGBA{R: red, G: green, B: blue, A: 255})

		t.Forward(100)
		t.Left(90)
		t.Forward(100)
		t.Left(90)
		t.Forward(100)
		t.Left(90)
		t.Forward(100)
		t.Left(90)

		t.Left(2)
	}

}

Turtle draws a square flower





Thanks for reading! Subscribe for free and receive updates and support my work.

Some text some message..