-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.sh
More file actions
110 lines (98 loc) · 2.67 KB
/
pong.sh
File metadata and controls
110 lines (98 loc) · 2.67 KB
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
paddle1=("█")
paddle2=("█")
space=" "
w=$(tput cols)
h=$(tput lines)
startx=`echo "$w/2" | bc`
starty=`echo "$h/2" | bc`
ballx=$startx
bally=$starty
vel_x=1
vel_y=-1
max_speed=2
position1x=3
position2x=$(( $w-2 ))
position1y=$(( $h-5 ))
position2y=$(( $h-5 ))
paddle1Pos=1
paddle2Pos=1
len=6
draw_paddle1() {
erase=$position1y
for ((i=0; i<len; i++)) {
echo -en "\033[${erase};${position1x}H$space"
erase=$(( $erase-1 ))
}
(( position1y += $1 ))
(( position1y = position1y > h ? h : position1y < len ? len : position1y ))
height=$position1y
for ((i=0; i<len; i++)); do
echo -en "\033[${height};${position1x}H$paddle1"
height=$(($height-1))
done
}
draw_paddle2() {
erase=$position2y
for ((i=0; i<len; i++)) {
echo -en "\033[${erase};${position2x}H$space"
erase=$(($erase-1))
}
(( position2y += $1 ))
(( position2y = position2y > h ? h : position2y < len ? len : position2y ))
height=$position2y
for ((i=0; i<len; i++)); do
echo -en "\033[${height};${position2x}H$paddle2"
height=$(($height-1))
done
}
tput civis
tput clear
clear
draw_paddle1 1;
draw_paddle2 1;
while [[ $q != q ]]; do
echo -en "\033[${bally};${ballx}H "
(( ballx += vel_x ))
(( bally += vel_y ))
echo -en "\033[${bally};${ballx}H●"
read -n 1 -s -t 0.05 q
case "$q" in
[wW] ) draw_paddle1 -2;;
[sS] ) draw_paddle1 2;;
[iI] ) draw_paddle2 -2;;
[kK] ) draw_paddle2 2;;
esac
(( ballx >= position2x - 1 || ballx <= position1x + 1 )) && (( vel_x = - vel_x ))
(( bally + vel_y < 1 )) && echo -en "\033[${bally};${ballx}H " && (( bally = 1 - bally - vel_y )) && (( vel_y = - vel_y )) &&
echo -en "\033[${bally};${ballx}H●"
(( bally + vel_y > h )) && echo -en "\033[${bally};${ballx}H " && (( bally = 2*h - 1 - bally - vel_y )) && (( vel_y = - vel_y )) &&
echo -en "\033[${bally};${ballx}H●"
if (( ballx <= position1x + 1 )); then
if (( bally <= position1y && bally >= position1y - len )); then
(( vel_y = bally - (position1y - len) - len/2 ))
(( ${vel_y//-/} > max_speed )) &&
vel_y=${vel-y//[0-9]*/$max_speed}
else
echo -en "\033[${bally};${ballx}H "
ballx=$startx
bally=$starty
vel_y=$(( RANDOM % max_speed + 1))
vell_x=1
fi
fi
if (( ballx >= position2x - 1 )); then
if (( bally <= position2y && bally >= position2y - len )); then
(( vel_y = bally - (position2y - len) - len/2 ))
(( ${vel_y//-/} > max_speed )) &&
vel_y=${vel-y//[0-9]*/$max_speed}
else
echo -en "\033[${bally};${ballx}H "
ballx=$startx
bally=$starty
vel_y=$(( RANDOM % max_speed + 1))
vell_x=-1
fi
fi
done
tput clear
tput cnorm