public
Last active

simple 2D text game movement in bash

  • Download Gist
board.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/bin/bash
 
cols=14
rows=7
emptychar=O
playerchar=X
 
repeats=('')
for ((i=1; i <= $cols; i++)); do
spaces=$(printf "%${i}s" ' ')
repeats[$i]=${spaces// /$emptychar}
done
clear=$(tput clear)
 
current_row=$((rows / 2))
current_col=$((cols / 2))
 
printboard()
{
echo -e "${clear}q=quit h=left j=down k=up l=right\n"
local r
for ((r=0; r < $current_row; r++)); do
echo "${repeats[$cols]}"
done
printf "%s%c%s\n" "${repeats[$current_col]}" "$playerchar" "${repeats[$cols - $current_col - 1]}"
for ((r=$current_row + 1; r < $rows; r++)); do
echo "${repeats[$cols]}"
done
}
 
while :; do
printboard
read -s -N 1 command
case $command in
h) (( $current_col )) && let current_col-- ;;
j) (( $current_row != $rows - 1 )) && let current_row++ ;;
k) (( $current_row )) && let current_row-- ;;
l) (( $current_col != $cols - 1 )) && let current_col++ ;;
q) exit
esac
done

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.