snake

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CenterAlignString added in v0.3.2

func CenterAlignString(input string, width int) string

CenterAlignString will align the input string in the middle, based on the supplied width

func ListenEvents added in v0.3.2

func ListenEvents(game *Game, inputEvent chan termbox.Key, resizeEvent chan Size)

ListenEvents will start listening for keyboard and resize events, then emit them on the supplied channels

Types

type Apple

type Apple struct {
	// contains filtered or unexported fields
}

Apple is delicious

func NewApple

func NewApple(position Position) *Apple

NewApple plops a delicious apple from the nearest apple tree

type Direction

type Direction struct {
	X int
	Y int
}

Direction in -1/+1 X and Y coordinates

func GetInputDirection

func GetInputDirection(key termbox.Key) Direction

GetInputDirection will convert a keyboard key to a Direction

func (Direction) Equals

func (dir Direction) Equals(x int, y int) bool

Equals compares this object with X and Y integers

func (Direction) Zero

func (dir Direction) Zero() bool

Zero returns true if values are set to 0, otherwise it returns true

type Game

type Game struct {
	// contains filtered or unexported fields
}

Game of sneaky snakey goodness

func NewGame

func NewGame() *Game

NewGame starts a new game of Snake (bet you didn't guess that!)

func (*Game) IncrementScore

func (game *Game) IncrementScore()

IncrementScore will increase the score of the current game

func (*Game) Resize added in v0.3.2

func (game *Game) Resize()

Resize adjusts the size of the game and level

func (*Game) Restart added in v0.3.2

func (game *Game) Restart()

Restart the game

func (*Game) Start

func (game *Game) Start()

Start will run the update loop in a goroutine

type Level

type Level struct {
	// contains filtered or unexported fields
}

Level of awesomeness?

func NewLevel

func NewLevel(game *Game, size Size) *Level

NewLevel creates a new level of a certain size, also spawning in the snake and an apple

func (*Level) EatApple

func (level *Level) EatApple()

EatApple will destroy the current apple, increment the snake size and score, finally spawning a new apple

func (*Level) GetRandomPosition

func (level *Level) GetRandomPosition() Position

GetRandomPosition will return a randomize Position, constrained to the size of the current level

func (*Level) IsWall added in v0.3.2

func (level *Level) IsWall(pos Position) bool

IsWall will return true if the supplied position is a wall or a corner

func (*Level) Render

func (level *Level) Render() string

Render returns a string representation of the level

func (*Level) Update

func (level *Level) Update()

Update keeps things moving

type Position

type Position struct {
	X int
	Y int
}

Position in X and Y coordinates

func (Position) Equals

func (pos Position) Equals(x int, y int) bool

Equals compares this object with X and Y integers

func (Position) Zero

func (pos Position) Zero() bool

Zero returns true if values are set to 0, otherwise it returns true

type Score added in v0.3.2

type Score struct {
	// contains filtered or unexported fields
}

Score information for the game

func NewScore added in v0.3.2

func NewScore() *Score

NewScore creates and returns a new Score object

func (*Score) GetHighscore added in v0.3.2

func (score *Score) GetHighscore() int

GetHighscore returns the best score

func (*Score) GetScore added in v0.3.2

func (score *Score) GetScore() int

GetScore returns the current score

func (*Score) IncrementScore added in v0.3.2

func (score *Score) IncrementScore()

IncrementScore updates the current and best score

func (*Score) SetScore added in v0.3.2

func (score *Score) SetScore(newScore int)

SetScore updates the current and best score

type Size

type Size struct {
	Width  int
	Height int
}

Size in Width and Hiehgt

type Snake

type Snake struct {
	// contains filtered or unexported fields
}

Snake wants an Apple

func NewSnake

func NewSnake(position Position, direction Direction) *Snake

NewSnake spawns a hissing creature

func (*Snake) CheckHitbox

func (snake *Snake) CheckHitbox(position Position) bool

CheckHitbox will see if the supplied position is within the snake's calculated "body" (including head)

func (*Snake) GetHead

func (snake *Snake) GetHead() Position

GetHead returns the snake head position

func (*Snake) IncrementSize

func (snake *Snake) IncrementSize()

IncrementSize will increase the size (length) of the snake

func (*Snake) Move

func (snake *Snake) Move()

Move the snake one coordinate along its current direction

func (*Snake) UpdateDirection

func (snake *Snake) UpdateDirection(direction Direction)

UpdateDirection will set the snake direction, so long as it's not the opposite direction

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL