Juego de Pac Man
Juego de Pac Man
Juego de Pac Man
h>
2. #include <iostream>
3. #include <conio.h>
4. //#include <stdlib.h>
5. #include <stdio.h>
6.
7. //#include <ctype.h>
8. //#pragma hdrstop
9. //#include <tchar.h>
10.
//#pragma argsused
11.
12.
#define ARRIBA
72
13.
#define IZQUIERDA
75
14.
#define DERECHA
77
15.
#define ABAJO
80
16.
#define ESC
27
17.
#define ENTER
13
18.
19.
int backcolor=0;
20.
int dir = 0;
21.
int x=39,y=22;
22.
int anteriorpx,anteriorpy;
23.
char tecla;
24.
25.
26.
int vidas = 3;
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
int color[7] = {
38.
0x009,
39.
0x00E,
40.
0x00C,
41.
0x002,
42.
0x00B,
43.
0x005,
44.
0x00F
45.
46.
};
47.
48.
49.
50.
HANDLE hCon;
51.
COORD dwPos;
52.
53.
dwPos.X = x;
54.
dwPos.Y = y;
55.
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
56.
SetConsoleCursorPosition(hCon,dwPos);
57.
58.
59.
char mapa[50][100] = {
60.
"
",
61.
"
AXXXXXXXXXXXXXXXXXXXB AXXXXXXXXXXXXXXXXXXXB",
62.
"
Y___________________Y Y___________________Y",
63.
"
Y_AXXXXXB_AXXXXXXXB_Y Y_AXXXXXXXB_AXXXXXB_Y",
64.
"
Y_Y
65.
"
Y_DXXXXXC_DXXXXXXXC_DXC_DXXXXXXXC_DXXXXXC_Y",
66.
"
Y________|_________|___|_________|________Y",
67.
"
Y_AXXXXXB_AXB_AXXXXXXXXXXXXXB_AXB_AXXXXXB_Y",
68.
"
Y_DXXXXXC_Y Y_DXXXXB
AXXXXC_Y Y_DXXXXXC_Y",
69.
"
Y_________Y Y______Y
Y______Y Y_________Y",
70.
"
DXXXXXXXB_Y DXXXXB_Y
Y_AXXXXC Y_AXXXXXXXC",
71.
"
",
72.
"
",
73.
"
XXXXXXXXC_DXC_AXXXXXX XXXXXXB_DXC_DXXXXXXXX",
74.
"
_________|____Y
75.
"
XXXXXXXXB_AXB_DXXXXXXXXXXXXXC_AXB_AXXXXXXXX",
76.
"
",
77.
"
",
78.
"
AXXXXXXXC_DXC_DXXXXB
AXXXXC_DXC_DXXXXXXXB",
79.
"
Y________|_________Y
Y_________|________Y",
Y_Y
Y_Y Y_Y
Y_Y
Y_Y",
Y____|_________",
80.
"
Y_AXXXXXB_AXXXXXXB_Y
81.
"
82.
"
83.
"
84.
"
AXXXC_DXC_Y Y_DXXXXB
AXXXXC_Y Y_DXC_DXXXB",
85.
"
Y_________Y Y______Y
Y______Y Y_________Y",
86.
"
Y_AXXXXXXXC DXXXXB_Y
Y_AXXXXC DXXXXXXXB_Y",
87.
"
Y_DXXXXXXXXXXXXXXC_DXXXC_DXXXXXXXXXXXXXXC_Y",
88.
"
Y_________________|_____|_________________Y",
89.
"
DXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXC",
90.
Y_AXXXXXXB_AXXXXXB_Y",
};
91.
92.
93.
94.
95.
96.
97.
gotoxy(i,j);
98.
99.
if(mapa[j][i] == 'X')
{setCColor(color[0]);printf("%c",205);}
100.
printf("%c",250);}
101.
else if(mapa[j][i] == 'Y')
{setCColor(color[0]);printf("%c",186);}
102.
else if(mapa[j][i] == 'A')
{setCColor(color[0]);printf("%c",201);}
103.
else if(mapa[j][i] == 'B')
{setCColor(color[0]);printf("%c",187);}
104.
else if(mapa[j][i] == 'C')
{setCColor(color[0]);printf("%c",188);}
105.
else if(mapa[j][i] == 'D')
{setCColor(color[0]);printf("%c",200);}
106.
107.
108.
109.
110.
}
}
111.
112.
113.
114.
setCColor(color[1]);
115.
gotoxy(x,y); printf("%c",2);
116.
117.
118.
119.
120.
121.
122.
void teclear(){
123.
if(kbhit()){
124.
tecla = getch();
125.
switch(tecla){
126.
case ARRIBA:
127.
dir = 0;
128.
break;
129.
130.
case ABAJO:
131.
dir = 1;
132.
break;
133.
134.
case DERECHA:
135.
dir = 2;
136.
break;
137.
138.
case IZQUIERDA:
139.
dir = 3;
140.
break;
141.
142.
143.
144.
145.
146.
147.
148.
class fantasma{
149.
int fdir;
150.
151.
int col;
152.
public:
153.
154.
155.
156.
void mover_fantasma();
157.
void choque_pacman();
158.
};
159.
160.
_x = x;
161.
_y = y;
162.
col = color;
163.
fdir = rand()%4;
164.
165.
166.
167.
setCColor(color[col]);
168.
gotoxy(_x,_y); printf("%c",6);
169.
170.
171.
172.
173.
174.
void fantasma::choque_pacman(){
175.
0
176.
||fdir == 2 && dir == 3 || fdir == 3 && dir == 2 )&&(_y == anteriorpy
&& _x == anteriorpx)){
177.
178.
179.
x = 39;
180.
y = 22;
181.
dir = 4;
182.
vidas--;
183.
184.
185.
186.
187.
188.
189.
void fantasma::mover_fantasma(){
190.
borrar_fantasma();
191.
choque_pacman();
192.
int bolx=_x,boly=_y;
193.
194.
if(mapa[_y][_x] == '|'){
195.
fdir = rand()%4;
196.
197.
if(mapa[_y][_x] == '*'){
198.
199.
fdir = 0;
}
200.
201.
if(fdir == 2){
202.
if(mapa[_y][_x+1] != 'X' && mapa[_y][_x+1] != 'A' && mapa[_y]
[_x+1] != 'Y' &&
203.
!= 'D')
204.
205.
206.
if(fdir == 3){
207.
1] != 'Y' &&
208.
!= 'D')
209.
210.
211.
if(fdir == 0){
212.
[_x] != 'Y' &&
213.
!= 'D')
214.
215.
216.
if(fdir == 1){
217.
[_x] != 'Y' &&
218.
'D')
219.
220.
221.
222.
if(mapa[boly][bolx] == '_') {setCColor(color[1]);
gotoxy(bolx,boly);printf("%c",250);}
223.
224.
// rutina atajo
225.
226.
227.
228.
dibujar_fantasma();
}
229.
230.
231.
void marcador(){
232.
233.
gotoxy(5,4); printf("PUNTOS");
234.
gotoxy(5,6); printf("
235.
setCColor(color[6]);
236.
gotoxy(5,6); printf("%ld",puntos);
");
237.
238.
setCColor(color[1]);
239.
gotoxy(5,25); printf("VIDAS");
240.
241.
gotoxy(5,i+27);printf(" ");
242.
243.
244.
245.
gotoxy(5,i+27);printf("%c",2);
246.
247.
248.
setCColor(color[2]);
249.
gotoxy(70,27);printf("%c",169);
250.
251.
252.
253.
254.
int main(){
255.
256.
257.
258.
fantasma A(41,14,2);
259.
fantasma B(43,14,3);
260.
fantasma C(40,14,4);
261.
fantasma D(39,14,5);
262.
pintar_mapa();
263.
264.
265.
marcador();
266.
borrar_pacman(x,y);
267.
anteriorpx = x; anteriorpy = y;
268.
269.
270.
teclear();
271.
272.
if(dir == 0 && mapa[y-1][x] != 'X' && mapa[y-1][x] != 'A' && mapa[y1][x] != 'Y' &&
273.
274.
275.
276.
else if(dir == 1 && mapa[y+1][x] != 'X' && mapa[y+1][x] != 'A' &&
mapa[y+1][x] != 'Y' &&
277.
278.
279.
280.
pacman(x,y);
281.
Sleep(55);
282.
borrar_pacman(x,y);
283.
284.
B.choque_pacman();
285.
C.choque_pacman();
286.
D.choque_pacman();
287.
A.choque_pacman();
288.
289.
//A.mover_fantasma();
290.
anteriorpx = x; anteriorpy = y;
291.
teclear();
292.
293.
294.
if(dir == 2 && mapa[y][x+1] != 'X' && mapa[y][x+1] != 'A' && mapa[y]
[x+1] != 'Y' &&
295.
296.
297.
298.
else if(dir == 3 && mapa[y][x-1] != 'X' && mapa[y][x-1] != 'A' &&
mapa[y][x-1] != 'Y' &&
299.
300.
301.
A.mover_fantasma();
302.
B.mover_fantasma();
303.
C.mover_fantasma();
304.
D.mover_fantasma();
305.
pacman(x,y);
306.
Sleep(55);
307.
308.
//pacman(x,y);
309.
//rutina atajo
310.
311.
borrar_pacman(x,y);
312.
x = 61;
313.
314.
315.
borrar_pacman(x,y);
316.
x= 18;
317.
318.
319.
320.
321.
322.
323.
324.
325.
326.
gotoxy(5,i+27);printf(" ");
327.
328.
329.
330.
system("pause>NULL");
331.
return 0;
332.
333.
334.
335.