shell脚本打字游戏界面框架

本来有音乐来着,但是由于音乐文件没有,所以没有音乐,如果需要音乐,怎么后台播放,以及怎么去杀死这个进程,当时测试的时候加上去的,参照程序顶头注释部分,前提条件:装好了相应的播放器,准备好了相应的音乐文件。此代码保存到xxx.sh文件即可运行 

#!/bin/bash
#declare -i mm
#nohup play 刚刚好.flac &
#pgrep play > 1.txt
#mm=$(sed -n '1p' 1.txt)
#kill $mm

############################################################
#下面的代码不要动
#游戏代码请在函数menu里面添加,函数里面有注释指明代码添加的位置
#排行榜的代码在函数paihb_menu里面添加,函数里面有注释指明代码添加的位置
#一级菜单标志位
declare -i flag
flag=0
############################################################
#该函数为退出显示界面 不要操作!!!
function end()
{
    declare -r str='
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000222222220000000000000000000002222222200000000000000000000
0000000000000000002200000002200000000000000000220000000022000000000000000000
0000000000000000022000000000220000000000000002200000000002200000000000000000
0000000000000000220002222000022000000000000022000022220000220000000000000000
0000000000000002200022002200002222222222222220000220022000022000000000000000
0000000000000000220022000220000220000000002200002200022000220000000000000000
0000000000000000022002200022222200000000000222222000220002200000000000000000
0000000000000000002222200022222200200000200222222000222220000000000000000000
0000000000000000000000000022222200000000000222222000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000002200000000000220000000000000000000000000000000
0000000000000000000000000000000220000000002200000000000000000000000000000000
0000000000000000000000000000000022222222200000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000'

    declare -i j=0
    declare -i row=2
    declare -a cloro=(31 32 33 34 35 36 37)
    declare -i a1=0
    line_char_count=77
    #################
    declare -i width
    declare -i high
    declare -i k=0
    declare -i n=37
    declare -i n1=30
    declare -i nn
    declare -i nm
    width=79
    high=23
    clear
    echo -e "\033[31;40m"
    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=1; j<=$high; j=j+1))
        do
            echo -e "\033["$j";"$i"H "
        done
    done

    for (( nn=0; nn<=5; nn=nn+1))
    do
        echo -e "\033[40m"
        echo -e "\033[?25l"
        if [ $k = 0 ];then
        n=37
        n1=30
        else
        n=30
        n1=37
       fi

    for (( i=1; i<=$width; i=i+1 ))
    do
        echo -e "\033[1;"$i"H="
        echo -e "\033["$high";"$i"H="
    done
    for (( i=2; i<=$high-1; i=i+1 ))
    do
        echo -e "\033["$i";1H||"
        echo -e "\033["$i";"$width"H||\n"
    done
#########################

    echo -ne "\033[2;3H"
    for ((i=0; i<${#str}; i++));do
        # 换行
        if [ "$[i%line_char_count]" == "0" ] && [ "$i" != "0" ];then
            row=$row+1
            echo -ne "\033["$row";3H"
        fi
    if [ "${str:$i:1}" == "0" ];then
            echo -ne " "
        elif [ "${str:$i:1}" == "1" ];then
            echo -ne "\033["$n"m."
        elif [ "${str:$i:1}" == "*" ];then
            echo -ne "\033["$n1"m*"
        elif [ "${str:$i:1}" == "2" ];then
            echo -ne "\033["${cloro[$a1]}"mo"
        fi

    done
nm=5-$nn
echo -e "\033[22;22H\033["${cloro[$a1]}"m感谢使用,再见!游戏即将退出,剩余$nm秒"
echo -ne "\033[2;3H"
        row=2
        sleep 1
        k=~k
        if (( a1 < 6 ));then
        a1=$a1+1
        else
        a1=0
        fi
done
}
function log()
{
    declare -r str='
00000000000000000000000000000000000000
00000000000000000000000000000000000000
00000000000000000000000000000000000000
00000000000000000000000000000000000000
00000222222000000000000000022222200000
00022000002200000000000000220000022000
02200022000220022222222002200022000220
22002200220002200000000220002200220022
02202200022222200000000222222000220220
00220220000002000000000020000002202200
00022220000002020000002020000002222000
00000000000002000000000020000000000000
00000000000002000000000020000000000000
00000000000002000000000020000000000000
00000000000002000000000020000000000000
00000000000000200000000200000000000000
00000000000000022222222000000000000000
00000000000000000000000000000000000000
00000000000000000000000000000000000000
00000000000000000000000000000000000000
00000000000000000000000000000000000000'

    declare -i j=0
    declare -i row=2
    declare -i a1=0
    line_char_count=39
    #################
    declare -i width
    declare -i high
    declare -i k=0
    declare -i n=37
    declare -i n1=30
    declare -i nn
    declare -i nm
    declare -i b
    width=79
    high=23
    clear
    echo -ne "\033[1;1H"
    echo -e "\033[31;40m"

    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=1; j<=18; j=j+1))
        do
            echo -e "\033[43m\033["$j";"$i"H \033[40m"
        done
    done
    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=19; j<=23; j=j+1))
        do
            echo -e "\033[42m\033["$j";"$i"H \033[40m"
        done
    done
    echo -e "\033[1;1H=\033["$high";1H=\033[1;"$width"H=\033["$high";"$width"H=\033[40m"

        echo -e "\033[?25l"
        if [ $k = 0 ];then
        n=37
        n1=30
        else
        n=30
        n1=37
       fi

    for (( i=1; i<=$width; i=i+1 ))
    do
        echo -e "\033[1;"$i"H="
        echo -e "\033["$high";"$i"H="
    done
    for (( i=2; i<=$high-1; i=i+1 ))
    do
        echo -e "\033["$i";1H||"
        echo -e "\033["$i";"$width"H||\n"
    done
#########################
    echo -e "\033[1;80H \033[40m"
    echo -e "\033[23;80H \033[40m"
    echo -ne "\033[2;3H"
    for ((i=0; i<${#str}; i++));do
        # 换行
        if [ "$[i%line_char_count]" == "0" ] && [ "$i" != "0" ];then
            row=$row+1
            echo -ne "\033["$row";3H"
        fi
	if (( row < 18 ));then
	b=43
	else
	b=42
	fi
        # 判断前景字符和背景字符
    if [ "${str:$i:1}" == "0" ];then
            echo -ne "\033["$b"m \033[40m"
        elif [ "${str:$i:1}" == "2" ];then
            echo -ne "\033[30;"$b"mo\033[40m"
        fi
    done
	echo -ne "\033[1;1H"
}

#该函数为欢迎显示界面 不要操作
function welcome()
{
    declare -r str='
0000000000100000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000010000000000000000000000000000000*00000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000*000000000000000000000000000000000000000000000000000000000000
0000001000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000100000000000000000000000000000000000000000000000*00
00000000000000000000000000000000000000000000*0000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000100000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000*0000000000000000000000000000000000000001000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000002000000020222222202000000000222200002222200002200000220222222200000000
0000002000200020200000002000000220000000220000022002020002020200000000000000
0000002000200020222222202000000220000000220000022002000220020222222200000000
0000000202020200200000002000000220000000220000022002000000020200000000000000
0000000020002000222222202222220000222200002222200002000000020222222200000000
0000000000000000000000000000000000000000000000000000000000000000000000000000'
    declare -i j=0
    declare -i row=2
    declare -a cloro=(31 32 33 34 35 36 37)
    declare -i a1=0
    line_char_count=77
    #################
    declare -i width
    declare -i high
    declare -i k=0
    declare -i n=37
    declare -i n1=30
    declare -i nn
    declare -i nm
    width=79
    high=23
    clear
    echo -ne "\033[1;1H"
    echo -e "\033[31;40m"
    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=1; j<=$high; j=j+1))
        do
            echo -e "\033["$j";"$i"H "
        done
    done

    
    for (( nn=0; nn<=7; nn=nn+1))
    do
	echo -e "\033[40m"
	echo -e "\033[?25l"
        if [ $k = 0 ];then
        n=37
	n1=30
        else
        n=30
	n1=37
       fi
    
    for (( i=1; i<=$width; i=i+1 ))
    do
        echo -e "\033["${cloro[$a1]}"m\033[1;"$i"H=\033[40m"
        echo -e "\033["${cloro[$a1]}"m\033["$high";"$i"H=\033[40m"
    done
    for (( i=2; i<=$high-1; i=i+1 ))
    do
        echo -e "\033["${cloro[$a1]}"m\033["$i";1H||\033[40m"
        echo -e "\033["${cloro[$a1]}"m\033["$i";"$width"H||\033[40m\n"
    done
#########################

    echo -ne "\033[2;3H"
    for ((i=0; i<${#str}; i++));do
        # 换行
        if [ "$[i%line_char_count]" == "0" ] && [ "$i" != "0" ];then
            row=$row+1
            echo -ne "\033["$row";3H"
        fi	
    if [ "${str:$i:1}" == "0" ];then
            echo -ne " "
        elif [ "${str:$i:1}" == "1" ];then
            echo -ne "\033["$n"m.\033[40m"
        elif [ "${str:$i:1}" == "*" ];then
            echo -ne "\033["$n1"m*\033[40m"
        elif [ "${str:$i:1}" == "2" ];then
            echo -ne "\033["${cloro[$a1]}"mo\033[40m"
        fi
    done
if (( nn < 2 ));then
echo -e "\033[22;35H\033["${cloro[$a1]}"m初始化中:...\033[40m"
else
nm=$nn-2
nm=$nm*20
echo -e "\033[22;35H\033["${cloro[$a1]}"m已加载:$nm%\033[40m"
fi
echo -ne "\033[2;3H"
        row=2
        sleep 0.5
        k=~k
	if (( a1 < 6 ));then
	a1=$a1+1
	else
	a1=0
	fi
done
echo -ne "\033[1;1H"
}
#该函数为背景及边框打印函数
function show()
{
    declare -i width
    declare -i high
    width=79
    high=23

    clear

    echo -e "\033[31;40m"
    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=1; j<=18; j=j+1))
        do
            echo -e "\033[40m\033["$j";"$i"H "
        done
    done

    echo -e "\033[40m\033[1;1H=\033["$high";1H=\033[1;"$width"H=\033["$high";"$width"H=" 
    for (( i=2; i<=$width-1; i=i+1 ))
    do
        echo -e "\033[1;"$i"H="
        echo -e "\033["$high";"$i"H="
    done
    for (( i=2; i<=$high-1; i=i+1 ))
    do
        echo -e "\033["$i";1H||"
        echo -e "\033["$i";"$width"H||\n"
    done
}
#模式选择菜单 该函数带一个参数
#送入的参数表示选择的模式
#例如:menu 1 表示进入简单模式
#  menu 2 表示进入困难模式
#  menu 3 表示进入无尽模式
#NOTE:   游戏代码请在此函数内添加
function menu()
{
declare -i flag2
flag2=0
log
if [ $flag2 = 0 ];then
	if [ "$1" == "1" ];then
	echo -e "\033[4;52H\033[37;41m简单模式"
	elif [ "$1" == "2" ];then
	echo -e "\033[4;52H\033[37;41m困难模式"
	elif [ "$1" == "3" ];then
	echo -e "\033[4;52H\033[37;41m无尽模式"
	fi
        echo -e "\033[8;42H\033[37;44m字母练习....................1\033[37;40m"
        echo -e "\033[10;42H\033[37;44m数字练习....................2\033[37;40m"
        echo -e "\033[12;42H\033[37;44m单词练习....................3\033[37;40m"
        echo -e "\033[14;42H\033[37;44m返回........................4\033[37;40m"
        echo -ne "\033[43m\033[16;45Hplease input you choic: "
        read choic
        elif [ $flag2 = 1 ];then
                echo "game start"
        fi
        case $choic in
        1)
        show
        if (whiptail --title "NOTE" --yesno "是否进行游戏?" 10 60)then
                {
                flag2=1

                for ((i=0;i<=100;i+=20));
                        do
                        sleep 0.5
                        echo $i 
                        done
                }|whiptail --gauge "加载中,请稍后....." 6 60 0

                while [ $flag2 ]
                do
		show
		if [ "$1" == "1" ];then
                ####################################################################在此之间添加代码 示例如下
                      #  echo -e "\033[14;28H$字母简单模式"
		declare -i i 
                declare -a arr=(a b c d e f g h)
                        for((k=0;k<5;k++))
                        do
                        i=$RANDOM%8
                        al=${arr[$i]}
                        str=$str$al
                        done
                        echo -e "\033[14;28H$str"
                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
			if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi

		fi
		
		
		if [ "$1" == "2" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$字母困难模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi

		if [ "$1" == "3" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$字母无尽模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi

                done
                else
                flag2=0
                fi;;
        2)
        show
        if (whiptail --title "NOTE" --yesno "是否进行游戏?" 10 60)then
                {
                flag2=1

                for ((i=0;i<=100;i+=20));
                        do
                        sleep 0.5
                        echo $i 
                        done
                }|whiptail --gauge "加载中,请稍后....." 6 60 0

                while [ $flag2 ]
                do
                show
		if [ "$1" == "1" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$数字简单模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi


                if [ "$1" == "2" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$数字困难困难"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi
		
		if [ "$1" == "3" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$数字无尽模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi
                done
        else
                flag2=0
        fi;;
        3)
        show
        if (whiptail --title "NOTE" --yesno "是否进行游戏?" 10 60)then
                {
                flag2=1

                for ((i=0;i<=100;i+=20));
                        do
                        sleep 0.5
                        echo $i 
			done
                }|whiptail --gauge "加载中,请稍后....." 6 60 0

                while [ $flag2 ]
                do
                show
		if [ "$1" == "1" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$单词简单模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi

		if [ "$1" == "2" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$单词困难模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi

                if [ "$1" == "3" ];then
                ####################################################################在此之间添加代码 示例如下
                        echo -e "\033[14;28H$单词无尽模式"

                ####################################################################下面的代码用于返回菜单 
                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                fi

                done
        else
                flag2=0
        fi;;

        4|q|Q)#退出
        clear
        flag=0;;
        *)#错误处理
        whiptail --title "ERROR" --msgbox "错误选项 请重新选择" 10 60;;
        esac

}
#排行榜菜单
function paihb_menu()
{
declare -i flag3
flag3=0
log
 if [ $flag3 = 0 ];then
	echo -e "\033[4;52H\033[37;41m排行榜"
        echo -e "\033[8;42H\033[37;44m简单模式排行榜..............1\033[37;40m"
        echo -e "\033[10;42H\033[37;44m困难模式排行榜..............2\033[37;40m"
        echo -e "\033[12;42H\033[37;44m无尽模式排行榜..............3\033[37;40m"
        echo -e "\033[14;42H\033[37;44m返回........................4\033[37;40m"
        echo -ne "\033[43m\033[16;45Hplease input you choic: "
        read choic
        fi
        case $choic in
        1)#简单模式排行榜代码位置
		clear
		show
                while [ $flag3 ]
                do
                show
                 ######################################################################在此添加游戏代码
                echo -e "\033[10;28H简单模式排行榜"

                ######################################################################以下代码用于返回上一级菜单



                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                done;;
        2)#困难模式排行榜代码位置
		clear
		show
                while [ $flag3 ]
                do
                show
                 ######################################################################在此添加游戏代码
                echo -e "\033[10;28H困难模式排行榜"

                ######################################################################以下代码用于返回上一级菜单



                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
			if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                done;;
        3)#无尽模式排行榜代码位置
		clear
		show
                while [ $flag3 ]
                do
                show
                 ######################################################################在此添加游戏代码
                echo -e "\033[10;28H无尽模式排行榜"

                ######################################################################以下代码用于返回上一级菜单



                        echo -ne "\033[40m\033[22;28Hplease input mk (q/Q exit):"
                        read mk
                        if [ $mk = q ]||[ $mk = Q ];then
                                break
                        fi
                done;;
        4|q|Q)#退出
        clear
        flag=0;;
        *)#错误处理
        whiptail --title "ERROR" --msgbox "错误选项 请重新选择" 10 60;;
        esac

}
############################################################
#下面的代码不要动
#游戏代码请在函数menu里面添加,函数里面有注释指明代码添加的位置
#排行榜的代码在函数paihb_menu里面添加,函数里面有注释指明代码添加的位置
#主函数
echo -e "\033[37;40m"
if (whiptail --title "欢迎使用" --yesno \
"	本游戏为打字游戏,可以进行各种打字练
	习。为保护你的视力,建议游戏时间两个
	小时以下,谢谢配合!
	注:解释权归制作组所有
	是否同意以上用户规定?(请选择 yes/no)" 11 60)then
clear
welcome
echo -e "\033[?25h"
while true
do
clear
log
if [ $flag = 0 ];then
echo -e "\033[3;52H\033[37;41m主菜单"
echo -e "\033[6;42H\033[37;44m简单模式....................1\033[37;40m"
echo -e "\033[8;42H\033[37;44m困难模式....................2\033[37;40m"
echo -e "\033[10;42H\033[37;44m无尽模式....................3\033[37;40m"
echo -e "\033[12;42H\033[37;44m排行榜......................4\033[37;40m"
echo -e "\033[14;42H\033[37;44m系统设置....................5\033[37;40m"
echo -e "\033[16;42H\033[37;44m关于我们....................6\033[37;40m"
echo -e "\033[18;42H\033[37;44m游戏说明....................7\033[37;40m"
echo -e "\033[20;42H\033[37;44m退出游戏................8|q|Q\033[37;40m"
echo -ne "\033[42m\033[22;45Hplease input you choice: "
read choice
fi
case $choice in
1)#简单模式菜单
	clear
	flag=1
	if [ $flag = 1 ];then
	menu $choice
	fi;; 
2)#困难模式菜单
	clear
	flag=1
	if [ $flag = 1 ];then
        menu $choice
	fi;;
3)#无尽模式菜单       
	clear
	flag=1
	flag=1
	if [ $flag = 1 ];then
	menu $choice
	fi;;
4)#排行榜菜单
	clear
	flag=1
	if [ $flag = 1 ];then
	paihb_menu
	fi;;	
5)#系统音量菜单
#	clear
	alsamixer;;
6)#关于我们
#	clear
	whiptail --title "关于我们" --msgbox \
"
制作组名单 : xxxx
	      xxxx
	      xxxx
	      xxxx
	      xxxx
	      xxxx
	      xxxx
	      xxxx
	      xxxx

特别鸣谢:Teacher li
	" 20 60
	;;
7)#版本说明
	whiptail --title "游戏说明" --msgbox \
	"
该游戏是我们开发的一款打字游戏,含有多项练习功能
,有字符练习、数字练习、单词练习;这三种练习分别
设有简单模式、困难模式、无尽模式;菜单里的排行榜
,可以对不同的模式及练习进行分数排行;且支持对
游戏音量的设置。
" 15 60;;
8|q|Q)#退出
	clear
	if (whiptail --title "EXIT" --yesno "是否退出游戏?" 10 60)then
                flag=1
		end
	        clear
		echo -e "\033[30;47m"
                exit
        else
                flag=0
        fi;;
*)#错误处理
	if [ $choice ];then
	whiptail --title "ERROR" --msgbox "错误选项 请重新选择" 10 60
	else
		flag=0
	fi;;
esac
done
else
	echo -e "\033[30;47m"
	exit
fi	

shell脚本打字游戏界面框架_第1张图片

shell脚本打字游戏界面框架_第2张图片

shell脚本打字游戏界面框架_第3张图片

shell脚本打字游戏界面框架_第4张图片

shell脚本打字游戏界面框架_第5张图片

shell脚本打字游戏界面框架_第6张图片

shell脚本打字游戏界面框架_第7张图片

shell脚本打字游戏界面框架_第8张图片

shell脚本打字游戏界面框架_第9张图片

shell脚本打字游戏界面框架_第10张图片

shell脚本打字游戏界面框架_第11张图片

 

 

 

 

 

你可能感兴趣的:(shell脚本编程)