render.go 1.5 KB

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