Skip to content

Instantly share code, notes, and snippets.

@cgsdev0
Last active April 3, 2025 15:59
Show Gist options
  • Save cgsdev0/437644f71b737323f7a3c7d95ff545af to your computer and use it in GitHub Desktop.
Save cgsdev0/437644f71b737323f7a3c7d95ff545af to your computer and use it in GitHub Desktop.
i hope you're happy
#!/usr/bin/env bash
wx=24
wy=9
px=18
py=4
cy=4
cx=1
jumping=
score=0
score_offset=19
clear
tput civis
restore() {
tput cnorm
}
ground=( \
" . . . . ." \
" . . . . ." \
" . . . . " \
)
gh="${#ground[@]}"
gw="${#ground[0]}"
gy=$((wy-gh-1))
t=0
jt=0
trap restore EXIT
draw() {
local drew_cactus=
local drew_dino=
printf "\e[%sA\e[%sD" "$((wy+1))" "$((wx+2))"
for ((y=0; y<=wy; y++)); do
for ((x=0; x<=wx; x++)); do
c=' '
wx1=$((wx-1))
cx1=$((cx+1))
px1=$((px+1))
if [[ x -eq cx && y -eq cy && x -lt wx1 ]]; then
c="🌡"
drew_cactus=1
# emojis are 2 wide, compensate
elif [[ x -eq cx1 && y -eq cy && -n $drew_cactus ]]; then
c=""
elif [[ x -eq px && y -eq py ]]; then
if [[ px1 -ne cx || cy -ne py ]]; then
c="πŸ¦–"
drew_dino=1
fi
# emojis are 2 wide, compensate
elif [[ x -eq px1 && y -eq py && -n $drew_dino ]]; then
c=""
fi
if [[ y -eq gy ]]; then
c='-'
fi
if [[ y -gt gy ]]; then
modded=$(((x - t) % gw))
id=$((y-gy-1))
c="${ground[$id]}"
c="${c:$modded:1}"
fi
if [[ y -eq 0 ]]; then
c='─'
if [[ x -eq 0 ]]; then
c='β”Œ'
elif [[ x -eq wx ]]; then
c='┐'
fi
elif [[ y -eq wy ]]; then
c='─'
if [[ x -eq 0 ]]; then
c='β””'
elif [[ x -eq wx ]]; then
c='β”˜'
fi
elif [[ x -eq 0 ]]; then
c='β”‚'
elif [[ x -eq wx ]]; then
c='β”‚'
fi
printf "%s" "$c"
done
printf " \n"
done
printf "Score: %s " $score
if [[ -z $drew_dino ]]; then
game_over
fi
}
game_over() {
echo " Game Over!"
exit 0
}
while true; do
read -r -s -N1 -t0.1 input && sleep 0.1
if [[ ${input:0:1} == " " && -z $jumping ]]; then
jumping=true
jt=0
py=3
fi
if [[ -n $jumping ]]; then
if [[ $jt -eq 1 ]]; then
py=2
elif [[ $jt -eq 2 ]]; then
py=3
fi
if [[ $jt -ge 3 ]]; then
py=4
jumping=
fi
fi
cx=$((t % wx + 1))
draw
((t++))
((jt++))
if (( (t - score_offset) % wx == 0)); then
((score++))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment