cli-games/bashit/bashit

243 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
show_scoreboard()
{
local boardName=".$(basename "$0").scoreboard"
if [ -f "$boardName" ] ; then
clear
echo -e "\nHigh Scores\n"
cat $boardName
local continue
read -n 1 -p "Press any key to continue: " continue
echo ""
fi
}
high_score()
{
local boardName=".$(basename "$0").scoreboard"
local newHighScore=0
local i=1
if [ ! -f "$boardName" ] ; then
local newHighScore=1
while [ $i -le 10 ] ; do
local score[$i]="0 anonymous"
let i++
done
else
while IFS=$'\n' read line ; do
local score[$i]="$line"
local oldScore=$(echo "${score[$i]}" | cut -d ' ' -f 1)
if [ $1 -gt $oldScore -a $newHighScore == 0 ] ; then
local newHighScore=$i
fi
let i++
done < $boardName
fi
if [ $newHighScore -gt 0 ] ; then
local continue="n"
echo "Congratulations! You beat the score at position $newHighScore!"
while [ "${continue^}" != "Y" ] ; do
read -ep "Enter your name: " name
if [ -z "$name" ] ; then
local name="Anonymous"
fi
read -n 1 -p "is $name correct? " continue
done
if [ $newHighScore -eq 1 ] ; then
read -n 1 -p "Post this score using TTYtter? " continue
if [ "${continue^}" == "Y" ] ; then
ttytter -readline=0 -silent -status="I just got first place in #$0 on my local scoreboard with $1 points! #storm-games" &> /dev/null&
echo "Score posted."
fi
fi
i=11
while [ $i -gt $newHighScore ] ; do
score[$i]="${score[$(echo "$i - 1" | bc)]}"
let i--
done
local name="$(echo "${name^}" | tr " " "_")"
local score[$newHighScore]="$1 $name"
local i=1
local scoreBoard=""
while [ $i -le 10 ] ; do
local scoreBoard="${scoreBoard}${score[$i]}\n"
let i++
done
echo -e "${scoreBoard:0:-2}" > $boardName
fi
}
show_instructions()
{
cat << EOF
To play, first choose the number of the sound pack you want.
When the game starts, use iether s or j for the left, d or k for the center, or l or f for the right.
To add sound packs, add a directory with the name of the soundpack under the sounds directory.
cpu and player.ogg sounds should be aprox .25 seconds in linkgth. Reward and lose sounds are optional.
Background sounds are optional too.
EOF
}
play_stop_bgm()
{
if [ "$1" == "play" ] ; then
if [ -f "$2/bgm.ogg" ] ; then
play -qV0 $2/bgm.ogg repeat 9999&
__bgmPID="$(echo "$!")"
fi
elif [ "$1" == "stop" ] ; then
if [[ "$__bgmPID" =~ ^[0-9]+$ ]] ; then
kill $__bgmPID
unset __bgmPID
fi
fi
}
select_soundpack()
{
local __soundPackVariable=$1
local i=1
for x in sounds/* ; do
if [ -d "$x" ] ; then
local soundPackList[$i]="$x"
if [ $i -lt 10 ] ; then
echo -n "0"
fi
echo "$i: $(basename "$x")"
let i++
fi
done
local continue="false"
while [ "$continue" == "false" ] ; do
local continue="true"
read -p "Select a soundpack: " soundpack
if ! [[ "$soundpack" =~ ^[0-9]+$ ]] ; then
local continue="false"
else
if ! [[ "$soundpack" =~ [1-9]+$ ]] ; then
local continue="false"
fi
if [ $soundpack -lt 1 -o $soundpack -gt $(($i - 1)) ] ; then
local continue="false"
fi
fi
done
if [[ $__soundPackVariable ]] ; then
eval $__soundPackVariable="'${soundPackList[$soundpack]}'"
else
echo "${soundPackList[$soundPack]}"
fi
}
play_bopit_direction()
{
local soundpack=$1
local __directionVariable=$2
direction="$(shuf -n 1 -e "center" "left" "right")"
case "$direction" in
"center")
play -qV0 $(shuf -n 1 -e $soundpack/cpu*ogg) remix v0.9 v0.9&
;;
"left")
play -qV0 $(shuf -n 1 -e $soundpack/cpu*ogg) remix v0.9 v0.1&
;;
"right")
play -qV0 $(shuf -n 1 -e $soundpack/cpu*ogg) remix v0.1 v0.9&
;;
esac
if [[ $__directionVariable ]] ; then
eval $__directionVariable="'$direction'"
else
echo "$direction"
fi
}
#Main Game Loop
while [ -z "$answer" ] ; do
read -n 1 -p "Would you like instructions? " answer
if [ "${answer^}" == "Y" ] ; then
show_instructions
read -n 1 -p "Press any key to continue. " answer
fi
done
gameLoop="true"
while [ "$gameLoop" == "true" ] ; do
clear
select_soundpack sound
speed="1.50"
continue="true"
if [ -f "$sound/intro.ogg" ] ; then
play -qV0 $sound/intro.ogg
else
echo "Ready"
echo
sleep 1
echo "set"
echo
sleep 1
echo "go"
fi
play_stop_bgm play $sound
difficultyIndex=0
score=0
while [ "$continue" == "true" ] ; do
if [ $difficultyIndex -ge 2 ] ; then
difficultyIndex=0
if [ "$speed" != "0.01" ] ; then
speed="$(echo "scale=2;$speed - .01" | bc)"
fi
fi
play_bopit_direction $sound bopDirection
read -st $speed -n 1 key
case "${key^}" in
"S" | "J")
key="left"
;;
"D" | "K")
key="center"
;;
"F" | "L")
key="right"
;;
esac
if [ "$key" != "$bopDirection" ] ; then
continue="false"
else
let score++
case "$key" in
"center")
play -qV0 $(shuf -n 1 -e $sound/player*ogg) remix v0.9 v0.9
;;
"left")
play -qV0 $(shuf -n 1 -e $sound/player*ogg) remix v0.9 v0.1
;;
"right")
play -qV0 $(shuf -n 1 -e $sound/player*ogg) remix v0.1 v0.9
;;
esac
if [ $(($score % 15)) -eq 0 ] ; then
if [ -f "$sound/reward.ogg" ] ; then
play -qV0 $(shuf -n 1 -e $sound/reward*ogg)
fi
fi
fi
let difficultyIndex++
done
play_stop_bgm stop
if [ -f "$sound/lose.ogg" ] ; then
play -qV0 $(shuf -n 1 -e $sound/lose*ogg)
fi
echo
echo "Score: $score."
high_score $score
show_scoreboard
read -n 1 -p "Play again? " answer
echo
if [ "${answer^}" != "Y" ] ; then
gameLoop="false"
fi
done
exit 0