Home >Backend Development >PHP Tutorial >Tetris in shell_PHP tutorial
#!/bin/bash
# Tetris Game
# 10.21.2003 xhchen
#Color definition
cRed=1
cGreen=2
cYellow=3
cBlue=4
cFuchsia=5
cCyan=6
cWhite=7
colorTable=($cRed $cGreen $cYellow $cBlue $cFuchsia $ cCyan $cWhite)
#Position and size
iLeft=3
iTop=2
((iTrayLeft = iLeft + 2))
((iTrayTop = iTop + 1))
((iTrayWidth = 10))
((iTrayHeight = 15))
#Color settings
cBorder=$cGreen
cScore=$cFuchsia
cScoreValue=$cCyan
#Control signal
#The game uses two processes, one for receiving input, and one for the game process and display interface;
#The former receives the up, down, left, and other buttons When , notify the latter by sending a signal to the latter.
sigRotate=25
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29
sigExit=30
#Definition of seven different blocks
#Through rotation, each block may be displayed in several styles
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)
#All the definitions of the boxes are placed in the box variable
box=($ $ $ $ $ $ $)
#The number of possible styles after rotation of various blocks
countBox=(1 2 2 2 4 4 4)
#The offset of various blocks in the box array
offsetBox=(0 1 3 5 7 11 15)
#The score that needs to be accumulated for each speed level increase
iScoreEachLevel=50 #be greater than 7
#Runtime data
sig=0 #Received signal
iScore=0 #Total score
iLevel=0 #Speed level
boxNew=() #The position definition of the new falling block
cBoxNew=0 #The color of the new falling block
iBoxNewType=0 #The type of the new falling box
iBoxNewRotate=0 #The rotation angle of the new falling box
boxCur=() #The position definition of the current box
cBoxCur=0 #The color of the current box
iBoxCurType=0 #The type of current box
iBoxCurRotate=0 #The rotation angle of the current box
boxCurX=-1 #The x-coordinate position of the current box
boxCurY=-1 #The y-coordinate position of the current box
iMap=() #Background block chart
#Initialize all background blocks to -1, which means there is no block
for ((i = 0; i < iTrayHeight * iTrayWidth; i++)); do iMap[$i]=-1; done
#The main function of the process that receives input
function RunAsKeyReceiver()
{
local pidDisplayer key aKey sig cESC sTTY
pidDisplayer=
aKey=(0 0 0)
cESC=`echo -ne ""`
cSpace=`echo -ne ""`
# Save terminal properties. When read -s reads a terminal key, the terminal's properties are temporarily changed.
#If the program is unfortunately killed during read -s, it may cause terminal confusion.
#Terminal attributes need to be restored when the program exits.
sTTY=`stty -g`
#Capture exit signal
trap "MyExit;" INT TERM
trap "MyExitNoSub;" $sigExit
#Hide cursor
echo -ne "[?25l"
while (( 1 ))
do
#Read input. Note -s does not echo, -n reads a character and returns immediately
read -s -n 1 key
aKey[0]=$
aKey[1]=$
aKey [2]=$key
sig=0
#Determine what key was entered
if [[ $key == $cESC && $ == $cESC ]]
then
#ESC key
MyExit
elif [[ $ == $cESC && $ == "[" ]]
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 #Spacebar
elif [[ $key == "Q" || $key == "q" ]] #Q, q
then
MyExit
fi
if [[ $sig != 0 ]]
then
#Send a message to another process
kill -$sig $pidDisplayer
fi
done
}
#Restore before exit
function MyExitNoSub()
{
local y
#Restore terminal attributes
stty $sTTY
((y = iTop + iTrayHeight + 4))
#Show cursor
echo -e "[?25h[$;0H"
exit
}
function MyExit()
{
#Notification display process needs to exit
kill -$sigExit $pidDisplayer
MyExitNoSub
}
#Handle display and game The main function of the process
function RunAsDisplayer()
{
local sigThis
InitDraw
#Mount various signal processing functions
trap "sig=$sigRotate;" $sigRotate
trap "sig=$sigLeft;" $sigLeft
trap "sig=$sigRight;" $sigRight
trap "sig=$sigDown;" $sigDown
trap "sig=$sigAllDown ;" $sigAllDown
trap "ShowExit;" $sigExit
while (( 1 ))
do
#According to the current speed level iLevel, set the number of corresponding loops
for ((i = 0; i < 21 - iLevel; i++))
do
sleep 0.02
sigThis=$sig
sig=0
#according to sig The variable determines whether the corresponding signal is received
if ((sigThis == sigRotate)); then BoxRotate; #Rotate
elif ((sigThis == sigLeft)); then BoxLeft; #Move one column to the left
elif ((sigThis == sigRight)); then BoxRight; #Move one column to the right
elif ((sigThis == sigDown)); then BoxDown; #Down one row
elif ((sigThis == sigAllDown)); then BoxAllDown ; #fall to the end
fi
done
#kill -$sigDown $$
BoxDown #drop a line
done
}
#BoxMove(y , x), test whether the moving block can be moved to the position of (x, y), return 0 yes, 1 not. > yTest=
xTest=
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
(( y = $ + yTest))
((x = $ + xTest))
if (( y < 0 || y >= iTrayHeight || x < 0 || x >= iTrayWidth) )
then
#hit the wall
return 1
fi
if ((${iMap[y * iTrayWidth + x]} != -1 ))
then
# Collided into other existing blocks
return 1
fi
done
return 0;
}
#Replace the currently moving block Put it on your back
http://www.bkjia.com/PHPjc/508515.html
www.bkjia.com