Cursive plaintext bannermaker turn-based snake
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.0 KiB

10 months ago
  1. #!/bin/zsh
  2. _STTY=$(stty -g) # Save current terminal setup
  3. printf "\e[2J" # clear screen, set cursos at beginning
  4. stty -echo -icanon # Turn off line buffering
  5. printf '\e[?25h' # Turn on the cursor
  6. # VARIABLES
  7. typeset -A IMAGE
  8. X=0
  9. Y=24
  10. height=0
  11. width=0
  12. snake='═'
  13. vertical=false
  14. forward=true
  15. upward=true
  16. pen_down=true
  17. function get_size() {
  18. # Get terminal size ('stty' is POSIX and always available).
  19. read -r LINES COLUMNS < <(stty size)
  20. height=$((LINES-1))
  21. width=$((COLUMNS-1))
  22. }
  23. function start_the_snake() {
  24. rm thesnake
  25. for i in {0..$height}
  26. do
  27. for j in {0..$width}
  28. do
  29. IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]}
  30. done
  31. done
  32. printf '\e[2J\e[H' ; printf '\e[0;24H'
  33. }
  34. function save_the_snake() {
  35. for i in {0..$height}
  36. do
  37. for j in {0..$width}
  38. do
  39. printf ${IMAGE["${i};${j}"]}
  40. done
  41. done >> thesnake
  42. sed -e "s/.\{$COLUMNS\}/&\n/g" < thesnake > multilinesnake
  43. }
  44. function show_the_snake() {
  45. printf '\e[2J\e[H'
  46. for i in {0..$height}
  47. do
  48. for j in {0..$width}
  49. do
  50. printf ${IMAGE["${i};${j}"]}
  51. done
  52. done
  53. printf '\e[%s;%sH' "$((Y+1))" "$((X+1))"
  54. }
  55. function at_exit() {
  56. printf "\e[?9l" # Turn off mouse reading
  57. printf "\e[?12l\e[?25h" # Turn on cursor
  58. stty "$_STTY" # reinitialize terminal settings
  59. clear
  60. exit
  61. }
  62. #═ ║╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟
  63. #╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬
  64. function move_snake_up() {
  65. if [ "$vertical" = false ] ; then
  66. if [ "$forward" = true ] ; then
  67. snake='╝'
  68. else
  69. snake='╚'
  70. fi
  71. else
  72. snake='║'
  73. fi
  74. #draw nothing it the pen is not down
  75. if [ "$pen_down" = false ] ; then
  76. snake=' '
  77. fi
  78. IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) ; vertical=true ; upward=true
  79. }
  80. function move_snake_down() {
  81. if [ "$vertical" = false ] ; then
  82. if [ "$forward" = true ] ; then
  83. snake='╗'
  84. else
  85. snake='╔'
  86. fi
  87. else
  88. snake='║'
  89. fi
  90. #draw nothing it the pen is not down
  91. if [ "$pen_down" = false ] ; then
  92. snake=' '
  93. fi
  94. IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) ; vertical=true ; upward=false
  95. }
  96. function move_snake_forward() {
  97. if [ "$vertical" = true ] ; then
  98. if [ "$upward" = true ] ; then
  99. snake='╔'
  100. else
  101. snake='╚'
  102. fi
  103. else
  104. snake='═'
  105. fi
  106. #draw nothing it the pen is not down
  107. if [ "$pen_down" = false ] ; then
  108. snake=' '
  109. fi
  110. IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) ; vertical=false ; forward=true
  111. }
  112. function move_snake_backward() {
  113. if [ "$vertical" = true ] ; then
  114. if [ "$upward" = true ] ; then
  115. snake='╗'
  116. else
  117. snake='╝'
  118. fi
  119. else
  120. snake='═'
  121. fi
  122. #draw nothing it the pen is not down
  123. if [ "$pen_down" = false ] ; then
  124. snake=' '
  125. fi
  126. IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) ; vertical=false ; forward=false
  127. }
  128. # Main #
  129. get_size
  130. start_the_snake
  131. echo
  132. while read -s -k 1 cr;
  133. do
  134. case "$cr" in
  135. w) move_snake_up ; show_the_snake ;;
  136. s) move_snake_down ; show_the_snake ;;
  137. a) move_snake_backward ; show_the_snake ;;
  138. d) move_snake_forward ; show_the_snake ;;
  139. t) pen_down=true ;;
  140. y) pen_down=false ;;
  141. q) save_the_snake && at_exit ;;
  142. esac
  143. done