Last active
December 11, 2015 08:59
-
-
Save Rhomboid/4577308 to your computer and use it in GitHub Desktop.
simple 2D text game movement in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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