-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-game-invader.sh
executable file
·145 lines (133 loc) · 2.09 KB
/
bash-game-invader.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
#!/bin/bash
if [[ $1 != "" ]]
then
cat << END
BashInvaders!
by Vidar 'koala_man' Holen
www.vidarholen.net
Originally an entry in the #linux.no 1KiB compo (thus the ugly source)
This is a pre-trim version with colors and proper tmp files. All bash.
Released under the GNU General Public License.
Control your ship with J and L, shoot with K.
Quit with Q or sigint.
Requires mktemp and sleep with fractions.
END
exit 0
fi
tput civis
cd /tmp
e=echo
c=clear
r=return
E="$e -ne "
A=$E\\033[
m() { $A$2\;$1\H
}
f() { $A\1\;3$2\m
}
trap z=SigInt SIGINT
g() {
$e ${K[$(($2*8+$1))]}
}
s() {
K[$(($2*8+$1))]=$3
}
u() {
[ $T = 0 ] && $r 0
m $S $((--T))
$E `f 3`"."
x=$((S-Y))
y=$((T-Z))
[ $((y%3)) = 0 -a $((x%6)) -lt 4 ] || $r 0
: $((y/=3)) $((x/=6))
[ "`g $x $y`" = 1 -a $x -le $o -a $x -ge $n -a $y -le $q -a $y -ge 0 ] || $r 0
[ $Q = 1 ] && z="You win!"
s $x $y 0
: $((Q--))
T=0
$r 1
}
a() {
w n +
w o -
h
}
w() {
d=0
for (( I=0; I<=q; I++ )) {
[ `g $(($1)) $I` = 1 ] && D=1
}
[ $D = 0 ] && : $(($1$2=1))
}
h() {
for (( I=q; I>=0; I--)) {
for (( J=n; J<=o; J++)) {
[ `g $J $I` = 1 ] && q=$I && $r
}
}
}
j() {
while read -n 1 S >/dev/null 2>&1
do
$e $S > $M
done
}
G=`mktemp`
L=`mktemp`
M=`mktemp`
N=`mktemp`
X=40
n=0
o=7
q=2
T=0
Y=2
Z=2
U=2
W=0
for (( Q=0; Q<24; Q++)) {
K[$Q]=1
}
j 0<&0 &
B=$!
until [ "$z" ]
do
: $((W++))
if [ -f $M ]
then
i=$(<$M)
rm $M
case "$i" in
q) z="Quit" ;;
j) X=$(($X-3)) ;;
l) X=$(($X+3)) ;;
k) [ $T = 0 ] && S=$((X+1)) && T=22
;;
esac
fi
rm $N
exec > $N
for (( J=0; J<=q; J++)) {
for (( I=n; I<=o; I++)) {
[ `g $I $J` = 1 ] && m $((I*6+Y)) $((J*3+Z)) && $e `f 4`/00\\
}
}
m $X 23
$e `f 2`"/|\\"
[ $T != 0 ] && u
a
m 0 0
exec > `tty`
$c
cat $N
sleep .1
[ $((W%2)) = 0 ] && : $((Y+=U)) && if [ $((Y+n*6)) -lt 2 -o $((Y+o*6)) -gt 75 ]
then
: $((U=-U)) $((Z+=2))
[ $((Z+q*3)) -le 20 ] || z="You lose!"
fi
done
$c
$e $z
rm $G $L $M $N $F &> /dev/null
kill $B