Permalink
Browse files

MVP! TOTALLY WORKS SHIP IT

  • Loading branch information...
1 parent 5e3281e commit ff3260e1dd5fa61695310578091ff922acf06e71 @Xion committed Aug 22, 2016
Showing with 20 additions and 10 deletions.
  1. +20 −10 flappybash.sh
View
@@ -9,14 +9,15 @@
W=`tput cols`
H=`tput lines`
+W2=$[W/2]
H2=$[H/2]
H4=$[H/4]
DT=0.05 # 20 FPS
G=2 # minimum height of pipe gap (is double this number)
KEY=. # last pressed key (dot is ignored)
-X=3
+X=2
Y=$H2
VY=0 # vertical velocity
AY=100 # vertical acceleration
@@ -25,11 +26,10 @@ DEAD=0
P=( ) # start as empty array
PX='' # pipe's current X
PVX='-100'
+S=0 # score
tick() {
- if [ $DEAD -gt 0 ]; then exit; fi
-
# Update
########
@@ -40,23 +40,30 @@ tick() {
esac
VY=`_ "$VY+($AY*$DT)"`
- Y=`_ "y=$Y+($VY*$DT);if(y<0)0 else if(y>$H)$H else y"`
+ Y=`_ "y=$Y+($VY*$DT);if(y<0)0 else if(y>$H)$H else y"`; tY=`t $Y`
PX=`_ "$PX+($PVX*$DT)"`; PX=`t $PX`
- if [ $PX -le 0 ]; then np; fi
+ # the -le check below is bad and I should feel bad, but doing it properly
+ # (i.e. keeping previous and current value of PX, and checking if X is in between)
+ # probably won't fit within the limit; oh well, maybe I'll try that later
+ if [ $PX -le $X ] && [ ${P[tY]} = '#' ]; then DEAD=1; quit; fi
+ if [ $PX -le 0 ]; then S=$[S+1]; np; fi
# Draw
######
clear
# draw the pipe
- for ((i=0; i<$H; i++)); do
+ for ((i=1; i<=$H; i++)); do
echo -en "\e[$i;${PX}f\e[1;32;49m${P[i]}\e[0m" # bold green-on-default
done
- # draw the player (bold white-on-red)
- echo -en "\e[`t $Y`;${X}f\e[1;37;41mB\e[0m"
+ # draw the player
+ echo -en "\e[$tY;${X}f\e[1;37;41mB\e[0m" # bold white-on-red
+
+ # draw the score
+ echo -en "\e[2;`_ "$W2-5"`f\e[1;37;49mScore: $S\e[0m" # bold white-on-default
# schedule the next call of this function
( sleep $DT; kill -ALRM $$ ) &
@@ -68,17 +75,20 @@ np() {
local l=$[H2 + G + RANDOM % (H2-H4-G)] # lower pipe is >= this one
P=( )
- for ((i=0; i<$u; i++)) do P[i]='#'; done
+ for ((i=1; i<$u; i++)) do P[i]='#'; done
for ((i=$u; i<$l; i++)) do P[i]=' '; done
- for ((i=$l; i<$H; i++)) do P[i]='#'; done
+ for ((i=$l; i<=$H; i++)) do P[i]='#'; done
PX=$[W-1] # start from the right side
}
quit() {
+ trap : ALRM
printf "\e[?12l\e[?25h" # cursor on
tput rmcup
echo -en "\e[0m"
clear
+ if [ $DEAD -gt 0 ]; then printf "score:$S\ngit gud\n"; fi
+ exit
}

0 comments on commit ff3260e

Please sign in to comment.