render.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "strconv"
  4. "github.com/gdamore/tcell/v2"
  5. )
  6. func checkSize(screen tcell.Screen) bool {
  7. var retval bool = true
  8. x, y := screen.Size()
  9. if x < 80 {
  10. retval = false
  11. }
  12. if y < 24 {
  13. retval = false
  14. }
  15. return retval
  16. }
  17. func drawScore(screen tcell.Screen, style tcell.Style, score int) {
  18. var x, y int = 5, 24
  19. for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score), 10) + " ]") {
  20. screen.SetContent(x, y-1, r, nil, style)
  21. x++
  22. }
  23. }
  24. func drawCoords(screen tcell.Screen, style tcell.Style, snakex *int, snakey *int) {
  25. var x, y int = 25, 24
  26. for _, r := range []rune("[ x:" + strconv.FormatInt(int64(*snakex), 10) + " y: " + strconv.FormatInt(int64(*snakey), 10) + " ]") {
  27. screen.SetContent(x, y-1, r, nil, style)
  28. x++
  29. }
  30. }
  31. func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style) {
  32. if y2 < y1 {
  33. y1, y2 = y2, y1
  34. }
  35. if x2 < x1 {
  36. x1, x2 = x2, x1
  37. }
  38. // Fill background
  39. for row := y1; row <= y2; row++ {
  40. for col := x1; col <= x2; col++ {
  41. s.SetContent(col, row, ' ', nil, style)
  42. }
  43. }
  44. // Draw borders
  45. for col := x1; col <= x2; col++ {
  46. s.SetContent(col, y1, '═', nil, style)
  47. s.SetContent(col, y2, '═', nil, style)
  48. }
  49. for row := y1 + 1; row < y2; row++ {
  50. s.SetContent(x1, row, '║', nil, style)
  51. s.SetContent(x2, row, '║', nil, style)
  52. }
  53. // Only draw corners if necessary
  54. if y1 != y2 && x1 != x2 {
  55. s.SetContent(x1, y1, '╔', nil, style)
  56. s.SetContent(x2, y1, '╗', nil, style)
  57. s.SetContent(x1, y2, '╚', nil, style)
  58. s.SetContent(x2, y2, '╝', nil, style)
  59. }
  60. }