A classic snake game implemented in golang.
Install and update this package with go get -u github.com/sinomoe/gosnake
.
-
Construct an initial baby snake and food pair.
-
Construct
WallGenerator
, aWallGenerator
is a function which receives aWorld
and return aWall
,Wall
is function which receives aCoordinates
and return whether that coordinates is on the wall. Here is theDefaultWallGenerator
.DefaultWallGenerator = func(w World) Wall { return func(c Coordinates) bool { x = c.x y = c.y // this is a rectangle if x <= 0 || y <= 0 || x >= w.XLen || y >= w.YLen { return true } return false } }
The means of a
WallGenerator
is to return an function(Wall
) which is binded withWorld
boundary. So we can restrict the reachable area by changing the if condition in L5 of above code. -
Construct
GameConfig
, callGameInit(config)
and return a*Game
instance. -
Call
G.WalkUp()
,G.WalkDown()
,G.WalkLeft()
,G.WalkRight()
according to the key inputed. -
Check game status by
G.Score()
andG.IsOver()
. -
If
G.IsOver()
returnstrue
, the game is over.
For example of what can be done take a look at demo/demo.go
. You can try it with go run: go run demos/demo.go
.
The outer coordinates of xb, yb denotes the coordinates of the gameBox
in demo/demo.go
, in this implementation, xb, yb is the coordinates of termbox-go. xoff and yoff denotes the offset value of the game coordinates with respect to the gameBox coordinates.
The inner coordinates of x, y denotes the coordinates of the Game
or World
in snake.go
; the red part of the picture denotes the Wall which is the border of the game world; the green part denotes the reachable area for the snake.
MIT