-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamaze.sh
executable file
·222 lines (210 loc) · 5.38 KB
/
amaze.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
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
player_moving=0
# 0: not moving, 1: left (+x), 2: right (-x), 3: bottom (+y), 4: top (-y)
player_x=14
player_y=9
player_width=4
player_height=2
# level
width=0
height=0
set_dimensions() {
local j=0;
for (( j = 0; j < ${#BLOCKS[@]}; $(( j+=4 )) )); do
local current_max_width=$((${BLOCKS[$j]} + ${BLOCKS[$j+2]}));
local current_max_height=$((${BLOCKS[$j+1]} + ${BLOCKS[$j+3]}));
if [[ $current_max_width -gt $width ]]; then
width=$current_max_width
fi
if [[ $current_max_height -gt $height ]]; then
height=$current_max_height
fi
done
}
move_block(){ # move player x steps in direction
if [[ "$player_moving" -eq "0" ]]; then
return;
fi
# get new position
block_x=${BLOCKS[0]}
block_y=${BLOCKS[1]}
block_width=${BLOCKS[2]}
block_height=${BLOCKS[3]}
new_x=$block_x
new_y=$block_y
case "$1" in
1)
new_x=$(( block_x+1 ))
;;
2)
new_x=$(( block_x-1 ))
;;
3)
new_y=$(( block_y+1 ))
;;
4)
new_y=$(( block_y-1 ))
;;
esac
# check collision
collides=0
for (( j = 4; j < ${#BLOCKS[@]}; $(( j+=4 )) )); do
block2_x=${BLOCKS[$j]}
block2_y=${BLOCKS[$j+1]}
block2_width=${BLOCKS[$j+2]}
block2_height=${BLOCKS[$j+3]}
block2_area_x=$(( block2_x + block2_width - 1 ))
block2_area_y=$(( block2_y + block2_height - 1 ))
case "$1" in
1)
if [[ $(( new_x + block_width - 1 )) -eq $(( block2_x )) ]]; then
if [[ $new_y -ge $block2_y && $new_y -le $block2_area_y ]] || [[ $(( new_y + block_height - 1 )) -ge $block2_y && $new_y -le $block2_y ]]; then
collides=1
fi
fi
;;
2)
if [[ $(( new_x )) -eq $(( block2_area_x )) ]]; then
if [[ $new_y -ge $block2_y && $new_y -le $block2_area_y ]] || [[ $(( new_y + block_height - 1 )) -ge $block2_y && $new_y -le $block2_y ]]; then
collides=1
fi
fi
;;
3)
if [[ $(( new_y + block_height - 1 )) -eq $(( block2_y )) ]]; then
if [[ $new_x -ge $block2_x && $new_x -le $block2_area_x ]] || [[ $(( new_x + block_width - 1 )) -ge $block2_x && $new_x -le $block2_x ]]; then
collides=1
fi
fi
;;
4)
if [[ $(( new_y )) -eq $(( block2_area_y )) ]]; then
if [[ $new_x -ge $block2_x && $new_x -le $block2_area_x ]] || [[ $(( new_x + block_width - 1 )) -ge $block2_x && $new_x -le $block2_x ]]; then
collides=1
fi
fi
;;
esac
done
if [[ "$collides" -eq 1 ]]; then
player_moving=0
else
player_x=$new_x
player_y=$new_y
fi
}
get_movement_direction(){
if [[ "$player_moving" -eq "0" ]]; then
while :
do
read -t 1 -n 1 key
case "$key" in
l)
player_moving=1
break;
;;
h)
player_moving=2
break
;;
j)
player_moving=3
break
;;
k)
player_moving=4
break
;;
esac
done
fi
}
position_is_cleared(){
local c=_BLOCK_$1_$2_IS_CLEAR
if [[ $c -eq 1 ]];
then
return 1
else
return 0
fi
}
set_objects(){
BLOCKS=( $player_x $player_y $player_width $player_height 22 9 4 2 0 1 1 28 26 1 1 28 0 0 27 1 0 28 27 1 1 1 4 2 1 7 4 2 5 11 5 13 14 22 4 2 22 26 4 2 18 1 4 2 )
# each 4 elements represent one block
# order: x y width height
# first 4 are reserved for player
}
render(){
final_string=""
has_missing_blocks=0
for (( i = 0; i <= $(( height-1 )); i++ )); do
for (( k = 0; k <= $(( width-1 )); k++ )); do
printed=0
for (( j = 0; j < ${#BLOCKS[@]}; $(( j+=4 )) )); do
block_x=${BLOCKS[$j]}
block_y=${BLOCKS[$j+1]}
block_width=${BLOCKS[$j+2]}
block_height=${BLOCKS[$j+3]}
block_area_x=$(( block_x + block_width - 1 ))
block_area_y=$(( block_y + block_height - 1 ))
if [[ $i -ge $block_y ]] && [[ $i -le $block_area_y ]]; then
if [[ ( $k -ge $block_x ) ]] && [[ ( $k -le $block_area_x ) ]]; then
if [[ "$j" -ne "0" ]]; then
final_string="$final_string|"
else
final_string="$final_string${RED}|${NC}"
if position_is_cleared "$k" "$i" -eq 0;
then
declare -r -g "_BLOCK_"$k"_"$i"_IS_CLEAR"=1
fi
fi
printed=1
break
fi
fi
done
if [[ "$printed" -eq "0" ]]; then
if position_is_cleared "$k" "$i" -eq 0;
then
final_string="$final_string."
has_missing_blocks=1
else
final_string="$final_string "
fi
fi
done
final_string="$final_string\n"
done
if [[ $has_missing_blocks -eq 0 ]];
then
final_string="$final_string\nYou cleared the level!"
fi
if [[ ${#old_string} -ne 0 ]]; then
for (( i = 0; i < ${#finaL_string}; i++ )); do
if [[ ${old_string:$i:1} != ${final_string:$i:1} ]]; then
continue;
fi
row=$((i/height))
col=$((i%height))
echo -ne "\033[<$row>;<$col>H${final_string:$i:1}"
done
else
clear
echo -ne "$final_string"
fi
}
cycle(){
while true
do
set_objects
render
get_movement_direction
move_block $player_moving
sleep 0.01 # you can try lower and higher values
done
}
set_objects
set_dimensions
cycle