Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Last active December 11, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rhomboid/4577308 to your computer and use it in GitHub Desktop.
Save Rhomboid/4577308 to your computer and use it in GitHub Desktop.
simple 2D text game movement in bash
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment