Browse Source

added support for different size of terminal

main
crunk 5 years ago
parent
commit
9af0edf8f5
  1. 8
      readme.md
  2. 30
      snakelogan

8
readme.md

@ -2,5 +2,11 @@
the cursive plaintext snake writer the cursive plaintext snake writer
* new so it still works pretty bad * new so it still works pretty bad
* you need your terminal to 80 characters wide and 30 characters high. * now support for any size of terminal
* use w,s,a,d to move the snake use q to exit and save the snake * use w,s,a,d to move the snake use q to exit and save the snake
toekomstmuziek:
* build a more TUI like interface where we don't clear the screen
* removing the flasing effect while drawing.
* put a blinking cursor where the snake currently is
* implement pen-up and pen-down

30
snakelogan

@ -2,24 +2,34 @@
_STTY=$(stty -g) # Save current terminal setup _STTY=$(stty -g) # Save current terminal setup
#printf "\e[2J" # clear screen, set cursos at beginning printf "\e[2J" # clear screen, set cursos at beginning
stty -echo -icanon # Turn off line buffering stty -echo -icanon # Turn off line buffering
printf '\e[?25h'
# VARIABLES # VARIABLES
typeset -A IMAGE typeset -A IMAGE
X=0 X=0
Y=24 Y=24
height=0
width=0
snake='═' snake='═'
vertical=false vertical=false
forward=true forward=true
upward=true upward=true
function get_size() {
# Get terminal size ('stty' is POSIX and always available).
read -r LINES COLUMNS < <(stty size)
height=$((LINES-1))
width=$((COLUMNS-1))
}
function start_the_snake() { function start_the_snake() {
rm thesnake rm thesnake
for i in {0..29} for i in {0..$height}
do do
for j in {0..79} for j in {0..$width}
do do
IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]} IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]}
done done
@ -27,20 +37,21 @@ function start_the_snake() {
} }
function save_the_snake() { function save_the_snake() {
for i in {0..29} for i in {0..$height}
do do
for j in {0..79} for j in {0..$width}
do do
printf ${IMAGE["${i};${j}"]} printf ${IMAGE["${i};${j}"]}
done done
done >> thesnake done >> thesnake
#sed -e "s/.\{$((width-1))\}/&\n/g" < thesnake > snake80
} }
function show_the_snake() { function show_the_snake() {
clear printf '\e[2J\e[H'
for i in {0..29} for i in {0..$height}
do do
for j in {0..79} for j in {0..$width}
do do
printf ${IMAGE["${i};${j}"]} printf ${IMAGE["${i};${j}"]}
done done
@ -110,6 +121,7 @@ function move_snake_backward() {
} }
# Main # # Main #
get_size
start_the_snake start_the_snake
while read -s -k 1 cr; while read -s -k 1 cr;
do do
@ -117,7 +129,7 @@ do
w) move_snake_up ; show_the_snake ;; w) move_snake_up ; show_the_snake ;;
s) move_snake_down ; show_the_snake ;; s) move_snake_down ; show_the_snake ;;
a) move_snake_backward ; show_the_snake ;; a) move_snake_backward ; show_the_snake ;;
d) move_snake_forward ; show_the_snake ;; d) move_snake_forward ; show_the_snake ;;
q) save_the_snake && at_exit ;; q) save_the_snake && at_exit ;;
esac esac
done done

Loading…
Cancel
Save