셸 러시아 블록 스 크 립 트 구현
draw 그래 픽 인터페이스 를 그 리 는 것 입 니 다.keytest 는 키 보드 를 가 져 오 는 것 이 고 tetris 는 전체 게임 입 니 다.
tetris.sh
#!/bin/bash
APP_NAME="${0##*[\\/]}"
APP_VERSION="1.0"
#
iSumColor=7 #
cRed=1 #
cGreen=2 #
cYellow=3 #
cBlue=4 #
cFuchsia=5 #
cCyan=6 # ( )
cWhite=7 #
#
marginLeft=3 #
marginTop=2 #
((mapLeft=marginLeft+2)) #
((mapTop=$marginTop+1)) #
mapWidth=10 #
mapHeight=15 #
#
cBorder=$cGreen
cScore=$cFuchsia
cScoreValue=$cCyan
#
# , , ;
# , signal 。
sigRotate=25 #
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29 #
sigExit=30
# ,7 19
# 8 , 2
box0_0=(0 0 0 1 1 0 1 1 0 4)
box1_0=(0 1 1 1 2 1 3 1 0 3)
box1_1=(1 0 1 1 1 2 1 3 -1 3)
box2_0=(0 0 1 0 1 1 2 1 0 4)
box2_1=(0 1 0 2 1 0 1 1 0 3)
box3_0=(0 1 1 0 1 1 2 0 0 4)
box3_1=(0 0 0 1 1 1 1 2 0 4)
box4_0=(0 2 1 0 1 1 1 2 0 3)
box4_1=(0 1 1 1 2 1 2 2 0 3)
box4_2=(1 0 1 1 1 2 2 0 -1 3)
box4_3=(0 0 0 1 1 1 2 1 0 4)
box5_0=(0 0 1 0 1 1 1 2 0 3)
box5_1=(0 1 0 2 1 1 2 1 0 3)
box5_2=(1 0 1 1 1 2 2 2 -1 3)
box5_3=(0 1 1 1 2 0 2 1 0 4)
box6_0=(0 1 1 0 1 1 1 2 0 3)
box6_1=(0 1 1 1 1 2 2 1 0 3)
box6_2=(1 0 1 1 1 2 2 1 -1 3)
box6_3=(0 1 1 0 1 1 2 1 0 4)
iSumType=7 #
boxStyle=(1 2 2 2 4 4 4) #
iScoreEachLevel=50 #
#
sig=0 # signal
iScore=0 #
iLevel=0 #
boxNext=() #
iboxNextColor=0 #
iboxNextType=0 #
iboxNextStyle=0 #
boxCur=() #
iBoxCurColor=0 #
iBoxCurType=0 #
iBoxCurStyle=0 #
boxCurX=-1 # x
boxCurY=-1 # y
map=() #
# -1,
for ((i = 0; i < mapHeight * mapWidth; i++))
do
map[$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 QUIT
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" ]]; then sig=$sigRight #< >
fi
elif [[ $key == "W" || $key == "w" ]]; then sig=$sigRotate #W, w
elif [[ $key == "S" || $key == "s" ]]; then sig=$sigDown #S, s
elif [[ $key == "A" || $key == "a" ]]; then sig=$sigLeft #A, a
elif [[ $key == "D" || $key == "d" ]]; then sig=$sigRight #D, d
elif [[ "[$key]" == "[]" ]]; then sig=$sigAllDown #
elif [[ $key == "Q" || $key == "q" ]] #Q, q
then
MyExit
fi
if [[ $sig != 0 ]]
then
#
kill -$sig $pidDisplayer
fi
done
}
#
MyExitNoSub()
{
local y
#
stty $sTTY
((y = marginTop + mapHeight + 4))
#
echo -e "\033[?25h\033[${y};0H"
exit
}
MyExit()
{
#
kill -$sigExit $pidDisplayer
MyExitNoSub
}
#
RunAsDisplayer()
{
local sigThis
InitDraw
#
trap "sig=$sigRotate;" $sigRotate
trap "sig=$sigLeft;" $sigLeft
trap "sig=$sigRight;" $sigRight
trap "sig=$sigDown;" $sigDown
trap "sig=$sigAllDown;" $sigAllDown
trap "ShowExit;" $sigExit
while :
do
# iLevel ,
for ((i = 0; i < 21 - iLevel; i++))
do
sleep 0.02
sigThis=$sig
sig=0
# sig
if ((sigThis == sigRotate)); then BoxRotate; #
elif ((sigThis == sigLeft)); then BoxLeft; #
elif ((sigThis == sigRight)); then BoxRight; #
elif ((sigThis == sigDown)); then BoxDown; #
elif ((sigThis == sigAllDown)); then BoxAllDown; #
fi
done
#kill -$sigDown $$
BoxDown #
done
}
# , ,0 ,1
DrawCurBox()
{
local i x y bErase sBox
bErase=$1
if (( bErase == 0 ))
then
sBox="\040\040" #
else
sBox="[]"
echo -ne "\033[1m\033[3${iBoxCurColor}m\033[4${iBoxCurColor}m"
fi
for ((i = 0; i < 8; i += 2))
do
((y = mapTop + 1 + ${boxCur[$i]} + boxCurY))
((x = mapLeft + 1 + 2 * (boxCurX + ${boxCur[$i + 1]})))
echo -ne "\033[${y};${x}H${sBox}"
done
echo -ne "\033[0m"
}
#
#BoxMove(y, x), (y, x) , 0 , 1
BoxMove()
{
local i x y xPos yPos
yPos=$1
xPos=$2
for ((i = 0; i < 8; i += 2))
do
#
((y = yPos + ${boxCur[$i]}))
((x = xPos + ${boxCur[$i + 1]}))
if (( y < 0 || y >= mapHeight || x < 0 || x >= mapWidth))
then
#
return 1
fi
if (( ${map[y * mapWidth + x]} != -1 ))
then
#
return 1
fi
done
return 0;
}
#
Box2Map()
{
local i j x y line
#
for ((i = 0; i < 8; i += 2))
do
#
((y = ${boxCur[$i]} + boxCurY))
((x = ${boxCur[$i + 1]} + boxCurX))
map[y*mapWidth+x]=$iBoxCurColor #
done
line=0
for ((i = 0; i < mapHeight; i++))
do
for ((j = 0; j < mapWidth; j++))
do
# ,
[[ ${map[i*mapWidth+j]} -eq -1 ]] && break
done
[ $j -lt $mapWidth ] && continue
# ,
(( line++ ))
# i , 0 i-1 , i-1
for ((j = i*mapWidth-1; j >= 0; j--))
do
((x = j + mapWidth))
map[$x]=${map[$j]}
done
# , 0
for ((i = 0; i < mapWidth; i++))
do
map[$i]=-1
done
done
[ $line -eq 0 ] && return
# line
((x = marginLeft + mapWidth * 2 + 7))
((y = marginTop + 11))
((iScore += line * 2 - 1))
#
echo -ne "\033[1m\033[3${cScoreValue}m\033[${y};${x}H${iScore} "
if ((iScore % iScoreEachLevel < line * 2 - 1))
then
if ((iLevel < 20))
then
((iLevel++))
((y = marginTop + 14))
#
echo -ne "\033[3${cScoreValue}m\033[${y};${x}H${iLevel} "
fi
fi
echo -ne "\033[0m"
#
for ((i = 0; i < mapHeight; i++))
do
#
((y = i + mapTop + 1))
((x = mapLeft + 1))
echo -ne "\033[${y};${x}H"
for ((j = 0; j < mapWidth; j++))
do
((tmp = i * mapWidth + j))
if ((${map[$tmp]} == -1))
then
echo -ne " "
else
echo -ne "\033[1m\033[3${map[$tmp]}m\033[4${map[$tmp]}m[]\033[0m"
fi
done
done
}
#
BoxLeft()
{
local x
((x = boxCurX - 1))
if BoxMove $boxCurY $x
then
DrawCurBox 0
((boxCurX = x))
DrawCurBox 1
fi
}
#
BoxRight()
{
local x
((x = boxCurX + 1))
if BoxMove $boxCurY $x
then
DrawCurBox 0
((boxCurX = x))
DrawCurBox 1
fi
}
#
BoxDown()
{
local y
((y = boxCurY + 1)) # y
if BoxMove $y $boxCurX #
then
DrawCurBox 0 #
((boxCurY = y))
DrawCurBox 1 #
else
# ,
Box2Map #
CreateBox #
fi
}
#
BoxAllDown()
{
local y iDown
#
iDown=0
(( y = boxCurY + 1 ))
while BoxMove $y $boxCurX
do
(( y++ ))
(( iDown++ ))
done
DrawCurBox 0 #
((boxCurY += iDown))
DrawCurBox 1 #
Box2Map #
CreateBox #
}
#
BoxRotate()
{
[ ${boxStyle[$iBoxCurType]} -eq 1 ] && return
((rotateStyle = (iBoxCurStyle + 1) % ${boxStyle[$iBoxCurType]}))
# boxTmp
boxTmp=( `eval 'echo ${boxCur[@]}'` )
boxCur=( `eval 'echo ${box'$iBoxCurType'_'$rotateStyle'[@]}'` )
if BoxMove $boxCurY $boxCurX #
then
#
boxCur=( `eval 'echo ${boxTmp[@]}'` )
DrawCurBox 0
boxCur=( `eval 'echo ${box'$iBoxCurType'_'$rotateStyle'[@]}'` )
DrawCurBox 1
iBoxCurStyle=$rotateStyle
else
# ,
boxCur=( `eval 'echo ${boxTmp[@]}'` )
fi
}
#
PrepareNextBox()
{
local i x y
#
if (( ${#boxNext[@]} != 0 )); then
for ((i = 0; i < 8; i += 2))
do
((y = marginTop + 1 + ${boxNext[$i]}))
((x = marginLeft + 2 * mapWidth + 7 + 2 * ${boxNext[$i + 1]}))
echo -ne "\033[${y};${x}H\040\040"
done
fi
#
(( iBoxNextType = RANDOM % iSumType ))
(( iBoxNextStyle = RANDOM % ${boxStyle[$iBoxNextType]} ))
(( iBoxNextColor = RANDOM % $iSumColor + 1 ))
boxNext=( `eval 'echo ${box'$iBoxNextType'_'$iBoxNextStyle'[@]}'` )
#
echo -ne "\033[1m\033[3${iBoxNextColor}m\033[4${iBoxNextColor}m"
for ((i = 0; i < 8; i += 2))
do
((y = marginTop + 1 + ${boxNext[$i]}))
((x = marginLeft + 2 * mapWidth + 7 + 2 * ${boxNext[$i + 1]}))
echo -ne "\033[${y};${x}H[]"
done
echo -ne "\033[0m"
}
#
CreateBox()
{
if (( ${#boxCur[@]} == 0 )); then
#
(( iBoxCurType = RANDOM % iSumType ))
(( iBoxCurStyle = RANDOM % ${boxStyle[$iBoxCurType]} ))
(( iBoxCurColor = RANDOM % $iSumColor + 1 ))
else
# ,
iBoxCurType=$iBoxNextType;
iBoxCurStyle=$iBoxNextStyle;
iBoxCurColor=$iBoxNextColor
fi
#
boxCur=( `eval 'echo ${box'$iBoxCurType'_'$iBoxCurStyle'[@]}'` )
#
boxCurY=boxCur[8];
boxCurX=boxCur[9];
DrawCurBox 1 #
if ! BoxMove $boxCurY $boxCurX
then
kill -$sigExit $PPID
ShowExit
fi
PrepareNextBox
}
#
DrawBorder()
{
clear
local i y x1 x2
#
echo -ne "\033[1m\033[3${cBorder}m\033[4${cBorder}m"
((x1 = marginLeft + 1)) # x
((x2 = x1 + 2 + mapWidth * 2)) # x
for ((i = 0; i < mapHeight; i++))
do
((y = i + marginTop + 2))
echo -ne "\033[${y};${x1}H||" #
echo -ne "\033[${y};${x2}H||" #
done
((x1 = marginTop + mapHeight + 2))
for ((i = 0; i < mapWidth + 2; i++))
do
((y = i * 2 + marginLeft + 1))
echo -ne "\033[${mapTop};${y}H==" #
echo -ne "\033[${x1};${y}H==" #
done
echo -ne "\033[0m"
# "Score" "Level"
echo -ne "\033[1m"
((y = marginLeft + mapWidth * 2 + 7))
((x1 = marginTop + 10))
echo -ne "\033[3${cScore}m\033[${x1};${y}HScore"
((x1 = marginTop + 11))
echo -ne "\033[3${cScoreValue}m\033[${x1};${y}H${iScore}"
((x1 = marginTop + 13))
echo -ne "\033[3${cScore}m\033[${x1};${y}HLevel"
((x1 = marginTop + 14))
echo -ne "\033[3${cScoreValue}m\033[${x1};${y}H${iLevel}"
echo -ne "\033[0m"
}
InitDraw()
{
clear #
DrawBorder #
CreateBox #
}
# GameOVer!
ShowExit()
{
local y
((y = mapHeight + mapTop + 3))
echo -e "\033[${y};1HGameOver!\033[0m"
exit
}
# .
if [[ "$1" == "--version" ]]; then
echo "$APP_NAME $APP_VERSION"
elif [[ "$1" == "--show" ]]; then
# --show ,
RunAsDisplayer
else
bash $0 --show& # --show
RunAsKeyReceiver $! #
fi
keytest.sh
#!/bin/bash
GetKey()
{
aKey=(0 0 0) # 3
cESC=`echo -ne "\033"`
cSpace=`echo -ne "\040"`
while :
do
read -s -n 1 key # , key
#echo $key
#echo XXX
aKey[0]=${aKey[1]} #
aKey[1]=${aKey[2]} #
aKey[2]=$key #
if [[ $key == $cESC && ${aKey[1]} == $cESC ]]
then
MyExit
elif [[ ${aKey[0]} == $cESC && ${aKey[1]} == "[" ]]
then
if [[ $key == "A" ]]; then echo KEYUP
elif [[ $key == "B" ]]; then echo KEYDOWN
elif [[ $key == "D" ]]; then echo KEYLEFT
elif [[ $key == "C" ]]; then echo KEYRIGHT
fi
fi
done
}
GetKey
draw.sh
#!/bin/bash
#
marginLeft=8 #
marginTop=6 #
((mapLeft=marginLeft+2)) #
((mapTop=$marginTop+1)) #
mapWidth=10 #
mapHeight=15 #
# ,7 19
# 8 , 2
box0_0=(0 0 0 1 1 0 1 1 0 4)
box1_0=(0 1 1 1 2 1 3 1 0 3)
box1_1=(1 0 1 1 1 2 1 3 -1 3)
box2_0=(0 0 1 0 1 1 2 1 0 4)
box2_1=(0 1 0 2 1 0 1 1 0 3)
box3_0=(0 1 1 0 1 1 2 0 0 4)
box3_1=(0 0 0 1 1 1 1 2 0 4)
box4_0=(0 2 1 0 1 1 1 2 0 3)
box4_1=(0 1 1 1 2 1 2 2 0 3)
box4_2=(1 0 1 1 1 2 2 0 -1 3)
box4_3=(0 0 0 1 1 1 2 1 0 4)
box5_0=(0 0 1 0 1 1 1 2 0 3)
box5_1=(0 1 0 2 1 1 2 1 0 3)
box5_2=(1 0 1 1 1 2 2 2 -1 3)
box5_3=(0 1 1 1 2 0 2 1 0 4)
box6_0=(0 1 1 0 1 1 1 2 0 3)
box6_1=(0 1 1 1 1 2 2 1 0 3)
box6_2=(1 0 1 1 1 2 2 1 -1 3)
box6_3=(0 1 1 0 1 1 2 1 0 4)
#
DrawBorder()
{
clear
local i y x1 x2
#
echo -ne "\033[1m\033[32m\033[42m"
((x1 = marginLeft + 1)) # x
((x2 = x1 + 2 + mapWidth * 2)) # x
for ((i = 0; i < mapHeight; i++))
do
((y = i + marginTop + 2))
echo -ne "\033[${y};${x1}H||" #
echo -ne "\033[${y};${x2}H||" #
done
((x1 = marginTop + mapHeight + 2))
for ((i = 0; i < mapWidth + 2; i++))
do
((y = i * 2 + marginLeft + 1))
echo -ne "\033[${mapTop};${y}H==" #
echo -ne "\033[${x1};${y}H==" #
done
echo -ne "\033[0m"
}
DrawBox()
{
local i x y xPos yPos
yPos=${box0_0[8]}
xPos=${box0_0[9]}
echo -ne "\033[1m\033[35m\033[45m"
for ((i = 0; i < 8; i += 2))
do
(( y = mapTop + 1 + ${box0_0[$i]} + yPos ))
(( x = mapLeft + 1 + 2 * (${box0_0[$i + 1]} + xPos) ))
echo -ne "\033[${y};${x}H[]"
done
echo -ne "\033[0m"
}
InitDraw()
{
clear #
DrawBorder #
DrawBox
while :
do
sleep 1
done
}
InitDraw
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ZSH에서 물고기까지ZSH는 수년 동안 내 기본 셸이었습니다. 이제 몇 달 동안 사용하면서 ZSH 구성에 대해 몇 가지 사항을 발견했습니다. 우리는 을 제공하는 시스템과 더 빨리 상호 작용하는 경향이 있습니다. 내.zshrc 구성에는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.