-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.sh
325 lines (271 loc) · 5.8 KB
/
final.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
function display {
DATA[0]=" # ###### # # # # # #### ####### ##### "
DATA[1]=" # # # # # # # # ## # # # # # # "
DATA[2]=" # # # # # # # # # # # # # # # #"
DATA[3]=" # # ###### #### # # # # # # # # # #"
DATA[4]=" ####### # # # # ####### # # # # # # # #"
DATA[5]=" # # # # # # # # # ## # # # # # "
DATA[6]=" # # # # # # # # # # #### ####### ##### "
echo
# virtual coordinate system is X*Y ${#DATA} * 8
## This is to put the title in the centre
REAL_OFFSET_X=$(($((`tput cols` - 56)) / 2))
REAL_OFFSET_Y=$(($((`tput lines` - 6)) / 2))
draw_char() {
stty -echo
V_COORD_X=$1
V_COORD_Y=$2
tput cup $((REAL_OFFSET_Y + V_COORD_Y)) $((REAL_OFFSET_X + V_COORD_X))
printf %c ${DATA[V_COORD_Y]:V_COORD_X:1}
}
trap 'exit 1' INT TERM
tput civis
clear
while :; do
for ((c=1; c <= 7; c++)); do
tput setaf $c
for ((x=0; x<${#DATA[0]}; x++)); do
for ((y=0; y<=6; y++)); do
draw_char $x $y
done
done
done
sleep 1
clear
break
done
}
display
stty -echo
# Init output
OLD_IFS="$IFS"
IFS=
DELAY=0.15
tput civis
clear
SCREEN_WIDTH=$(tput cols)
SCREEN_HEIGHT=$(($(tput lines) - 1))
WIDTH=$((40 % (SCREEN_WIDTH - 2)))
HEIGHT=$((14 % (SCREEN_HEIGHT - 2)))
HALF_WIDTH=$((WIDTH/2))
HALF_HEIGHT=$((HEIGHT/2))
TOP=$(((SCREEN_HEIGHT - HEIGHT) / 2))
LEFT=$(((SCREEN_WIDTH - WIDTH) / 2))
BOTTOM=$((TOP + HEIGHT))
RIGHT=$((LEFT + WIDTH))
STATE='stop'
plateX=$((LEFT + HALF_WIDTH))
plateW=5
plateS=""
ballY=$((BOTTOM - 1))
ballX=$((LEFT + HALF_WIDTH + plateW / 2))
ballDY=-1
ballDX=-1
ballColors=(52 88 124 160 196)
currentColor=1
ballChar="0"
state="stop"
lifes=39767
bricks=()
function putBrik {
index=1$1\0$2 #6854354645
briks[$index]=1
tput cup $1 $2
echo '+'
}
function drawBricks {
for (( row = $((TOP + 2)); row <= $((TOP + 7)); row++ ))
do
for (( column = $((LEFT + 3)); column <= $((RIGHT - 3)); column++ ))
do
if [ $(($column % 3)) == 0 ]
then
tput setaf 83
putBrik $row $column
fi
done
done
}
function drawBorder {
tput setaf 244
tput cup $((TOP - 5)) $((LEFT + 5))
echo " _ _ _ "
tput cup $((TOP - 4)) $((LEFT + 5))
echo " ___ ___| |_ ___ ___ ___|_|_| |"
tput cup $((TOP - 3)) $((LEFT + 5))
echo "| .'| _| '_| .'| | . | | . |"
tput cup $((TOP - 2)) $((LEFT + 5))
echo "|__,|_| |_,_|__,|_|_|___|_|___|"
line=""
tput setaf 241
for (( column = 0; column <= WIDTH; column++ ))
do
line+="~"
done
tput cup $((TOP - 1)) $LEFT
echo $line
tput cup $BOTTOM $LEFT
echo $line
for (( row = 0; row <= HEIGHT; row++ ))
do
tput cup $((TOP + row)) $((LEFT - 1))
echo "|"
tput cup $((TOP + row)) $((RIGHT + 1))
echo "|"
done
tput cup $((BOTTOM + 2)) $LEFT
echo " Press 'h' or 'l' to start playing"
}
function clearBall {
tput cup $ballY $ballX
echo " "
}
function drawBall {
tput setaf ${ballColors[$currentColor]}
tput cup $ballY $ballX
echo $ballChar
#currentColor=$(($currentColor + 1))
}
function resetBall {
clearBall
ballY=$((BOTTOM - 1))
ballX=$((plateX + plateW / 2))
ballDY=-1
ballDY=-1
drawPlate
drawBall
}
function move {
(sleep $DELAY && kill -ALRM $$) &
if [ $state != 'playing' ]
then
return
fi
clearBall
ballY=$((ballY + ballDY))
ballX=$((ballX + ballDX))
if [ $ballX -gt $RIGHT ] || [ $ballX -lt $LEFT ]
then
ballDX=$((-ballDX))
ballX=$((ballX + ballDX ))
fi
if [ $ballY -lt $TOP ]
then
ballDY=$((-ballDY))
ballY=$((ballY + ballDY ))
fi
if [ $ballY -gt $((BOTTOM - 1)) ]
then
if [ $ballX -le $((plateX + plateW)) ] && [ $ballX -ge $plateX ]
then
#ballX=$((plateX + plateW / 2))
ballDY=$((-ballDY))
ballY=$((ballY + ballDY + ballDY))
drawPlate
else
resetBall
drawBorder
state='stop'
lifes=$((lifes - 1))
fi
fi
index=1$ballY\0$ballX
if [ ${briks[$index]} ] && [ ${briks[$index]} == 1 ]
then
tput cup $ballY $ballX
drawBall
clearBall
ballDY=$((-ballDY))
ballY=$((ballY))
briks[$index]=0
fi
drawBall
tput setaf 7
tput cup $((BOTTOM + 2)) $LEFT
if [ $lifes -gt 0 ]
then
echo "Lifes: " $lifes " "
else
echo "Game over "
exitGame
fi
}
function calcPlateS {
plateS=" +"
for (( i = 0; i < $((plateW - 2)); i++ ))
do
plateS+="-"
done
plateS+="+ "
}
function drawPlate {
tput setaf 2
tput cup $((BOTTOM - 1)) $((plateX - 1))
echo $plateS
if [ $plateX == $LEFT ]
then
tput setaf 241
tput cup $((BOTTOM - 1)) $((plateX - 1))
fi
}
function exitGame {
echo "Goodbye!"
trap exit ALRM
tput cnorm
IFS="$OLD_IFS"
stty echo
exit 0
}
function startGame {
if [ $lifes -ge 0 ]
then
state='playing'
else
state='gameOver'
fi
}
trap move ALRM
calcPlateS
drawBricks
drawBorder
drawBall
drawPlate
resetBall
move
while :
do
read -s -n 1 key
case "$key" in
h)
if [ $state == 'stop' ]
then
ballDX=-1
startGame
else
if [ $plateX -gt $LEFT ]
then
plateX=$((plateX - 1))
drawPlate
drawBall
fi
fi
;;
l)
if [ $state == 'stop' ]
then
ballDX=1
startGame
else
if [ $plateX -lt $((RIGHT - $plateW)) ]
then
plateX=$((plateX + 1))
drawPlate
drawBall
fi
fi
;;
q)
exitGame
;;
esac
done