1
使用shell编写打字游戏
source link: https://www.lujun9972.win/blog/2017/06/29/%E4%BD%BF%E7%94%A8shell%E7%BC%96%E5%86%99%E6%89%93%E5%AD%97%E6%B8%B8%E6%88%8F/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
使用shell编写打字游戏
获取随机数的方式
$RANDOM
变量内容的截取
${variable:position:length}
bash中的数组操作
array_name=(value0 value1 value2 value3)
declare -a array_name
array_name[0]=value0 array_name[1]=value1 array_name[n]=valuen
valuen=${array_name[n]}
获取数组长度
length=${#array_name[@]}
unset array[1] # 删除数组中第一个元素 unset array # 删除整个数组
read -n 1 -t 2
定义游戏参数
width=70 height=10 interval=1 while getopts w:h:i: ARG do case $ARG in w) width=$OPTARG ;; h) height=$OPTARG ;; i) interval=$OPTARG ;; *) echo "Unkown options" exit 1 esac done shift $(($OPTIND-1))
定义初始内容
for (( i=0;i<$width;i++ )) do blank_line="$blank_line " done for ((i=0;i<$height;i++)) do lines[$i]=$blank_line # 全局变量 done
生成随机的字符
function generate_random_char() { full_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" char_position=$(($RANDOM%62)) char=${full_chars:$char_position:1} echo $char } function insert_random_char_into_line() { local line=$1 local position=$2 local char=$3 prefix_line=${line:0:$position} suffix_line=${line:$(($position+1))} echo "$prefix_line$char$suffix_line" } function generate_new_line() { char=$(generate_random_char) position=$(($RANDOM%$width)) new_line=$(insert_random_char_into_line "$blank_line" $position $char) echo "$new_line" }
function refresh_ui() { clear for ((i=0;i<$height;i++)) do echo "${lines[$i]}" done }
读取输入的字符
function get_input() { local input read -n 1 -t 0.1 input echo $input }
消除匹配的字符
function remove_input_char() { local char=$1 for((i=0;i<$height;i++)) do line=${lines[$i]} line=$(echo "$line"|sed -e "s/$char/ /g") lines[$i]=$line done }
function screen_down() { for ((i=$(($height-1));i>0;i--)) do lines[$i]=${lines[$(($i-1))]} done lines[0]=$(generate_new_line) refresh_ui }
refresh_ui while : do old_date=$(date +"%s") input=$(get_input) if [ "$input" != "" ];then remove_input_char $input refresh_ui fi new_date=$(date +"%s") if [ $(($new_date-$old_date)) -ge $interval ];then screen_down fi done
注意要点:
- 读取数组元素的语法是${array[0]}而不能写成$array[0]
- 字符串变量作为参数时,请一定记得放在""里面,否则前面的空格会丢失
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK