Skip to content

@Rhomboid /board.sh
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.