Linux/Unix Shell Scripting - Ayuda con script bash

 
Vista:

Ayuda con script bash

Publicado por Ferran (1 intervención) el 25/05/2017 15:49:15
He hecho un script de un juego, que vas pillando cosas y suma puntos. Necesitaria que alguien me ayudase a incluir un carácter que apareciera aleatoriamente dentro del area de juego y al tocarlo me descontara puntos o me matara, alguna suguerencia? El script:

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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
clear
 
 
#vora dal
tput setaf 6
tput cup 1 1
x=1
while [ $x -le 40 ];do
	printf %b "\x1b(0\x71\x1b(B"
	let x=x+1
done
#vora baix
tput setaf 6
tput cup 20 1
x=1
while [ $x -le 40 ];do
	printf %b "\x1b(0\x71\x1b(B"
	let x=x+1
done
#vora esquerra
tput setaf 6
tput cup 1 1
x=1
while [ $x -le 20 ];do
	tput cup $x 1
	printf %b "\x1b(0\x78\x1b(B"
	let x=x+1
done
#vora dreta
tput setaf 6
tput cup 1 40
x=1
while [ $x -le 20 ];do
	tput cup $x 40
	printf %b "\x1b(0\x78\x1b(B"
	let x=x+1
done
 
#cantonades
tput cup 1 1
printf %b "\x1b(0\x6c\x1b(B"
tput cup 1 40
printf %b "\x1b(0\x6b\x1b(B"
tput cup 20 1
printf %b "\x1b(0\x6d\x1b(B"
tput cup 20 40
printf %b "\x1b(0\x6a\x1b(B"
 
#situació inicial
tput setaf 4
tput cup 10 20
printf %b "*"
 
tput cup 1 41
 
echo "Score: 0"
 
tecla=1
function massanetes(){
 
while [ $tecla -ne 0 ]; do
	declare -A myglobal
 
m1lin=$((RANDOM%19+2))
m1col=$((RANDOM%39+2))
tput cup $m1lin $m1col
printf %b "@"
 
m2lin=$((RANDOM%19+2))
m2col=$((RANDOM%39+2))
tput cup $m2lin $m2col
printf %b "@"
 
m3lin=$((RANDOM%19+2))
m3col=$((RANDOM%39+2))
tput cup $m3lin $m3col
printf %b "@"
 
m4lin=$((RANDOM%19+2))
m4col=$((RANDOM%39+2))
tput cup $m4lin $m4col
printf %b "@"
 
m5lin=$((RANDOM%19+2))
m5col=$((RANDOM%39+2))
tput cup $m5lin $m5col
printf %b "@"
 
myglobal["m1lin"]=$m1lin
myglobal["m1col"]=$m1col
 
myglobal["m2lin"]=$m2lin
myglobal["m2col"]=$m2col
 
myglobal["m3lin"]=$m3lin
myglobal["m3col"]=$m3col
 
myglobal["m4lin"]=$m4lin
myglobal["m4col"]=$m4col
 
myglobal["m5lin"]=$m5lin
myglobal["m5col"]=$m5col
declare -p myglobal > /dev/shm/foo
 
sleep 10
tput cup $m1lin $m1col
tput setaf 5
printf " "
 
tput cup $m2lin $m2col
tput setaf 5
printf " "
 
tput cup $m3lin $m3col
tput setaf 5
printf " "
 
tput cup $m4lin $m4col
tput setaf 5
printf " "
 
tput cup $m5lin $m5col
tput setaf 5
printf " "
 
done
}
massanetes &
 
tput setaf 9
tput cup 22 0
 
contador=0
tecla=1
posxlin=10
posycol=20
 
while [ $tecla -ne 0 ];do
	read -s -n 1 tecla
	#read -p "Mou: (8-dalt, 2-baix, 4-esquerra, 6-dreta, 0-sortir)" tecla
	tput cup 22 0
 
	case $tecla in
 
		8)
		tput cup $posxlin $posycol
		printf %b " "
		let posxlin=posxlin-1
		if [ $posxlin -eq 1 ]; then
			let posxlin=posxlin+1
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
		else
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
			. /dev/shm/foo
			if [ $posxlin -eq ${myglobal["m1lin"]} ] && [ $posycol -eq ${myglobal["m1col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m2lin"]} ] && [ $posycol -eq ${myglobal["m2col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m3lin"]} ] && [ $posycol -eq ${myglobal["m3col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m4lin"]} ] && [ $posycol -eq ${myglobal["m4col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m5lin"]} ] && [ $posycol -eq ${myglobal["m5col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
		fi
		;;
		2)
		tput cup $posxlin $posycol
		printf %b " "
		let posxlin=posxlin+1
		if [ $posxlin -eq 20 ]; then
			let posxlin=posxlin-1
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
		else
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
			. /dev/shm/foo
			if [ $posxlin -eq ${myglobal["m1lin"]} ] && [ $posycol -eq ${myglobal["m1col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m2lin"]} ] && [ $posycol -eq ${myglobal["m2col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m3lin"]} ] && [ $posycol -eq ${myglobal["m3col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m4lin"]} ] && [ $posycol -eq ${myglobal["m4col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m5lin"]} ] && [ $posycol -eq ${myglobal["m5col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
		fi
		;;
		4)
		tput cup $posxlin $posycol
		printf %b " "
		let posycol=posycol-1
		if [ $posycol -eq 1 ]; then
			let posycol=posycol+1
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
		else
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
			. /dev/shm/foo
			if [ $posxlin -eq ${myglobal["m1lin"]} ] && [ $posycol -eq ${myglobal["m1col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m2lin"]} ] && [ $posycol -eq ${myglobal["m2col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m3lin"]} ] && [ $posycol -eq ${myglobal["m3col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m4lin"]} ] && [ $posycol -eq ${myglobal["m4col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m5lin"]} ] && [ $posycol -eq ${myglobal["m5col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
		fi
		;;
		6)
		tput cup $posxlin $posycol
		printf %b " "
		let posycol=posycol+1
		if [ $posycol -eq 39 ]; then
			let posycol=posycol-1
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
		else
			tput cup $posxlin $posycol
			tput setf 3
			printf %b "*"
			. /dev/shm/foo
			if [ $posxlin -eq ${myglobal["m1lin"]} ] && [ $posycol -eq ${myglobal["m1col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m2lin"]} ] && [ $posycol -eq ${myglobal["m2col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m3lin"]} ] && [ $posycol -eq ${myglobal["m3col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m4lin"]} ] && [ $posycol -eq ${myglobal["m4col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
		fi
			if [ $posxlin -eq ${myglobal["m5lin"]} ] && [ $posycol -eq ${myglobal["m5col"]} ]; then
			let contador=contador+1
			tput cup 1 41
			echo "Score: $contador"
			fi
		fi
		;;
		0) killall -e joc2.sh
		;;
		*) echo "Aquest moviment no està permés"
		;;
	esac
	tput cup 22 0
 done
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder