Juego de Pac Man

7
1. #include <windows.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 // NUMEROS ASOCIADOS A LAS FLECHAS DEL TECLADO 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. long int puntos = -5; 26. int vidas = 3; 27. 28. void setCColor( int color) 29. { 30. static HANDLE hConsole; 31. 32. hConsole = GetStdHandle( STD_OUTPUT_HANDLE ); 33. 34. SetConsoleTextAttribute( hConsole, color | (backcolor * 0x10 + 0x100) ); 35. } 36. 37. int color[7] = { 38. 0x009, 39. 0x00E, 40. 0x00C, 41. 0x002, 42. 0x00B, 43. 0x005, 44. 0x00F 45. 46. }; 47. 48. void gotoxy(int x, int y) // funcion que posiciona el cursos en la coordenada (x,y) 49. {

description

....

Transcript of Juego de Pac Man

Page 1: Juego de Pac Man

 

1. #include <windows.h>2. #include <iostream>3. #include <conio.h>4. //#include <stdlib.h>5. #include <stdio.h>6.  7. //#include <ctype.h>8. //#pragma hdrstop9. //#include <tchar.h>10. //#pragma argsused11.  12. #define ARRIBA     72      // NUMEROS ASOCIADOS A LAS FLECHAS DEL TECLADO13. #define IZQUIERDA  7514. #define DERECHA    7715. #define ABAJO      8016. #define ESC        2717. #define ENTER      1318.  19. int backcolor=0;20. int dir = 0;21. int x=39,y=22;22. int anteriorpx,anteriorpy;23. char tecla;24.  25. long int puntos = -5;26. int vidas = 3;27.  28. void setCColor( int color)29. {30.         static HANDLE hConsole;31.  32.         hConsole = GetStdHandle( STD_OUTPUT_HANDLE );33.  34.         SetConsoleTextAttribute( hConsole, color | (backcolor * 0x10 +

0x100) );35. }36.  37. int color[7] = {38.       0x009,39.       0x00E,40.       0x00C,41.       0x002,42.       0x00B,43.       0x005,44.       0x00F45.  46.      };47.  48. void gotoxy(int x, int y)  // funcion que posiciona el cursos en la

coordenada (x,y)49. {

Page 2: Juego de Pac Man

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     Y_Y       Y_Y Y_Y       Y_Y     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.      "                          Y_Y AXXXXC_DXXXC_DXXXXB Y_Y        ",72.      "                          Y_Y Y_________________Y Y_Y        ",73.      "                  XXXXXXXXC_DXC_AXXXXXX XXXXXXB_DXC_DXXXXXXXX",74.      "                  _________|____Y      *      Y____|_________",75.      "                  XXXXXXXXB_AXB_DXXXXXXXXXXXXXC_AXB_AXXXXXXXX",76.      "                          Y_Y Y_________________Y Y_Y        ",77.      "                          Y_Y Y_AXXXXXXXXXXXXXB_Y Y_Y        ",78.      "                  AXXXXXXXC_DXC_DXXXXB   AXXXXC_DXC_DXXXXXXXB",79.      "                  Y________|_________Y   Y_________|________Y",80.      "                  Y_AXXXXXB_AXXXXXXB_Y   Y_AXXXXXXB_AXXXXXB_Y",81.      "                  Y_DXXXB Y_DXXXXXXC_DXXXC_DXXXXXXC_Y AXXXC_Y",82.      "                  Y_____Y Y_________|_____|_________Y Y_____Y",83.      "                  DXXXB_Y Y_AXB_AXXXXXXXXXXXXXB_AXB_Y Y_AXXXC",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. };91.  92.  93. void pintar_mapa()// Funcion que pinta el borde de la pantalla de juego94. {95.      for(int i = 0 ; i < 78 ; i++){96.          for(int j = 0 ; j < 30 ; j++){97.                gotoxy(i,j);98.  99.                if(mapa[j][i] == 'X')

{setCColor(color[0]);printf("%c",205);}100.                if(mapa[j][i] == '_') {setCColor(color[1]);

printf("%c",250);}

Page 3: Juego de Pac Man

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. void pacman(int x , int y){114.      setCColor(color[1]);115.      gotoxy(x,y); printf("%c",2);116. }117.  118. void borrar_pacman(int x, int y){119.      gotoxy(x,y); printf(" ");120.      if(mapa[y][x] == '_'){ mapa[y][x] = ' '; puntos += 5;}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.       }// FIN SWITCH143.  144.    }// FIN DEL IF145.  146. }147.  148. class fantasma{

Page 4: Juego de Pac Man

149.       int fdir;150.       int _x, _y;151.       int col;152. public:153.       fantasma(int x , int y , int color); // constructor154.       void dibujar_fantasma() const;155.       void borrar_fantasma() const;156.       void mover_fantasma();157.       void choque_pacman();158. };159. fantasma::fantasma(int x , int y, int color){160.       _x = x;161.       _y = y;162.       col = color;163.       fdir = rand()%4;164. }165. void fantasma::dibujar_fantasma() const{166.  167.      setCColor(color[col]);168.      gotoxy(_x,_y); printf("%c",6);169. }170. void fantasma::borrar_fantasma() const{171.  172.      gotoxy(_x,_y); printf(" ");173. }174. void fantasma::choque_pacman(){175.      if(y == _y && x == _x || (fdir == 0 && dir == 1 || fdir == 1 && dir ==

0176.      ||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.              fdir = 0;199.      }

Page 5: Juego de Pac Man

200.  201.      if(fdir == 2){202.           if(mapa[_y][_x+1] != 'X' && mapa[_y][_x+1] != 'A' && mapa[_y]

[_x+1] != 'Y' &&203.           mapa[_y][_x+1] != 'B' && mapa[_y][_x+1] != 'C' && mapa[_y][_x+1]

!= 'D')         _x++;204.           else fdir = rand()%4;205.      }206.      if(fdir == 3){207.           if(mapa[_y][_x-1] != 'X' && mapa[_y][_x-1] != 'A' && mapa[_y][_x-

1] != 'Y' &&208.           mapa[_y][_x-1] != 'B' && mapa[_y][_x-1] != 'C' && mapa[_y][_x-1]

!= 'D')          _x--;209.           else fdir = rand()%4;210.      }211.      if(fdir == 0){212.           if(mapa[_y-1][_x] != 'X' && mapa[_y-1][_x] != 'A' && mapa[_y-1]

[_x] != 'Y' &&213.           mapa[_y-1][_x] != 'B' && mapa[_y-1][_x] != 'C' && mapa[_y-1][_x]

!= 'D')           _y--;214.           else fdir = rand()%4;215.      }216.      if(fdir == 1){217.           if(mapa[_y+1][_x] != 'X' && mapa[_y+1][_x] != 'A' && mapa[_y+1]

[_x] != 'Y' &&218.       mapa[_y+1][_x] != 'B' && mapa[_y+1][_x] != 'C' && mapa[_y+1][_x] !=

'D')                _y++;219.           else fdir = rand()%4;220.      }221.  222.      if(mapa[boly][bolx] == '_') {setCColor(color[1]);

gotoxy(bolx,boly);printf("%c",250);}223.  224.      // rutina atajo225.      if(_x <= 17) _x = 61;226.         else if(_x >= 62) _x = 18;227.      dibujar_fantasma();228. }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.    for(int i = 0 ; i <= vidas ; i++){241.        gotoxy(5,i+27);printf(" ");242.  243.    }

Page 6: Juego de Pac Man

244.    for(int i = 0 ; i < vidas ; i++){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.    while(vidas > 0 && puntos < 1950){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[y-

1][x] != 'Y' &&273.       mapa[y-1][x] != 'B' && mapa[y-1][x] != 'C' && mapa[y-1][x] != 'D')  

y--;274.  275.  276.       else if(dir == 1 && mapa[y+1][x] != 'X' && mapa[y+1][x] != 'A' &&

mapa[y+1][x] != 'Y' &&277.       mapa[y+1][x] != 'B' && mapa[y+1][x] != 'C' && mapa[y+1][x] != 'D')  

y++;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.  

Page 7: Juego de Pac Man

293.  294.       if(dir == 2 && mapa[y][x+1] != 'X' && mapa[y][x+1] != 'A' && mapa[y]

[x+1] != 'Y' &&295.       mapa[y][x+1] != 'B' && mapa[y][x+1] != 'C' && mapa[y][x+1] != 'D')  

x++;296.  297.  298.       else if(dir == 3 && mapa[y][x-1] != 'X' && mapa[y][x-1] != 'A' &&

mapa[y][x-1] != 'Y' &&299.       mapa[y][x-1] != 'B' && mapa[y][x-1] != 'C' && mapa[y][x-1] != 'D')  

x--;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 atajo310.       if(x < 17) {311.         borrar_pacman(x,y);312.         x = 61;313.       }314.       else if(x > 62){315.         borrar_pacman(x,y);316.         x= 18;317.       }318.  319.  320.  321.    }322.  323.  324.  325.    for(int i = 0 ; i <= vidas ; i++){326.        gotoxy(5,i+27);printf(" ");327.  328.    }329.  330.    system("pause>NULL");331.    return 0;332.  333. }334.  335.