linux 像素飞机大战 命令行

linux实现飞机大战

命令行方式实现

#!/bin/sh
###——————项目名称::     像素飞机大战
###————————————————————————————————————游戏玩法::

###————————————————————“W”“S”“A”“D”分别控制上下左右
###—————————————————————“N”退出
###—————————————死亡后按“1”继续,按“0”退出
###—————————————子弹数:10发,打完后不能增加
###—————————————计分规则:击落一架敌机得三分,刷新3次得一分(相当与存活时间)

###——————————————————————————————————任务分配::

###————————共420行
###——————————————————————————————————2019/03/19

###——————————————————————————————
###——————————————————————————————

###定义全局变量
declare width=50  #列数
declare length=30 #行数
declare -x point=0
declare -x loactiona=22
declare -x loactionb=23
declare -x loactionc=24
declare -x loactiond=25
declare -x loactione=26
declare -x loactionA=26
declare -x loactionB=27
declare -x loactionC=28 #以上飞机的行纵坐标
declare -x bulletnum=10
declare -x juegebullet=0
declare -x number=0
declare -x index=0
declare -x map=()      #定义子弹数组
declare -x enemy=()  
declare -x dnumber=0
declare -x locbull=() #定义子弹坐标数组
declare -x gameover=1 #循环变量

###————————————————————————————————
###随机生成敌机位置

function diji(){
if((number%3==0))
then    enemy[((index))]=3
       enemy[((index+1))]=$((RANDOM%44+3))
       index=$((index+2))
fi
}

###——————————————————————————————————
###敌机位置赋值给 map 方便打印

function enemyloaction(){
for ((i=0;i<index;i=i+2))
do map[((50*enemy[i]+enemy[((i+1))]))]=7
map[((50*(enemy[i]-1)+enemy[((i+1))]-1))]=7
map[((50*(enemy[i]-1)+enemy[((i+1))]+1))]=7
map[((50*(enemy[i]-1)+enemy[((i+1))]))]=9
map[((50*(enemy[i]-2)+enemy[((i+1))]))]=9
map[((50*(enemy[i]-2)+enemy[((i+1))]+1))]=10
map[((50*(enemy[i]-2)+enemy[((i+1))]-1))]=10
done
}

###——————————————————————————————————
###清空上一次敌机图形
function unenemyloaction(){

for ((i=0;i<index;i=i+2))
do map[((50*enemy[i]+enemy[((i+1))]))]=0
map[((50*(enemy[i]-1)+enemy[((i+1))]-1))]=0
map[((50*(enemy[i]-1)+enemy[((i+1))]+1))]=0
map[((50*(enemy[i]-1)+enemy[((i+1))]))]=0
map[((50*(enemy[i]-2)+enemy[((i+1))]))]=0
map[((50*(enemy[i]-2)+enemy[((i+1))]+1))]=0
map[((50*(enemy[i]-2)+enemy[((i+1))]-1))]=0
done
}

###————————————————————————————————
###敌机位置自增

function enemyadd(){
   for ((i=0;i<index;i++))
do if((i%2==0))
    then ((enemy[i]++))
 fi
done
}

###——————————————————————————————————
#初始子弹坐标
for((a=0;a<20;a++))
do locbull[$a]=0
done                   

###——————————————————————————————————
###初始化界面
for((a=0;a<$length;a++))
do 
   for((b=0;b<$width;b++))
 do if((b==0||b==49||a==0||a==29))
      then map[((a*50+b))]=8 #边框
      else
      map[((a*50+b))]=0 
fi
done
done                       #循环打印主界面

###  实时改变子弹坐标 ——————————————————————
##子弹位置自减

function scan(){
for((a=0;a<20;a++))
do  if((  (a%2==0)&&(locbull[((a))]!=0) ))
    then  ((locbull[$a]--))
fi
done
}

### ————————————————————————————————
###飞机位置                              
function myairplane(){
 map[((50*$6+$3))]=4
 map[((50*$7+$1))]=2
 map[((50*$7+$2))]=3
 map[((50*$7+$3))]=2
 map[((50*$7+$4))]=3
 map[((50*$7+$5))]=2
 map[((50*$8+$2))]=2
 map[((50*$8+$3))]=3
 map[((50*$8+$4))]=2
} 

### ————————————————————————————————
###移动飞机后将上一次飞机所在的值赋为0,便于打印

function reset(){ 
 map[((50*$6+$3))]=0
 map[((50*$7+$1))]=0
 map[((50*$7+$2))]=0
 map[((50*$7+$3))]=0
 map[((50*$7+$4))]=0
 map[((50*$7+$5))]=0
 map[((50*$8+$2))]=0
 map[((50*$8+$3))]=0
 map[((50*$8+$4))]=0

}

###—————————————————————————————————
###计分
function score(){
(( point=dnumber+number/3))

}

###——————————————————————————————————
#####   此函数起到全局大脑作用,进行刷新界面
   
function jianmian(){
   score
  enemyloaction
for((a=0;a<$length;a++))
do 
   for((b=0;b<$width;b++))
 do useless=${map[a*50+b]}
   if((useless==1&&a==1))  ##1代表子弹,对子弹位置进行判断
  then echo -n " "
  elif ((b==0||b==49||a==0||a==29))
  then echo -n -e "\033[44;34m8\033[0m"
   elif((useless==1&&a!=1))
  then map[(a-1)*50+b]=1     ##以下对飞机进行像素块操作
       echo -n -e "\033[42;32m8\033[0m"      
       map[a*50+b]=0
  elif ((useless==2))
  then echo -n -e "\033[45;35m8\033[0m"
  elif ((useless==3))
  then echo -n -e "\033[41;31m8\033[0m"
  elif ((useless==4))
  then echo -n -e "\033[44;34m8\033[0m"
  elif((useless==0))   ##如果为零,则输出空格
  then echo -n " "
  elif ((useless==7))
  then echo -n -e "\033[43;33m8\033[0m" 
   elif ((useless==9))
  then echo -n -e "\033[42;32m8\033[0m"
elif ((useless==10))
  then echo -n -e "\033[41;31m8\033[0m"
fi
done
echo
done
echo
 echo -n -e "\033[40;37m  分数:   ${point}           \033[0m"
             
}

###——————————————————————————————————
###退出函数

function fexit(){
#((gameover=0))
echo
echo
echo
echo
echo -n "        "
echo  -e "\033[46;31m      GAME  OVER       \033[0m"
echo -n "        "
echo  -e "\033[46;31m   你最后的得分是:${point}   \033[0m"

echo -n "        "
echo  -e "\033[46;33m  输入 1 重新开始游戏  \033[0m"
echo -n "        "
echo  -e "\033[46;33m   输入0残忍离开       \033[0m"
read -s -n 1 -a choice
if((choice==1))
then clear
      # exit
         ./beautiful.sh
elif((choice==0)) 
 then clear
       exit
else 
break
exit
fi


}

###——————————————————————————
###相撞事件

function collision() #locationA locationc
{
###判断敌机和主机相撞 
    for ((i=0;i<index;i=i+2))
do  x=${enemy[$i]}
    y=${enemy[((i+1))]}
     if(((x==loactionA)&&(y==loactionc)))
     
	then
fexit
	fi
     J=$((loactionA+2))
           K=$((y-loactionc))
         l=$((loactionA+4))
           m=$((loactionc+3))
           o=$((loactionc-3)) ##判断位置关系
       if((   (x==J)&&(-2<=K)&&(K<=2)   ))
         then
fexit
	fi
       if(( (x==l)&&((m==y)||(o==y)) ))
   then
fexit
	fi
       
done
###判断子弹和敌机相撞
	for ((i=0;i<20;i++))
	do
	     if (((i%2)==0))
	     then
	     Ba=$((locbull[$i]))

            
           fi
	     if(((i%2)==1))
            then
	     Bb=$((locbull[$i]))
   	     fi

             for ((h=0;h<index;h=h+2))
      do  Xx=${enemy[$h]}
         Yy=${enemy[((h+1))]} 
             q=$((Yy-Bb))
  
	     if(((Yy==Bb)&&((Xx-1)==Ba))) ##判断位置关系
	    then
             enemy[$h]=50
            locbull[$h]=0
               

		((dnumber=dunmber+3))
	     fi
if(((-1<=q)&&(q<=1)&&(Ba==(Xx-2))))
then
             enemy[$h]=50
            locbull[$h]=0
		((dnumber=dunmber+3))
 fi
     done
	done

}
###——————————————————————————————————————————————————————————————————
###——————————————————————————————————————————————————————————————————
###打印打印初始化界面
diji
myairplane loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC  
jianmian

###——————————————————————————————————————————————————————————————————
###函数集合

function move(){
clear
scan    ##改变子弹坐标
myairplane loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC  ##确定移动后友机坐标
collision
jianmian

}

###——————————————————————————————————————————————————————————————————
##确定是否打出子弹 并将子弹坐标写入数组 每打初一发子弹,子弹数减一

function bullet(){   
if((juegebullet==1&&bulletnum>0))
 then map[((50*($1-1)+$loactionc))]=1
   locbull[((20-bulletnum*2))]=$1
   locbull[((21-bulletnum*2))]=$loactionc
     (( bulletnum--))
     $juegebullet=0
fi
}

###——————————————————————————————————————————————————————————————————
###——————————————————————————————————————————————————————————————————

###  发挥主函数作用 接受用户操作信息
###  对飞机位置加以判断的同时,改变飞机位置,接受一次信息,界面刷新一次
###  只能接受 W S A D J 对其他输入无反应,(但由于在终端进行,当输入向上箭头时,也会有所反应)
###  终止条件为 游戏结束

function rekey(){
while ((gameover==1))
     echo -e "\033[?25l"
do  read -s -n 1 -a key
key="${key[@]: -1}"
case $key in
        W) (( number++))
                diji
         if (( loactionA==1))
           then unenemyloaction
                       enemyadd
                         move
                     
           else reset loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC
                      unenemyloaction
                (( loactionA--,loactionB--,loactionC--))
                      enemyadd
                move
           fi ;;
        S) (( number++))
                   diji
          if (( loactionC==28))
           then unenemyloaction
                       enemyadd
                         move
           else reset loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC
                      unenemyloaction
                (( loactionA++,loactionB++,loactionC++))
                      enemyadd
                move
           fi ;;
        A) (( number++))
            diji
          if (( loactiona==1))
           then thenunenemyloaction
                       enemyadd
                         move
           else reset loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC
                     unenemyloaction
               (( loactiona--,loactionb--,loactionc--,loactiond--,loactione--))                        
                     enemyadd
                move
           fi ;;
        D) (( number++))
                diji
           if (( loactione==48))
           then unenemyloaction
                       enemyadd
                         move
           else  reset loactiona loactionb loactionc loactiond loactione loactionA loactionB loactionC
                        unenemyloaction
               (( loactiona++,loactionb++,loactionc++,loactiond++,loactione++))
                      enemyadd
                move 
           fi ;; 
        J) (( juegebullet=1))
            if (( juegebullet==1))  
                  then 
                bullet loactionA 
               fi
               move ;;
          N)  ##退出
               clear
               break
              exit  ;;
           
        *)  continue ;;  ##否则继续
esac
done
}

###————————————————————————————————————————————————————————————————————
###————————————————————————————————————————————————————————————————————


rekey  #调用主函数

















     

你可能感兴趣的:(linux 像素飞机大战 命令行)