shell脚本游戏之--俄罗斯方块

 # 俄罗斯方块.sh 标题)vim elsfk.sh#!/bin/bash#Tetris Game#10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declarationAPP_NAME="${0##*[\\/]}"APP_VERSION="1.0" #颜色定...
摘要由CSDN通过智能技术生成

 

# 俄罗斯方块.sh

 

标题

)

vim elsfk.sh



#!/bin/bash
#Tetris Game
#10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]>
  
#APP declaration
APP_NAME="${0##*[\\/]}"
APP_VERSION="1.0"
  
  
#颜色定义
cRed=1
cGreen=2
cYellow=3
cBlue=4
cFuchsia=5
cCyan=6
cWhite=7
colorTable=($cRed $cGreen $cYellow $cBlue $cFuchsia $cCyan $cWhite)
  
#位置和大小
iLeft=3
iTop=2
((iTrayLeft = iLeft + 2))
((iTrayTop = iTop + 1))
((iTrayWidth = 10))
((iTrayHeight = 15))
  
#颜色设置
cBorder=$cGreen
cScore=$cFuchsia
cScoreValue=$cCyan
  
#控制信号
#改游戏使用两个进程,一个用于接收输入,一个用于游戏流程和显示界面;
#当前者接收到上下左右等按键时,通过向后者发送signal的方式通知后者。
sigRotate=25
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29
sigExit=30
  
#七中不同的方块的定义
#通过旋转,每种方块的显示的样式可能有几种
box0=(0 0 0 1 1 0 1 1)
box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3)
box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0)
box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1)
box4=(0 1 0 2 1 1 2 1 1 0 1 1 1 2 2 2 0 1 1 1 2 0 2 1 0 0 1 0 1 1 1 2)
box5=(0 1 1 1 2 1 2 2 1 0 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 0 1 1 1 2)
box6=(0 1 1 1 1 2 2 1 1 0 1 1 1 2 2 1 0 1 1 0 1 1 2 1 0 1 1 0 1 1 1 2)
#所有其中方块的定义都放到box变量中
box=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]})
#各种方块旋转后可能的样式数目
countBox=(1 2 2 2 4 4 4)
#各种方块再box数组中的偏移
offsetBox=(0 1 3 5 7 11 15)
  
#每提高一个速度级需要积累的分数
iScoreEachLevel=50        #be greater than 7
  
#运行时数据
sig=0                #接收到的signal
iScore=0        #总分
iLevel=0        #速度级
boxNew=()        #新下落的方块的位置定义
cBoxNew=0        #新下落的方块的颜色
iBoxNewType=0        #新下落的方块的种类
iBoxNewRotate=0        #新下落的方块的旋转角度
boxCur=()        #当前方块的位置定义
cBoxCur=0        #当前方块的颜色
iBoxCurType=0        #当前方块的种类
iBoxCurRotate=0        #当前方块的旋转角度
boxCurX=-1        #当前方块的x坐标位置
boxCurY=-1        #当前方块的y坐标位置
iMap=()                #背景方块图表
  
#初始化所有背景方块为-1, 表示没有方块
for ((i = 0; i < iTrayHeight * iTrayWidth; i++)); do iMap[$i]=-1; done
  
  
#接收输入的进程的主函数
function RunAsKeyReceiver()
{
        local pidDisplayer key aKey sig cESC sTTY
  
        pidDisplayer=$1
        aKey=(0 0 0)
  
        cESC=`echo -ne "\033"`
        cSpace=`echo -ne "\040"`
  
        #保存终端属性。在read -s读取终端键时,终端的属性会被暂时改变。
        #如果在read -s时程序被不幸杀掉,可能会导致终端混乱,
        #需要在程序退出时恢复终端属性。
        sTTY=`stty -g`
  
        #捕捉退出信号
        trap "MyExit;" INT TERM
        trap "MyExitNoSub;" $sigExit
  
        #隐藏光标
        echo -ne "\033[?25l"
  
  
        while :
        do
                #读取输入。注-s不回显,-n读到一个字符立即返回
                read -s -n 1 key
  
                aKey[0]=${aKey[1]}
                aKey[1]=${aKey[2]}
                aKey[2]=$key
                sig=0
  
                #判断输入了何种键
                if [[ $key == $cESC && ${aKey[1]} == $cESC ]]
                then
                        #ESC键
                        MyExit
                elif [[ ${aKey[0]} == $cESC && ${aKey[1]} == "[" ]]
                then
                        if [[ $key == "A" ]]; then sig=$sigRotate        #<向上键>
                       elif [[ $key == "B" ]]; then sig=$sigDown        #<向下键>
                        elif [[ $key == "D" ]]; then sig=$sigLeft        #<向左键>
                        elif [[ $key == "C" 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值