render.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. content := "[ Score: " + strconv.FormatInt(int64(score.score), 10) + " ]"
  10. for i := 0; i < len(content); i++ {
  11. screen.SetContent(x, y-1, rune(content[i]), nil, style)
  12. x++
  13. }
  14. }
  15. // drawCoords Print the coordinates of the head of the snake
  16. // func drawCoords(screen tcell.Screen, style tcell.Style, snake *Snake) {
  17. // var x, y int = 25, 24
  18. // var snakex int = *&snake.head.x
  19. // var snakey int = *&snake.head.y
  20. // content := "[ x:" + strconv.Itoa(snakex) + " y: " + strconv.Itoa(snakey) + " ]"
  21. // for i := 0; i < len(content); i++ {
  22. // screen.SetContent(x, y-1, rune(content[i]), nil, style)
  23. // x++
  24. // }
  25. // }
  26. // drawBox Draw the outline of the box the snake can move in
  27. func drawBox(s tcell.Screen, style tcell.Style, x1, y1, x2, y2 int) {
  28. if y2 < y1 {
  29. y1, y2 = y2, y1
  30. }
  31. if x2 < x1 {
  32. x1, x2 = x2, x1
  33. }
  34. // Fill background
  35. for row := y1; row <= y2; row++ {
  36. for col := x1; col <= x2; col++ {
  37. s.SetContent(col, row, ' ', nil, style)
  38. }
  39. }
  40. // Draw borders
  41. for col := x1; col <= x2; col++ {
  42. s.SetContent(col, y1, '═', nil, style)
  43. s.SetContent(col, y2, '═', nil, style)
  44. }
  45. for row := y1 + 1; row < y2; row++ {
  46. s.SetContent(x1, row, '║', nil, style)
  47. s.SetContent(x2, row, '║', nil, style)
  48. }
  49. // Only draw corners if necessary
  50. if y1 != y2 && x1 != x2 {
  51. s.SetContent(x1, y1, '╔', nil, style)
  52. s.SetContent(x2, y1, '╗', nil, style)
  53. s.SetContent(x1, y2, '╚', nil, style)
  54. s.SetContent(x2, y2, '╝', nil, style)
  55. }
  56. }