-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-game-tetris.sh
executable file
·449 lines (445 loc) · 11.7 KB
/
bash-game-tetris.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/bash
# Tetris Game
# 10.21.2003 xhchen
# http://read.pudn.com/downloads99/sourcecode/game/404711/tetris.sh__.htm
# Copyright (C) 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)
#size & position
iLeft=3
iTop=2
((iTrayLeft = iLeft + 2))
((iTrayTop = iTop + 1))
((iTrayWidth = 10))
((iTrayHeight = 15))
#style definition
cBorder=$cGreen
cScore=$cFuchsia
cScoreValue=$cCyan
#control signal
sigRotate=25
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29
sigExit=30
#boxes
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=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]})
countBox=(1 2 2 2 4 4 4 )
offsetBox=(0 1 3 5 7 11 15)
#score
iScoreEachLevel=50 #be greater than 7
#Runtime data
sig=0
iScore=0
iLevel=0
boxNew=() #new box
cBoxNew=0 #new box color
iBoxNewType=0 #new box type
iBoxNewRotate=0 #new box rotate degree
boxCur=() #current box
cBoxCur=0 #current box color
iBoxCurType=0 #current box type
iBoxCurRotate=0 #current box rotate degree
boxCurX=-1 #current X position
boxCurY=-1 #current Y position
iMap=()
for ((i = 0; i < iTrayHeight * iTrayWidth; i++)); do iMap[$i]=-1; done
RunAsKeyReceiver() {
local pidDisplayer key aKey sig cESC sTTY
pidDisplayer=$1
aKey=(0 0 0)
cESC=`echo -ne "\033"`
cSpace=`echo -ne "\040"`
sTTY=`stty -g`
trap "MyExit;" INT TERM
trap "MyExitNoSub;" $sigExit
echo -ne "\033[?25l"
while :
do
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
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
elif [[ $key == "S" || $key == "s" ]]; then sig=$sigDown
elif [[ $key == "A" || $key == "a" ]]; then sig=$sigLeft
elif [[ $key == "D" || $key == "d" ]]; then sig=$sigRight
elif [[ "[$key]" == "[]" ]]; then sig=$sigAllDown
elif [[ $key == "Q" || $key == "q" ]]
then
MyExit
fi
if [[ $sig != 0 ]]
then
kill -$sig $pidDisplayer
fi
done
}
MyExitNoSub(){
local y
stty $sTTY
((y = iTop + iTrayHeight + 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
for ((i = 0; i < 21 - iLevel; i++))
do
usleep 20000
sigThis=$sig
sig=0
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
}
BoxMove(){
local j i x y xTest yTest
yTest=$1
xTest=$2
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + yTest))
((x = ${boxCur[$i]} + xTest))
if (( y < 0 || y >= iTrayHeight || x < 0 || x >= iTrayWidth))
then
return 1
fi
if ((${iMap[y * iTrayWidth + x]} != -1 ))
then
return 1
fi
done
return 0;
}
Box2Map(){
local j i x y xp yp line
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + boxCurY))
((x = ${boxCur[$i]} + boxCurX))
((i = y * iTrayWidth + x))
iMap[$i]=$cBoxCur
done
line=0
for ((j = 0; j < iTrayWidth * iTrayHeight; j += iTrayWidth))
do
for ((i = j + iTrayWidth - 1; i >= j; i--))
do
if ((${iMap[$i]} == -1)); then break; fi
done
if ((i >= j)); then continue; fi
((line++))
for ((i = j - 1; i >= 0; i--))
do
((x = i + iTrayWidth))
iMap[$x]=${iMap[$i]}
done
for ((i = 0; i < iTrayWidth; i++))
do
iMap[$i]=-1
done
done
if ((line == 0)); then return; fi
((x = iLeft + iTrayWidth * 2 + 7))
((y = iTop + 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 = iTop + 14))
echo -ne "\033[3${cScoreValue}m\033[${y};${x}H${iLevel} "
fi
fi
echo -ne "\033[0m"
for ((y = 0; y < iTrayHeight; y++))
do
((yp = y + iTrayTop + 1))
((xp = iTrayLeft + 1))
((i = y * iTrayWidth))
echo -ne "\033[${yp};${xp}H"
for ((x = 0; x < iTrayWidth; x++))
do
((j = i + x))
if ((${iMap[$j]} == -1))
then
echo -ne " "
else
echo -ne "\033[1m\033[7m\033[3${iMap[$j]}m\033[4${iMap[$j]}m\040\040\033[0m"
fi
done
done
}
BoxDown(){
local y s
((y = boxCurY + 1))
if BoxMove $y $boxCurX
then
s="`DrawCurBox 0`"
((boxCurY = y))
s="$s`DrawCurBox 1`"
echo -ne $s
else
Box2Map
RandomBox
fi
}
BoxLeft(){
local x s
((x = boxCurX - 1))
if BoxMove $boxCurY $x
then
s=`DrawCurBox 0`
((boxCurX = x))
s=$s`DrawCurBox 1`
echo -ne $s
fi
}
BoxRight(){
local x s
((x = boxCurX + 1))
if BoxMove $boxCurY $x
then
s=`DrawCurBox 0`
((boxCurX = x))
s=$s`DrawCurBox 1`
echo -ne $s
fi
}
BoxAllDown(){
local k j i x y iDown s
iDown=$iTrayHeight
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + boxCurY))
((x = ${boxCur[$i]} + boxCurX))
for ((k = y + 1; k < iTrayHeight; k++))
do
((i = k * iTrayWidth + x))
if (( ${iMap[$i]} != -1)); then break; fi
done
((k -= y + 1))
if (( $iDown > $k )); then iDown=$k; fi
done
s=`DrawCurBox 0`
((boxCurY += iDown))
s=$s`DrawCurBox 1`
echo -ne $s
Box2Map
RandomBox
}
BoxRotate(){
local iCount iTestRotate boxTest j i s
iCount=${countBox[$iBoxCurType]}
((iTestRotate = iBoxCurRotate + 1))
if ((iTestRotate >= iCount))
then
((iTestRotate = 0))
fi
for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
do
boxTest[$j]=${boxCur[$j]}
boxCur[$j]=${box[$i]}
done
if BoxMove $boxCurY $boxCurX
then
for ((j = 0; j < 8; j++))
do
boxCur[$j]=${boxTest[$j]}
done
s=`DrawCurBox 0`
for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
do
boxCur[$j]=${box[$i]}
done
s=$s`DrawCurBox 1`
echo -ne $s
iBoxCurRotate=$iTestRotate
else
for ((j = 0; j < 8; j++))
do
boxCur[$j]=${boxTest[$j]}
done
fi
}
DrawCurBox(){
local i j t bDraw sBox s
bDraw=$1
s=""
if (( bDraw == 0 ))
then
sBox="\040\040"
else
sBox="\040\040"
s=$s"\033[1m\033[7m\033[3${cBoxCur}m\033[4${cBoxCur}m"
fi
for ((j = 0; j < 8; j += 2))
do
((i = iTrayTop + 1 + ${boxCur[$j]} + boxCurY))
((t = iTrayLeft + 1 + 2 * (boxCurX + ${boxCur[$j + 1]})))
s=$s"\033[${i};${t}H${sBox}"
done
s=$s"\033[0m"
echo -n $s
}
RandomBox(){
local i j t
#change current box
iBoxCurType=${iBoxNewType}
iBoxCurRotate=${iBoxNewRotate}
cBoxCur=${cBoxNew}
for ((j = 0; j < ${#boxNew[@]}; j++))
do
boxCur[$j]=${boxNew[$j]}
done
if (( ${#boxCur[@]} == 8 ))
then
#calculate current box's starting position
for ((j = 0, t = 4; j < 8; j += 2))
do
if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
done
((boxCurY = -t))
for ((j = 1, i = -4, t = 20; j < 8; j += 2))
do
if ((${boxCur[$j]} > i)); then i=${boxCur[$j]}; fi
if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
done
((boxCurX = (iTrayWidth - 1 - i - t) / 2))
echo -ne `DrawCurBox 1`
if ! BoxMove $boxCurY $boxCurX
then
kill -$sigExit ${PPID}
ShowExit
fi
fi
#clear old box
for ((j = 0; j < 4; j++))
do
((i = iTop + 1 + j))
((t = iLeft + 2 * iTrayWidth + 7))
echo -ne "\033[${i};${t}H "
done
#get a random new box
((iBoxNewType = RANDOM % ${#offsetBox[@]}))
((iBoxNewRotate = RANDOM % ${countBox[$iBoxNewType]}))
for ((j = 0, i = (${offsetBox[$iBoxNewType]} + $iBoxNewRotate) * 8; j < 8; j++, i++))
do
boxNew[$j]=${box[$i]};
done
((cBoxNew = ${colorTable[RANDOM % ${#colorTable[@]}]}))
#display new box
echo -ne "\033[1m\033[7m\033[3${cBoxNew}m\033[4${cBoxNew}m"
for ((j = 0; j < 8; j += 2))
do
((i = iTop + 1 + ${boxNew[$j]}))
((t = iLeft + 2 * iTrayWidth + 7 + 2 * ${boxNew[$j + 1]}))
echo -ne "\033[${i};${t}H\040\040"
done
echo -ne "\033[0m"
}
InitDraw(){
clear
RandomBox
RandomBox
local i t1 t2 t3
#draw border
echo -ne "\033[1m"
echo -ne "\033[3${cBorder}m\033[4${cBorder}m"
((t2 = iLeft + 1))
((t3 = iLeft + iTrayWidth * 2 + 3))
for ((i = 0; i < iTrayHeight; i++))
do
((t1 = i + iTop + 2))
echo -ne "\033[${t1};${t2}H\040\040"
echo -ne "\033[${t1};${t3}H\040\040"
done
((t2 = iTop + iTrayHeight + 2))
for ((i = 0; i < iTrayWidth + 2; i++))
do
((t1 = i * 2 + iLeft + 1))
echo -ne "\033[${iTrayTop};${t1}H\040\040"
echo -ne "\033[${t2};${t1}H\040\040"
done
echo -ne "\033[0m"
#draw score & level prompt
echo -ne "\033[1m"
((t1 = iLeft + iTrayWidth * 2 + 7))
((t2 = iTop + 10))
echo -ne "\033[3${cScore}m\033[${t2};${t1}HScore"
((t2 = iTop + 11))
echo -ne "\033[3${cScoreValue}m\033[${t2};${t1}H${iScore}"
((t2 = iTop + 13))
echo -ne "\033[3${cScore}m\033[${t2};${t1}HLevel"
((t2 = iTop + 14))
echo -ne "\033[3${cScoreValue}m\033[${t2};${t1}H${iLevel}"
echo -ne "\033[0m"
}
ShowExit(){
local y
((y = iTrayHeight + iTrayTop + 3))
echo -e "\033[${y};0HGameOver!\033[0m"
exit
}
#Start from here.
if [[ $1 != "--go" ]]
then
$0 --go&
RunAsKeyReceiver $!
exit
else
RunAsDisplayer
exit
fi