snake/render.go

59 lines
1.4 KiB
Go

package main
import (
"strconv"
"github.com/gdamore/tcell/v2"
)
func drawScore(screen tcell.Screen, style tcell.Style, score int) {
var x, y int = 5, 24
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 10) + " ]") {
screen.SetContent(x, y-1, r, nil, style)
x++
}
}
func drawCoords(screen tcell.Screen, style tcell.Style, snake *Snake) {
var x, y int = 25, 24
for _, r := range []rune("[ x:" + strconv.FormatInt(int64(*&snake.head.x), 10) + " y: " + strconv.FormatInt(int64(*&snake.head.y), 10) + " ]") {
screen.SetContent(x, y-1, r, nil, style)
x++
}
}
func drawBox(s tcell.Screen, style tcell.Style, x1, y1, x2, y2 int) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
// Fill background
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
s.SetContent(col, row, ' ', nil, style)
}
}
// Draw borders
for col := x1; col <= x2; col++ {
s.SetContent(col, y1, '═', nil, style)
s.SetContent(col, y2, '═', nil, style)
}
for row := y1 + 1; row < y2; row++ {
s.SetContent(x1, row, '║', nil, style)
s.SetContent(x2, row, '║', nil, style)
}
// Only draw corners if necessary
if y1 != y2 && x1 != x2 {
s.SetContent(x1, y1, '╔', nil, style)
s.SetContent(x2, y1, '╗', nil, style)
s.SetContent(x1, y2, '╚', nil, style)
s.SetContent(x2, y2, '╝', nil, style)
}
}