2023-11-07 21:47:32 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
2023-11-23 23:28:44 +01:00
|
|
|
// initSnake Initialize the snake
|
2023-11-23 22:36:44 +01:00
|
|
|
func initSnake(snakex *int, snakey *int) Snake {
|
|
|
|
var x, y int = *snakex, *snakey
|
|
|
|
snake := Snake{
|
|
|
|
head: Position{
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
tail: []Position{
|
|
|
|
{
|
|
|
|
x: x - 1,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: x - 2,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: x - 3,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
length: 3,
|
|
|
|
direction: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
return snake
|
|
|
|
}
|
|
|
|
|
|
|
|
func drawSnake(screen tcell.Screen, style tcell.Style, snake *Snake) {
|
|
|
|
|
|
|
|
// Reverse tail, chop of the excesses,
|
|
|
|
snake.tail = ReverseSlice(snake.tail)
|
|
|
|
if len(snake.tail) > snake.length {
|
|
|
|
snake.tail = snake.tail[:snake.length]
|
|
|
|
}
|
|
|
|
// reverse it back to the original order, replace the tail in `snake`
|
|
|
|
snake.tail = ReverseSlice(snake.tail)
|
|
|
|
|
|
|
|
// Draw the body
|
|
|
|
for _, segment := range snake.tail {
|
|
|
|
screen.SetContent(segment.x, segment.y, '+', nil, style)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the head, make sure it is on top of everything
|
|
|
|
screen.SetContent(snake.head.x, snake.head.y, '0', nil, style)
|
|
|
|
|
2023-11-07 21:47:32 +01:00
|
|
|
}
|
2023-11-12 14:51:06 +01:00
|
|
|
|
2023-11-23 22:36:44 +01:00
|
|
|
func snakeDirection(press int, snake *Snake) {
|
2023-11-12 14:51:06 +01:00
|
|
|
if press == Left {
|
2023-11-23 22:36:44 +01:00
|
|
|
if snake.direction != Right {
|
|
|
|
snake.head.x--
|
|
|
|
snake.tail = append(snake.tail, Position{x: snake.head.x + 1, y: snake.head.y})
|
|
|
|
snake.direction = press
|
|
|
|
}
|
2023-11-12 14:51:06 +01:00
|
|
|
} else if press == Right {
|
2023-11-23 22:36:44 +01:00
|
|
|
if snake.direction != Left {
|
|
|
|
snake.head.x++
|
|
|
|
snake.tail = append(snake.tail, Position{x: snake.head.x - 1, y: snake.head.y})
|
|
|
|
snake.direction = press
|
|
|
|
}
|
2023-11-12 14:51:06 +01:00
|
|
|
} else if press == Up {
|
2023-11-23 22:36:44 +01:00
|
|
|
if snake.direction != Down {
|
|
|
|
snake.head.y--
|
|
|
|
snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y + 1})
|
|
|
|
snake.direction = press
|
|
|
|
}
|
2023-11-12 14:51:06 +01:00
|
|
|
} else if press == Down {
|
2023-11-23 22:36:44 +01:00
|
|
|
if snake.direction != Up {
|
|
|
|
snake.head.y++
|
|
|
|
snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y - 1})
|
|
|
|
snake.direction = press
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a pos hits the tail
|
|
|
|
func hitsTail(snake *Snake, x int, y int) bool {
|
|
|
|
var hit bool = false
|
|
|
|
|
|
|
|
for _, segment := range snake.tail {
|
|
|
|
if segment.x == x && segment.y == y {
|
|
|
|
hit = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hit
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReverseSlice[T comparable](s []T) []T {
|
|
|
|
var r []T
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
|
|
r = append(r, s[i])
|
2023-11-12 14:51:06 +01:00
|
|
|
}
|
2023-11-23 22:36:44 +01:00
|
|
|
return r
|
2023-11-12 14:51:06 +01:00
|
|
|
}
|