Inconveniente tratando de hacer juego SNAKE
Publicado por Julian (3 intervenciones) el 28/05/2014 00:02:58
Hola compañeros programadores, tengo un problema con el código en una acción del procedure moverSegmento.
Si puede ayudarme lo re agradezco.
Si puede ayudarme lo re agradezco.
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//PROGRAMA - INICIO
program x;
uses
crt;
const
REA_INTERVALO_DE_MOVIMIENTO = 200;
CHA_VACIO = '\';
CHA_LADRILLO = '#';
CHA_ESPACIO = ' ';
INT_LIMITE_DE_COLUMNAS = 100;
INT_LIMITE_DE_FILA = 100;
SEG_CABEZA_IZQUIERDA = '<';
SEG_CABEZA_DERECHA = '>';
SEG_CABEZA_ABAJO = 'v';
SEG_CABEZA_ARRIBA = '^';
SEG_CUERPO = 'O';
IZQUIERDA = 1;
DERECHA = 2;
ABAJO = 3;
ARRIBA = 4;
//*TIPOS - INICIO
type matrizChar = array[1..INT_LIMITE_DE_COLUMNAS, 1..INT_LIMITE_DE_FILA] of char;
type tipoSerpiente = record
segmento: integer;
posicionX: integer;
posicionY: integer;
direccion: integer;
largo: integer;
end;
//*TIPOS - FIN
var
ancho, alto, largoDeSerpiente: integer;
mapa: matrizChar;
muerta: boolean;
direccion, i1, i2: integer;
serpiente: array[1..(INT_LIMITE_DE_COLUMNAS * INT_LIMITE_DE_FILA)] of tipoSerpiente;
//*PROCEDIMIENTOS Y FUNCIONES - INICIO
function limiteXDelMapa(mapa: matrizChar): integer;
//**Funcion limiteXDelMapa(mapa: matrizChar):
//este procedimiento obtiene el valor del limite X del mapa
var
indice, x: integer;
begin
x := 0;
for indice := 1 to INT_LIMITE_DE_COLUMNAS do
begin
if mapa[indice, 1] <> CHA_VACIO then
x := x + 1;
end;
limiteXDelMapa := x;
end;
//limiteXDelMapa fin
function limiteYDelMapa(mapa: matrizChar): integer;
//**Funcion limiteYDelMapa(mapa: matrizChar):
//este procedimiento obtiene el valor del limite Y del mapa
var
indice, y: integer;
begin
y := 0;
for indice := 1 to INT_LIMITE_DE_FILA do
begin
if mapa[1, indice] <> CHA_VACIO then
y := y + 1;
end;
limiteYDelMapa := y;
end;
//limiteXDelMapa fin
function obtenerDireccionDeSegmento(segmento: integer): integer;
var
tempInt: integer;
begin
tempInt := segmento - 1;
if (tempInt > 0) then
begin
obtenerDireccionDeSegmento := serpiente[tempInt].direccion;
end;
end;
procedure inicializarMapa(var matriz: matrizChar; alto, ancho: integer);
//**Procedimiento inicializarMapa(var matriz: matrizChar; ancho, alto: integer):
//este procedimiento hace que: viendo a la variable matrizChar desde un plano bidimensional, sus bordes sean igual a la constante CHA_LADRILLO
var
indice1, indice2: integer;
begin
ancho := ancho + 2;
alto := alto + 2;
if (alto <= INT_LIMITE_DE_FILA) and (ancho <= INT_LIMITE_DE_COLUMNAS) then
begin
for indice1 := 1 to ancho do
begin
for indice2 := 1 to alto do
begin
if (indice1 = 1) or (indice1 = ancho) then
begin
matriz[indice1, indice2] := CHA_LADRILLO;
end
else
begin
if (indice2 = 1) or (indice2 = alto) then
begin
matriz[indice1, indice2] := CHA_LADRILLO;
end
else
begin
matriz[indice1, indice2] := CHA_ESPACIO;
end;
end;
end;
end;
end;
end;
//**inicializarMapa fin
procedure limpiarMatriz(var matriz: matrizChar);
//**Procedimiento limpiarMatriz(var matriz: matrizChar):
//este procedimiento asigna una constante CHA_VACIO a cada ðndice de la variable tipo matrizChar.
var
indice1, indice2: integer;
begin
for indice1 := 1 to INT_LIMITE_DE_COLUMNAS do
begin
for indice2 := 1 to INT_LIMITE_DE_FILA do
begin
matriz[indice1, indice2] := CHA_VACIO;
end;
end;
end;
//**limpiarMatriz fin
procedure escribirMapa(var mapa: matrizChar);
//**Procedimiento escribirMatriz(var matriz: matrizChar):
//este procedimiento escribe por pantalla los valores de los indices de la variable matrizChar en un plano bidimensional
var
x, y, indice1, indice2: integer;
begin
x := limiteXDelMapa(mapa);
y := limiteYDelMapa(mapa);
for indice1 := 1 to x do
begin
for indice2 := 1 to y do
Write(mapa[indice1, indice2]);
if (indice1 <> x) or (indice2 <> y) then
WriteLn();
end;
end;
//**escribirMatriz fin
procedure comienzo;
{ESTE PROCEDIMIENTO MARCA EL COMIENZO DEL JUEGO}
var
indiceColor, indice2: integer;
comienza: boolean;
begin
comienza := false;
indiceColor := 1;
ClrScr();
Write(' ______________________________________________________'); WriteLn;
Write(' / \'); WriteLn;
Write('| SSSS N N A K K EEEEE |'); WriteLn;
Write('| S NN N A A K K E |'); WriteLn;
Write('| SSS N N N AAAAA KK EEE |'); WriteLn;
Write('| S N NN A A K K E |'); WriteLn;
Write('| SSSS N N A A K K EEEEE |'); WriteLn;
Write(' \______________________________________________________/');
end;
//**comienzo fin
procedure serpienteCome();
{PRUEBA: ESTE PROCEDIMIENTO SE ENCARGA DE AGREGAR UN SEGMENTO A LA SERPIENTE}
begin
serpiente[largoDeSerpiente + 1].direccion := serpiente[largoDeSerpiente].direccion;
serpiente[largoDeSerpiente + 1].posicionX := serpiente[largoDeSerpiente].posicionX;
serpiente[largoDeSerpiente + 1].posicionY := serpiente[largoDeSerpiente].posicionY;
serpiente[largoDeSerpiente + 1].segmento := largoDeSerpiente + 1;
end;
//**serpienteCome fin
procedure moverSegmento(segmento: integer);
var
x, y, direccion: integer;
begin
x := serpiente[segmento].posicionX;
y := serpiente[segmento].posicionY;
direccion := serpiente[segmento].direccion;
if largoDeSerpiente > 1 then
begin
case direccion of
IZQUIERDA:
begin
//ACÁ SE PRESENTA EL PROBLEMA, TIRA ERROR POR QUÉ?
serpiente[segmento].direccion := serpiente[segmento - 1].direccion;
end;
end;
end
else
begin
case serpiente[segmento].direccion of
IZQUIERDA:
begin
x := x - 1;
if mapa[x, y] <> CHA_LADRILLO then
begin
GotoXY(x + 1, y);
Write(CHA_ESPACIO);
serpiente[segmento].posicionX := x;
GotoXY(x, y);
Write(SEG_CABEZA_IZQUIERDA);
end
else
begin
muerta := true;
end;
end;
DERECHA:
begin
x := x + 1;
if mapa[x, y] <> CHA_LADRILLO then
begin
GotoXY(x - 1, y);
Write(CHA_ESPACIO);
serpiente[segmento].posicionX := x;
GotoXY(x, y);
Write(SEG_CABEZA_DERECHA);
end
else
begin
muerta := true;
end;
end;
ABAJO:
begin
y := y + 1;
if mapa[x, y] <> CHA_LADRILLO then
begin
GotoXY(x, y - 1);
Write(CHA_ESPACIO);
serpiente[segmento].posicionY := y;
GotoXY(x, y);
Write(SEG_CABEZA_ABAJO);
end
else
begin
muerta := true;
end;
end;
ARRIBA:
begin
y := y - 1;
if mapa[x, y] <> CHA_LADRILLO then
begin
GotoXY(x, y + 1);
Write(CHA_ESPACIO);
serpiente[segmento].posicionY := y;
GotoXY(x, y);
Write(SEG_CABEZA_ARRIBA);
end
else
begin
muerta := true;
end;
end;
end;
end;
end;
procedure juegoCorre(muerta: boolean);
var
indice, x, y: integer;
begin
while not muerta do
begin
for indice := 1 to largoDeSerpiente do
begin
moverSegmento(indice);
end;
Delay(REA_INTERVALO_DE_MOVIMIENTO);
end;
end;
//**juegoCorre fin
//*PROCEDIMIENTOS Y FUNCIONES - FIN
begin
comienzo();
WriteLn();
Write('Ingrese el ancho del mapa a jugar: ');
ReadLn(ancho);
Write('Ingrese el alto del mapa a jugar: ');
ReadLn(alto);
ClrScr();
limpiarMatriz(mapa);
inicializarMapa(mapa, ancho, alto);
escribirMapa(mapa);
muerta := false;
serpiente[1].direccion := IZQUIERDA;
serpiente[1].posicionX := 20;
serpiente[1].posicionY := 10;
serpiente[1].segmento := 1;
largoDeSerpiente := 1;
serpiente[2].direccion := IZQUIERDA;
serpiente[2].posicionX := 21;
serpiente[2].posicionY := 10;
serpiente[2].segmento := 2;
largoDeSerpiente := largoDeSerpiente + 1;
juegoCorre(muerta);
ReadKey();
end.
//PROGRAMA - FIN
Valora esta pregunta


0