您当前的位置: 首页 >  游戏

江湖有缘

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

shell脚本实战之坦克大战小游戏

江湖有缘 发布时间:2022-05-18 14:24:35 ,浏览量:0

shell脚本实战之坦克大战小游戏
  • 一、脚本内容
  • 二、运行脚本

一、脚本内容
#Author:jeven
#time:Wed 18 May 2022 01:27:57 PM CST
#filename:game_tanke.sh
#Script description:
#######################################



#place temporary files
tmpdir='/tmp'
#u:up d:down l:left r:right
boundary_u=2
boundary_d=26
boundary_l=3
boundary_r=80
boundary_color=45
smallboundary_color=34
#
#about time (second)
#about color (x0:black x1:red  x2:green x3:Orange x4:blue x5:pink x6:light blue) x=[3|4]
#about tank
#if enemy_tank_color is empty, it will be random value of enemy_tank_color_type.
enemy_tank_color_type=( 41 43 43 45 44 46 )
enemy_tank_color=''
#level: 0    1  ..  8   9   10   11   ..  18  19 (20)
#time : 1.0 0.9 .. 0.2 0.1 0.09 0.08  .. 0.01 0 
game_level=9
my_tank_color=42
#about bullet
enemy_bullet_color=41
enemy_bullet_speed=0.02
my_bullet_color=41
my_bullet_speed=0
#after tank dead, next will appear
next_tank_interval_time=0.5
#about death symbol 
symbol_keep_time=1
#game PID
MYPID=$$
#
#SIGNAL: [20-31] [35-36]
#stop enemy tank signal: 20 (PID: $run_enemytank_pid)
#enemy tank be hitted: 22 (PID: $run_enemytank_pid)
#enemy tank pause: 23 (PID: $run_enemytank_pid)
#enemy tank continue after pause: 24 (PID: $run_enemytank_pid)
#enemy tank level/speed up: 28 (PID: $run_enemytank_pid)
#enemy tank level/slow down: 29 (PID: $run_enemytank_pid)
#stealth mode: 35 (PID: $run_enemytank_pid)
#cancel stealth mode: 36 (PID: $run_enemytank_pid)
#game over: 21 (PID: $MYPID)
#mytank pause: 25 (PID: $MYPID)
#mytank continue: 26 (PID: $MYPID)
#for "LingYi" components: 27 (PID: run_print_mywords_pid)
#for "infor" components: 30 (PID: run_print_infors_pid)
#for "Dtime" components: 31 (PID: run_print_dtime_pid)
#components [ true|false ]
print_mode=true
print_infor=true
print_LingYi=true
print_dtime=true
print_roll=true
expressions=('o(-"-)o' '^*(- -)*^' '($ _ $)' '(^O^)' 'Y(^_^)Y')
string1="you are so smart !"
string2="you are really talented !"
string3="Come on, boy !"
string4="I believe you will win !"
string5="Keep up the good work !"
strings=("$string1" "$string2" "$string3" "$string4" "$string5")
gameover_string="Stupid guy ~~ ha-ha !!!"
#
infor1="The game is running !"
infor2='Press "C" to continue !'
infor3='Press "P" to pause game !'
infor4='Press "U" to level up !'
infor5='Press "L" to slow down ! '
infor6='Press "Q" to end the game !'
infor7='Press "F" to kill one tank !'
infor8='Press "G" to open/close Stealth Mode !'
infor9='Press "V" to open/close God Mode '
infor10='press "N" to kill all tanks !'
infor11='if pressed "N", press M to close it !'
infor12='Press Space or Enter key to shoot !'
infors=("$infor1" "$infor2" "$infor3" "$infor4" "$infor5" "$infor6" "$infor7"\
    ${infors[@]} "$infor7" "$infor9" "$infor10" "$infor11" "$infor12")
roll_words="Tank Game  LingYi 2016.03.01 Y(^_^)Y"
# Positions of Enemytanks:
# =============================
# | 0           4           2 |
# |      9(random)            |
# | 7           8           5 |
# |                           |
# | 1           6           3 |
# =============================
#Define random positions, if allow using.
#value [ yes|no ]
random_9_position="yes"
random_8_position="no"
random_7_position="yes"
random_6_position="yes"
random_5_position="yes"
random_4_position="yes"
random_3_position="yes"
random_2_position="yes"
random_1_position="yes"
random_0_position="yes"
#get random direction.
GetRandDirect()
{
    case $[RANDOM%4] in 
        0) echo u;; 
        1) echo d;;
        2) echo l;; 
        3) echo r;; 
    esac; 
}
#display and record in the file.
#HandleTank {-d|-c} "x;y" {u|d|l|r} enemy/my
HandleTank()
{
    local hp=($(echo "$2" | awk -F';' '{print $1,$2}'))
    local body body_col
    body[0]="$2"
    case $3 in
    'u')
        body[1]="$((hp[0]+1));$((hp[1]-1))" 
        body[2]="$((hp[0]+1));$((hp[1]+1))" 
        body[3]="$((hp[0]+2));$((hp[1]-1))" 
        body[4]="$((hp[0]+2));$((hp[1]+1))" 
        ;;
    'd')
        body[1]="$((hp[0]-1));$((hp[1]+1))"
        body[2]="$((hp[0]-1));$((hp[1]-1))"
        body[3]="$((hp[0]-2));$((hp[1]+1))"
        body[4]="$((hp[0]-2));$((hp[1]-1))"
        ;;
    'l')
        body[1]="$((hp[0]+1));$((hp[1]+1))"
        body[2]="$((hp[0]-1));$((hp[1]+1))"
        body[3]="$((hp[0]+1));$((hp[1]+2))"
        body[4]="$((hp[0]-1));$((hp[1]+2))"
        ;;
    'r')
        body[1]="$((hp[0]-1));$((hp[1]-1))"
        body[2]="$((hp[0]+1));$((hp[1]-1))"
        body[3]="$((hp[0]-1));$((hp[1]-2))"
        body[4]="$((hp[0]+1));$((hp[1]-2))"
        ;;
    esac
    if [[ $1 == '-c' ]]; then
        local i
        for((i=0; i${tmpdir}/enemybody ;;
    "my" )
        body_col=$my_tank_color 
        body_symbol=('*' '*' '*' '*' '*')
        echo "${body[@]}" >${tmpdir}/mybody    ;;
    *    )  : ;;
    esac
    for((i=0; i/dev/null
            HandleTank -d "${mytank_head[0]};${mytank_head[1]}" $mytank_direct my    
        else
            kill -20 $run_enemytank_pid &>/dev/null
            kill -21 $MYPID
        fi
    fi
}
#Bullet $direct $headpoint(x y) { my | enemy [true|false] }
Bullet()
{
    #tank head point / bullet distance [thp/bdis]
    local thp=($2 $3) bdis n
    local myfile=${tmpdir}/mybody
    local enemyfile=${tmpdir}/enemybody
    case $4 in 
    "my") 
        bullet_color=$my_bullet_color
        bullet_symbol='@'
        bullet_speed=$my_bullet_speed ;;
    "enemy") 
        bullet_color=$enemy_bullet_color
        bullet_symbol=' '
        bullet_speed=$enemy_bullet_speed ;;
    esac
    case $1 in 
    'u') bdis=$(( thp[0]-boundary_u-2 )); (( thp[0]-=1 )) ;;
    'd') bdis=$(( boundary_d-thp[0]-2 )); (( thp[0]+=1 )) ;;
    'l') bdis=$(( thp[1]-boundary_l-2 )); (( thp[1]-=1 )) ;;
    'r') bdis=$(( boundary_r-thp[1]-2 )); (( thp[1]+=1 ));;
    esac
    for((n=1; n/dev/null
                break
            fi ;;
        "enemy")
            if ! $5 && echo " $(cat $myfile) " | grep -q " ${thp[0]};${thp[1]} "; then
                kill -21 $MYPID
                break
            fi ;;
        esac
        echo -ne "\033[${thp[0]};${thp[1]}H\033[${bullet_color}m${bullet_symbol}\033[0m"
        sleep $bullet_speed
        echo -ne "\033[${thp[0]};${thp[1]}H \033[0m"
    done
}
#Collision judgment
CollisJudge()
{    
    local k ifcoll=false
    enemy_points=$(cat ${tmpdir}/enemybody)
    my_points=( `cat ${tmpdir}/mybody` )
    for((k=0; k/dev/null
    $print_LingYi && kill -27 $run_print_mywords_pid &>/dev/null
    $print_infor  && kill -30 $run_print_infors_pid &>/dev/null
    $print_dtime  && kill -31 $run_print_dtime_pid &>/dev/null
    $print_mode   && kill -20 $run_print_mode_pid &>/dev/null
    $print_roll   && kill -10 $run_print_roll_words_pid &>/dev/null
    echo -ne "\033[$((boundary_u+12));$((boundary_r+3))H\033[41m\033[33mState\033[0m  \033[1;34mGame Over !!\033[0m"
    local line_posi=$(( (boundary_d-boundary_u)/2+boundary_u-2 ))
    local col_posi=$(( (boundary_r-boundary_l)/2+boundary_l-18 ))
    DisplayDeathSymbol $mytank_direct ${mytank_head[@]} my notclean
    echo -e "\033[$((line_posi + 0 ));${col_posi}H\033[1;31m-----------------------------------\033[0m"
    echo -e "\033[$((line_posi + 1 ));${col_posi}H\033[1;31m|                                 |\033[0m"
    echo -e "\033[$((line_posi + 2 ));${col_posi}H\033[1;31m|      Shit , You Are Dead !      |\033[0m"
    echo -e "\033[$((line_posi + 3 ));${col_posi}H\033[1;31m|                                 |\033[0m"
     echo -e "\033[$((line_posi + 4 ));${col_posi}H\033[1;31m-----------------------------------\033[0m"
    tput cnorm
    echo -ne "\033[$((boundary_d+1));$((boundary_r+1))H\n\033[0m"
    #read -s -t 1 -n 100 
}
print_mywords()
{
    [[ -z $strings ]] || [[ -z $expressions ]] && return
    StopSaying=false
    trap 'StopSaying=true' 27
    local oldwords space_sum="" i mywords="example" 
    local myexpress="" oldexpress
    echo -ne "\033[$((boundary_u+20));$((boundary_r+3))H\033[45m\033[1;39mLingYi\033[0m:"
    while ! $StopSaying 
    do     
        space_sum=" "
        #print the words and expression.
        mywords="${strings[$((RANDOM%${#strings[@]}))]}"
        myexpress="${expressions[$((RANDOM%${#expressions[@]}))]}"
        c1=3$((RANDOM%6+1))
        c2=3$((RANDOM%6+1))
        echo -ne "\033[$((boundary_u+21));$((boundary_r+3))H\033[1;${c1}m${mywords} \033[${c2}m${myexpress}\033[0m"
        sleep $((RANDOM%2))
        #clean up the words and expression
        for((i=1; i            
关注
打赏
1665849170
查看更多评论
0.0488s