-
Notifications
You must be signed in to change notification settings - Fork 0
/
snash.sh
executable file
·282 lines (272 loc) · 5.51 KB
/
snash.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
#
# Snash
#
# Snake in bash, main file
#
# @author Timon Ole Ensel
# https://github.com/forgottosave/snash
#
# @license MIT
#
#!/bin/bash
##### GLOBAL VARs
# game fixed
SPF=130 # milliseconds per frame
Y_MAX=16
X_MAX=32
MODE=5
INIT_SIZE=3 # initial snake size
APL_PLUS=10
APL_MINUS=-10
# game state
DIR="" # player direction
POS=$(($Y_MAX / 2 * $X_MAX + $X_MAX / 2))
APP="" # apple position
ACN=0 # apple existing counter
ARR=1 # apple respawn rate (value>=1, 1=highest)
SCORE=0 # game score
# colors
RED='\033[0;31m'
GRN='\033[0;32m'
BLU='\033[0;34m'
YLW='\033[0;33m'
# characters
PLR="#"
NOO="."
APL="O"
# additional files needed
THIS_DIR=$(dirname "$0")
F_STARTFRAMES="$THIS_DIR/resources/.startupframes"
F_HELPTEXT="$THIS_DIR/resources/.helptext"
F_SCORES="$THIS_DIR/.scores"
# fifo array for positions
declare -a FIFO
##### ARGUMENT PARSING
case $1 in
-h|--help)
cat $F_HELPTEXT
exit 0
;;
-s|--scores)
cat .scores
exit 0
;;
-f|--fullscreen)
Y_MAX=$(($(tput lines) - 2)) #16
X_MAX=$(($(tput cols) - 1)) #32
POS=$(($Y_MAX / 2 * $X_MAX + $X_MAX / 2))
;;
-easy)
MODE='EASY (0) '
SPF=200
INIT_SIZE=2
APL_MUNUS=0
;;
-medium) #Default difficulty
MODE='MEDIUM (5) '
SPF=130
INIT_SIZE=3
APL_MINUS=-10
;;
-hard)
MODE='HARD (10) '
SPF=90
INIT_SIZE=4
APL_MINUS=-20
;;
-d)
# change game difficulty
MODE="$2"
SPF=200
INIT_SIZE=2
APL_MINUS=0
[[ $2 -le 0 ]] && break;
SPF=$((2 * "$SPF" / "$MODE" + 50))
[[ $SPF -le 10 ]] && SPF=10
[[ $SPF -ge 200 ]] && SPF=200
INIT_SIZE=$(("$MODE" / 4 + "$INIT_SIZE"))
APL_MINUS=$(("$APL_MINUS" - "$MODE" * 2))
;;
esac
##### METHODS
# game end
stop_game() { # $1=end_string
printf "\033[5D\033[s\033[1A\033[K$1 Score: $SCORE\033[u"
touch $F_SCORES
echo "$MODE // Death: $1 // Score: $SCORE" >> .scores
exit 0
}
# define interrupt action
interrupt() {
stop_game "Game interrupted by user."
}
trap interrupt INT
# draw one point on screen
draw() { # $1=position $2=character
x=$(pos_to_x "$1")
y=$(pos_to_y "$1")
# print new pos
printf "\033[1D\033[s\033[${y}A\033[${x}C$2\033[u"
}
draw_det() { # $1=y $2=x $3=character
printf "\033[1D\033[s\033[$1A\033[$2C$2\033[u"
}
# own collision
# @return 0 if dead, 1 if alive
player_dead() {
#echo "Score: $SCORE -- Death: head ${POS} found in snake ${FIFO[@]}" >log
[[ " ${FIFO[*]} " =~ " ${POS} " ]] && return 0 || return 1
}
# keyboard input to player direction
update_pos() { # $1 = user input
old_pos=$POS
case $DIR in
w|W|k|K)
POS=$(($POS + $X_MAX));;
a|A|h|H)
POS=$(($POS - 1));;
s|S|j|J)
POS=$(($POS - $X_MAX));;
d|D|l|L)
POS=$(($POS + 1));;
*)
printf "\033[1A\033[K"
echo "frame=$var, ---PAUSED---, SCORE=$SCORE"
;;
esac
# check game bounds
old_x=$(pos_to_x "$old_pos")
old_y=$(pos_to_y "$old_pos")
new_x=$(pos_to_x "$POS")
new_y=$(pos_to_y "$POS")
if (($new_y < 2)) || (($new_y > ($Y_MAX + 1))) || (! ((old_y == new_y)) && ! ((old_x == new_x))) ; then
stop_game "Game Over (hit wall)"
fi
}
# updates apple spawning & despawning
update_apple() { # $1=current_frame
ACN=$(($ACN + 1))
# delete apple if existing time too long
if (($ACN > 100)) && ! [[ $APP == "" ]]; then
ACN=0
draw $APP "${NOO}"
APP=""
SCORE=$(($SCORE + $APL_MINUS))
# yellow apple if close to delete
elif (($ACN > 80)) && ! [[ $APP == "" ]]; then
draw $APP "${YLW}${APL}"
fi
# only spawn every 20th frame and if no other exists
#if (($1 % 20)) || ! [[ $APP == "" ]]; then
if (($1 % $ARR)) || ! [[ $APP == "" ]]; then
return
fi
# spawn apple
ACN=0
APP=$((($RANDOM % ($X_MAX * $Y_MAX)) + $X_MAX + 1))
if [[ " ${FIFO[*]} " =~ " ${APP} " ]]; then
APP=""
else
draw $APP "${GRN}${APL}"
fi
#draw $APP "${GRN}${APL}"
}
# player position
pos_to_x() {
ret=$(($1 % $X_MAX))
echo "$ret"
}
pos_to_y() {
ret=$(($1 / $X_MAX + 1))
echo "$ret"
}
# increase player score
increase_score() {
SCORE=$(($SCORE + $APL_PLUS))
SPF=$(($SPF - 1))
}
# updates player for next frame
update_player() {
# check for apple
if [[ "$APP" -eq "$POS" ]]; then
APP=""
FIFO+=("$POS")
increase_score
fi
# push new pos
FIFO+=("$POS")
draw $POS "${GRN}${PLR}"
# pop tail
tail_pos=${FIFO[0]}
FIFO=("${FIFO[@]:1}")
draw $tail_pos "${RED}${NOO}"
}
# startup sequence
startup() {
# draw fancy start screen stuff
for i in {00..64}; do
grep -A 5 "$i" $F_STARTFRAMES
sleep .02
printf "\033[6A"
done
# create player
for i in $(seq 1 $INIT_SIZE); do
FIFO+=("$POS")
done
# start
echo "Press any key to start..."
read -n 1 input
printf "\033[1D\033[1A"
DIR='w'
# draw gamepanel & start
for row in $(seq 0 $Y_MAX); do
line=""
for col in $(seq 0 $(($X_MAX - 2))); do
line="${line}${NOO}"
done
echo " ${line} "
done
}
# calculates and loads one frame
# @return 0 if dead, 1 if alive after frame
loadframe() {
printf "\033[1D"
# print debug info"
printf "\033[1A\033[K"
echo "frame=$var, input=$input, DIR=$DIR, SCORE=$SCORE"
# frame
printf "\033[s"
update_pos
if player_dead && [[ ! $DIR == "" ]]; then
return 0
fi
update_player
update_apple "$var"
printf "\033[u"
# catch user input
read -n 1 -s -t .02 input
if [[ $? -gt 128 ]]; then
return 1
fi
DIR="$input";
return 1
}
##### GAME
startup
# game loop
starttime=`date +%s%N`
var=0
while true; do
newtime=`date +%s%N`
nextframe=$(($starttime + $SPF * 1000000))
if [[ "$newtime" -gt "$nextframe" ]]; then
starttime=`date +%s%N`
if loadframe; then
break
fi
fi
#sleep 1
var=$((var + 1))
done
# game ending
stop_game "Game Over (don't eat yourself)"