#!/bin/bash# Author: Mark Cardinal# Date: December 12th 2007# Program: pacman.sh### USAGE: ./pacman.sh [Options]## Options: -s NUM - where NUM is the delay in seconds before moving pacman# -o NUM - where NUM is a number between 40 - 47 representing the eye colour for Open Mouth pacman# -c NUM - where NUM is a number between 40 - 47 representing the eye colour for closed mouth pacman# -b NUM - where NUM is a number between 40 - 47 representing the body colour for pacman (later make option for open/closed mouth body pacman)# -R NUM - where NUM is a number greater than zero representing the number of rows to make pacman romp on the screen# -C NUM - where NUM is a number greater than zero representing the number of rows to make pacman romp on the screen# -r NUM - where NUM is a number greater than zero representing the number of rotations the program will run for, DEFAULT continuous# -h - prints this usageclearspeed=1 # number of seconds between drawing new packman (speed packman appears to move at)eyecolourA=45 # eye colour for Open mouth packmaneyecolourB=44 # eye colour for Closed mouth packmanbodycolour=43 # body colour of packmannumrows=2 # number of rows for pacman to traversenumcols=2 # number of columns for pacman to traverserotations=0 # number rotations the program runs for, DEFAULTS to 0 for infinitex=1 # x coordinate of the top left corner of the packman to draw/Erase in DrawPackman/Erase respectivelyy=1 # y coordinate of the top left corner of the packman to draw/Erase in DrawPackman/Erase respectivelywidth=18 # constant;height=9 # constant;mouthdirection=0 # 0 means draw the packman with mouth on the right side| 1 means draw the packman with mouth on the left sidemouthstatus=0 # 0 means draw with open mouth && eyecolourA | 1 means draw with closed mouth and eyecolourBfunction PrintColours(){ for((i=40;i<=47;i++)); do printf "\033[${30};${i}m${i}\033[0m " done}while getopts "hs:o:c:b:R:C:r:" flagdo case "$flag" in [s]) if (($OPTARG>0)); then speed=$OPTARG else printf " -s requires an integer argument greater than zero\n" exit 1 fi ;; [o]) if (($OPTARG>=40 && $OPTARG<=47)); then eyecolourA=$OPTARG else printf " -o requires an integer argument from 40 to 47\n" exit 1 fi ;; [c]) if (($OPTARG>=40 && $OPTARG<=47)); then eyecolourB=$OPTARG else printf " -c requires an integer argument from 40 to 47\n" exit 1 fi ;; [b]) if (($OPTARG>=40 && $OPTARG<=47)); then bodycolour=$OPTARG else printf " -b requires an integer argument from 40 to 47\n" exit 1 fi ;; [R]) if (($OPTARG>0)); then numrows=$OPTARG else printf " -R requires an integer argument greater than zero\n" exit 1 fi ;; [C]) if (($OPTARG>0)); then numcols=$OPTARG else printf " -C requires an integer argument greater than zero\n" exit 1 fi ;; [r]) if (($OPTARG>0)); then rotations=$OPTARG else printf " -r requires an integer argument greater than zero\n" exit 1 fi ;; [h]) printf "USAGE: ./pacman.sh [Options]\n\n" printf "Options: \n" printf "%5s -s NUM - where NUM is the delay in seconds before moving pacman\n" "" printf "%5s -o NUM - where NUM is a number between 40 - 47 representing the eye colour for Open Mouth pacman\n" "" printf "%5s -c NUM - where NUM is a number between 40 - 47 representing the eye colour for closed mouth pacman\n" "" printf "%5s -b NUM - where NUM is a number between 40 - 47 representing the body colour for pacman (later make option for open/closed mouth body pacman)\n" "" printf "%5s -R NUM - where NUM is a number greater than zero representing the number of rows to make pacman romp on the screen\n" "" printf "%5s -C NUM - where NUM is a number greater than zero representing the number of rows to make pacman romp on the screen\n" "" printf "%5s -r NUM - where NUM is a number greater than zero representing the number of rotations the program will run for, DEFAULT continuous\n" "" printf "%5s -h %3s - prints this usage\n" "" "" printf "%5s (Colours: " "" PrintColours printf " )\n\n" exit 0 ;; esacdonefunction DrawPackman(){ # copy x,y coordinates; y because it changes in the function and x just to make things look clean funcX=$x funcY=$y printf "\033[${funcY};${funcX}H"; printf "%6s\033[${bodycolour}m%8s\033[0m" " " " "; ((funcY++)) printf "\033[${funcY};${funcX}H"; if [ $mouthstatus -eq 0 ]; then # print with eye colour A printf "%4s\033[${bodycolour}m%5s\033[${eyecolourA}m%2s\033[${bodycolour}m%5s\033[0m" " " " " " "; else # print with eye colour B printf "%4s\033[${bodycolour}m%5s\033[${eyecolourB}m%2s\033[${bodycolour}m%5s\033[0m" " " " " " "; fi ((funcY++)) printf "\033[${funcY};${funcX}H"; printf "%2s\033[${bodycolour}m%16s\033[0m" "" " "; ((funcY++)) printf "\033[${funcY};${funcX}H"; if [ $mouthstatus -eq 0 ]; then # print top half of open mouth if [ $mouthdirection -eq 0 ]; then # print right facing pacman mouth printf "%1s\033[${bodycolour}m%12s\033[0m" "" " "; else # print left facing pacman mouth printf "%6s\033[${bodycolour}m%12s\033[0m" "" " "; fi else # print top half of closed mouth printf "%1s\033[${bodycolour}m%17s\033[0m" "" " "; fi ((funcY++)) printf "\033[${funcY};${funcX}H"; if [ $mouthdirection -eq 0 ]; then # print right facing pacman mouth printf "%1s\033[${bodycolour}m%9s\033[0m" "" " "; else # print left facing pacman mouth printf "%9s\033[${bodycolour}m%9s\033[0m" "" " "; fi ((funcY++)) printf "\033[${funcY};${funcX}H"; if [ $mouthstatus -eq 0 ]; then # print bottom half of open mouth if [ $mouthdirection -eq 0 ]; then # print right facing pacman mouth printf "%1s\033[${bodycolour}m%12s\033[0m" "" " "; else # print left facing pacman mouth printf "%6s\033[${bodycolour}m%12s\033[0m" "" " "; fi else # print bottom half of closed mouth printf "%1s\033[${bodycolour}m%17s\033[0m" "" " "; fi ((funcY++)) printf "\033[${funcY};${funcX}H"; printf "%2s\033[${bodycolour}m%16s\033[0m" "" " "; ((funcY++)) printf "\033[${funcY};${funcX}H"; printf "%4s\033[${bodycolour}m%12s\033[0m" "" " "; ((funcY++)) printf "\033[${funcY};${funcX}H"; printf "%6s\033[${bodycolour}m%8s\033[0m" " " " ";}function Erase(){ for((j=y;j<y+height;j++));do printf "\033[${j};${x}H%${width}s"; done}#*********************************************** MAIN ***************************************for((counter=0;counter<rotations||rotations==0;counter++)); do for((counterA=0;counterA<numrows;counterA++,y+=height,mouthdirection=!mouthdirection)); do if ((counterA % 2 == 0)); then for((x=0,counterB=0;counterB<numcols;counterB++,x+=width,mouthstatus=!mouthstatus)); do DrawPackman sleep $speed Erase done else for((x-=width,counterB=0;counterB<numcols;counterB++,x-=width,mouthstatus=!mouthstatus)); do DrawPackman sleep $speed Erase done fi done y=1done#*********************************************** END ***************************************printf "\n\n"