render.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "strconv"
  4. "github.com/gdamore/tcell/v2"
  5. )
  6. // drawScore Print the score
  7. func drawScore(screen tcell.Screen, style tcell.Style, score *Score) {
  8. var x, y int = 5, 24
  9. for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(score.score), 10) + " ]") {
  10. screen.SetContent(x, y-1, r, nil, style)
  11. x++
  12. }
  13. }
  14. // drawCoords Print the coordinates of the head of the snake
  15. func drawCoords(screen tcell.Screen, style tcell.Style, snake *Snake) {
  16. var x, y int = 25, 24
  17. for _, r := range []rune("[ x:" + strconv.FormatInt(int64(*&snake.head.x), 10) + " y: " + strconv.FormatInt(int64(*&snake.head.y), 10) + " ]") {
  18. screen.SetContent(x, y-1, r, nil, style)
  19. x++
  20. }
  21. }
  22. // drawBox Draw the outline of the box the snake can move in
  23. func drawBox(s tcell.Screen, style tcell.Style, x1, y1, x2, y2 int) {
  24. if y2 < y1 {
  25. y1, y2 = y2, y1
  26. }
  27. if x2 < x1 {
  28. x1, x2 = x2, x1
  29. }
  30. // Fill background
  31. for row := y1; row <= y2; row++ {
  32. for col := x1; col <= x2; col++ {
  33. s.SetContent(col, row, ' ', nil, style)
  34. }
  35. }
  36. // Draw borders
  37. for col := x1; col <= x2; col++ {
  38. s.SetContent(col, y1, '═', nil, style)
  39. s.SetContent(col, y2, '═', nil, style)
  40. }
  41. for row := y1 + 1; row < y2; row++ {
  42. s.SetContent(x1, row, '║', nil, style)
  43. s.SetContent(x2, row, '║', nil, style)
  44. }
  45. // Only draw corners if necessary
  46. if y1 != y2 && x1 != x2 {
  47. s.SetContent(x1, y1, '╔', nil, style)
  48. s.SetContent(x2, y1, '╗', nil, style)
  49. s.SetContent(x1, y2, '╚', nil, style)
  50. s.SetContent(x2, y2, '╝', nil, style)
  51. }
  52. }