-
Notifications
You must be signed in to change notification settings - Fork 4
/
bong.sh
executable file
·187 lines (149 loc) · 2.88 KB
/
bong.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
#!/bin/bash
trap "stty $(stty -g)" EXIT
stty -echo -icanon -icrnl time 0 min 0
top=5
bottom=29
left=10
right=109
paddleLeft=$((top+(bottom-top)/2))
paddleRight=$((top+(bottom-top)/2))
paddleColor=1
paddleLen=3
scoreLeft=0
scoreRight=0
ballX=$((left+(right-left)/2))
ballY=$((top+(bottom-top)/2))
speedX=1
speedY=1
lines=$(tput lines)
cols=$(tput cols)
scoreLeft=0
scoreRight=0
gameOver=0
iteration=0
iterationOld=0
run=1
laststatus=""
status() {
laststatus=$1
}
update() {
local key=$(cat -v)
#echo -n $key
if [ "$key" == "q" ]; then
run=0
fi
local dy=0
if [ "$key" == "^[[A" ]; then
dy=$((dy-1))
fi
if [ "$key" == "^[[B" ]; then
dy=$((dy+1))
fi
paddleLeft=$((paddleLeft+dy))
if [ $ballY -eq $top ]; then
status "Top wall"
speedY=1
elif [ $ballY -eq $bottom ]; then
status "Bottom wall"
speedY=-1
fi
# Test whether somebody loses
if [ $ballX -eq $left ]; then
if [ $ballY -gt $paddleLeft ] && [ $ballY -lt $((paddleLeft + paddleLen)) ]; then
status "Left paddle"
speedX=1
else
status "Left wall"
scoreRight=$((++scoreRight))
speedX=1
ballX=$((left+(right-left)/2))
fi
elif [ $ballX -eq $right ]; then
if [ $ballY -gt $paddleRight ] && [ $ballY -lt $((paddleRight + paddleLen)) ]; then
status "Right paddle"
speedX=-1
else
status "Right wall"
scoreLeft=$((++scoreLeft))
speedX=-1
ballX=$((left+(right-left)/2))
fi
fi
if [ $gameOver -eq 1 ]; then
status "Game Over"
else
let ballX=ballX+speedX
let ballY=ballY+speedY
fi
}
fillXY() {
echo -ne "\033[$1;"$2"f "
}
echoXY() {
echo -ne "\033[$1;"$2"f"$3
}
clearXY() {
echo -ne '\0033\0143'
# hide the cursor
tput civis -- invisible
}
render() {
# update color
if [ $iteration -ge $((iterationOld+10)) ]; then
iterationOld=$iteration
paddleColor=$((++paddleColor))
[ $paddleColor -ge 8 ] && paddleColor=1
fi
buffer=$(
# left paddle
tput setab $paddleColor
for (( pY=$paddleLeft; pY < $((paddleLeft+paddleLen)); pY++ ))
do
fillXY $pY $left
done
# right paddle
for (( pY=$paddleRight; pY < $((paddleRight+paddleLen)); pY++ ))
do
fillXY $pY $right
done
# ball
tput setab 1
fillXY $ballY $ballX
# score
tput setab 0
tput setaf 2
echoXY 2 40 $scoreLeft
echoXY 2 80 $scoreRight
# corners
tput setab 6
for (( borderY=$((top-1)); borderY <= $((bottom+1)); borderY++ )); do
if [ $borderY -eq $((top-1)) ] || [ $borderY -eq $((bottom+1)) ]; then
for (( borderX=$left; borderX <= $right; borderX++ )); do
fillXY $borderY $borderX
done
fi
fillXY $borderY $((left-1))
fillXY $borderY $((right+1))
done
# show status
tput setab 3
tput setaf 0
echoXY $((lines-3)) 0 $laststatus
) # end buffer
tput setab 0
clearXY
echo -n $buffer
}
main() {
while [ $run -eq 1 ]; do
update
render
let iteration=iteration+1
sleep 0.05
done;
clearXY
# show cursor again
tput cnorm -- normal
}
main