snake/render.go

71 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"strconv"
"github.com/gdamore/tcell/v2"
)
2023-11-12 16:57:52 +01:00
// func checkSize(screen tcell.Screen) bool {
// var retval bool = true
// x, y := screen.Size()
// if x < 80 {
// retval = false
// }
// if y < 24 {
// retval = false
// }
// return retval
// }
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, snakex *int, snakey *int) {
var x, y int = 25, 24
for _, r := range []rune("[ x:" + strconv.FormatInt(int64(*snakex), 10) + " y: " + strconv.FormatInt(int64(*snakey), 10) + " ]") {
screen.SetContent(x, y-1, r, nil, style)
x++
}
}
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
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)
}
}