shell脚本实战之坦克大战小游戏
Posted 江湖有缘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本实战之坦克大战小游戏相关的知识,希望对你有一定的参考价值。
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<5; i++)); do
echo -ne "\\033[$body[i]H \\033[0m"
done
return
fi
case $4 in
"enemy")
body_col=$seted_enemy_tank_color
body_symbol=(' ' ' ' ' ' ' ' ' ')
echo "$body[@]" >$tmpdir/enemybody ;;
"my" )
body_col=$my_tank_color
body_symbol=('*' '*' '*' '*' '*')
echo "$body[@]" >$tmpdir/mybody ;;
* ) : ;;
esac
for((i=0; i<5; i++)); do
echo -ne "\\033[$body[i]H\\033[$body_colm$body_symbol[i]\\033[0m"
done
DisplayRandTank()
local positions='0123456789'
[[ $random_9_position == "no" ]] && positions=$(echo $positions | sed "s/9//")
[[ $random_8_position == "no" ]] && positions=$(echo $positions | sed "s/8//")
[[ $random_7_position == "no" ]] && positions=$(echo $positions | sed "s/7//")
[[ $random_6_position == "no" ]] && positions=$(echo $positions | sed "s/6//")
[[ $random_5_position == "no" ]] && positions=$(echo $positions | sed "s/5//")
[[ $random_4_position == "no" ]] && positions=$(echo $positions | sed "s/4//")
[[ $random_3_position == "no" ]] && positions=$(echo $positions | sed "s/3//")
[[ $random_2_position == "no" ]] && positions=$(echo $positions | sed "s/2//")
[[ $random_1_position == "no" ]] && positions=$(echo $positions | sed "s/1//")
[[ $random_0_position == "no" ]] && positions=$(echo $positions | sed "s/0//")
local rand_direct=$(GetRandDirect)
local rand_pos
while :
do
rand_pos=$[RANDOM%10]
echo $positions | grep -q $rand_pos && break
done
#set enemy tank body color
if [[ -z $enemy_tank_color ]]; then
seted_enemy_tank_color=$enemy_tank_color_type[$(( RANDOM % $#enemy_tank_color_type[@] ))]
else
seted_enemy_tank_color=$enemy_tank_color
fi
PositionNine()
local hand_1 hand_2
while :
do
head_1=$((RANDOM%boundary_d))
[[ $head_1 -ge $(( boundary_u+3)) ]] && [[ $head_1 -le $(( boundary_d-3 )) ]] && break
done
while :
do
head_2=$((RANDOM%boundary_r))
[[ $head_2 -ge $(( boundary_l+3)) ]] && [[ $head_2 -le $(( boundary_r-3 )) ]] && break
done
hp="$head_1;$head_2"
case $rand_direct in
'u')
case $rand_pos in
0) hp="$(( boundary_u+1 ));$(( boundary_l+2 ))" ;;
1) hp="$(( boundary_d-3 ));$(( boundary_l+2 ))" ;;
2) hp="$(( boundary_u+1 ));$(( boundary_r-2 ))" ;;
3) hp="$(( boundary_d-3 ));$(( boundary_r-2 ))" ;;
4) hp="$(( boundary_u+1 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
5) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_r-2 ))" ;;
6) hp="$(( boundary_d-3 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
7) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_l+2 ))" ;;
8) hp="$(( (boundary_d-boundary_u)/2+boundary_u));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
9) PositionNine ;;
esac ;;
'd')
case $rand_pos in
0) hp="$(( boundary_u+3 ));$(( boundary_l+2 ))" ;;
1) hp="$(( boundary_d-1 ));$(( boundary_l+2 ))" ;;
2) hp="$(( boundary_u+3 ));$(( boundary_r-2 ))" ;;
3) hp="$(( boundary_d-1 ));$(( boundary_r-2 ))" ;;
4) hp="$(( boundary_u+3 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
5) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_r-2 ))" ;;
6) hp="$(( boundary_d-1 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
7) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_l+2 ))" ;;
8) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
9) PositionNine ;;
esac ;;
'l')
case $rand_pos in
0) hp="$(( boundary_u+2 ));$(( boundary_l+1 ))" ;;
1) hp="$(( boundary_d-2 ));$(( boundary_l+1 ))" ;;
2) hp="$(( boundary_u+2 ));$(( boundary_r-3 ))" ;;
3) hp="$(( boundary_d-2 ));$(( boundary_r-3 ))" ;;
4) hp="$(( boundary_u+2 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
5) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_r-3 ))" ;;
6) hp="$(( boundary_d-2 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
7) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_l+1 ))" ;;
8) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
9) PositionNine ;;
esac ;;
'r')
case $rand_pos in
0) hp="$(( boundary_u+2 ));$(( boundary_l+3 ))" ;;
1) hp="$(( boundary_d-2 ));$(( boundary_l+3 ))" ;;
2) hp="$(( boundary_u+2 ));$(( boundary_r-1 ))" ;;
3) hp="$(( boundary_d-2 ));$(( boundary_r-1 ))" ;;
4) hp="$(( boundary_u+2 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
5) hp="$(( (boundary_d-boundary_u)/2 ));$(( boundary_r-1 ))" ;;
6) hp="$(( boundary_d-2 ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
7) hp="$(( (boundary_d-boundary_u)/2+boundary_u ));$(( boundary_l+3 ))" ;;
8) hp="$(( (boundary_d-boundary_u)/2+boundary_ ));$(( (boundary_r-boundary_l)/2+boundary_l ))" ;;
9) PositionNine ;;
esac ;;
esac
enemytank_direct=$rand_direct
first_moving=true
#clean up the small boundary
local n m
for n in 1 2 3 4 5
do
for m in 4 5 6 7 8 9 10
do
echo -ne "\\033[$((boundary_u+n));$((boundary_r+m))H*\\033[0m"
done
done
#display a tank model in small boundary
case $enemytank_direct in
'u') small_boundary_tank_head="$((boundary_u+2));$((boundary_r+7))" ;;
'd') small_boundary_tank_head="$((boundary_u+4));$((boundary_r+7))" ;;
'l') small_boundary_tank_head="$((boundary_u+3));$((boundary_r+6))" ;;
'r') small_boundary_tank_head="$((boundary_u+3));$((boundary_r+8))" ;;
esac
HandleTank -d "$small_boundary_tank_head" $enemytank_direct enemy
HandleTank -d "$hp" $enemytank_direct enemy
#get a new position from new, old direction and an old position.
#GetNewPos $newdirect $olddirect $headpoint(x y)
GetNewPos()
local new_head=($3 $4)
case $1 in
'u')
case $2 in
'u') [[ new_head[0] -gt $((boundary_u+1)) ]] && (( new_head[0]-=1 ));;
'd') (( new_head[0]-=2 ));;
'l') (( new_head[0]-=1 )); (( new_head[1]+=1 ));;
'r') (( new_head[0]-=1 )); (( new_head[1]-=1 ));;
esac
;;
'd')
case $2 in
'u') (( new_head[0]+=2 ));;
'd') [[ new_head[0] -lt $((boundary_d-1)) ]] && (( new_head[0]+=1 ));;
'l') (( new_head[0]+=1 )); (( new_head[1]+=1 ));;
'r') (( new_head[0]+=1 )); (( new_head[1]-=1 ));;
esac
;;
'l')
case $2 in
'u') (( new_head[0]+=1 )); (( new_head[1]-=1 ));;
'd') (( new_head[以上是关于shell脚本实战之坦克大战小游戏的主要内容,如果未能解决你的问题,请参考以下文章