Skip to main content

Creating the Snake

Let's take a look at the main component of the game: The snake. There are several ways to implement one.

If you've used Go in the past, this next section won't be too surprising for you. You'll create a new type for the snake with the required properties.

For this, create a new file an call it snake.go. Then fill it with the code-snippet below.

package main
type Point struct {    X int    Y int}
type Snake struct {    Body      []Point    Direction Point}

The snake type contains the body and the current direction of the snake instance. But it lacks any functionality for now.

BuiltIn Type

Go offers a buildIn type for points. It's in image. Creating our own type reduces the size of the cart.