Advertisement
Guest User

ZippoLag

a guest
Apr 29th, 2008
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.12 KB | None | 0 0
  1. /*------------------------------------------------------------------------*/
  2. /*-----------------------------------PACOMAN------------------------------*/
  3. /*------------------------------------------------------------------------*/
  4.  
  5. /*The following is a translation of a "Pacman clone" I made aout a year ago to see exatly what I remembered about C before I went on to teaching other people, the overall desing of the app can be snipped out of context as "hwy, what about if I made a pacman clone? I would need, hmhm.. a aloop in wich I draw stuff to the screen, then read input and move things around properly, I should be able to get something working by this afternoon", I actually said that a day at around 10am, of course, all that was there at that afternoon was an skeleton of the map, on the next day I had the pills placed around and a smily face that moved around responding to input, eating pills and respecting no boundaries. After 3 more days of constant key-stroking (and not much thinking) I had the current version of Pacoman -wich I never ever think of upgrading, well, maybe I'll rewrite it on C# with a better design someday, and perhaps it'll have nice graphics and will run at more than 1 FPS-. Btw, sorry if my translation sucks, it will hopefully not be worse than the code itself, tho.*/
  6.  
  7. #include <string.h>
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <dos.h>
  11. #include <stdlib.h>
  12.  
  13. /* SIMBOLIC CONSTANTS */
  14.        /* VERSION */
  15. #define PACOVER "v0.5a"
  16. #define COMPATIVERSION 'a' //a:casa //b:notebook hp //->needed this to proper display some ascii chars in some computers, mode a is the way to go on most comps
  17.  
  18.    /* SOUND */
  19.  
  20. //#define CONWAKA 1       //pacoman will make *wakawakawaka* when moving if uncommented -sounds really bad-
  21. //#define CONTWEET 1      //make a *tweet* when a ghost is eaten?
  22. //#define CONMUSICAMUERTE //really, doesn't sound right either
  23.  
  24.         /* MAP SIMBOLS */ //This ones are needed because, well, they really aren't needed, but they made my life easy at the moment.
  25. #define PVAI 185   //¹:185   vertical wall open to the left
  26. #define PVER 186   //º:186   vertical wall
  27. #define ECSD 187   //»:187   upper right closed corner
  28. #define ECID 188   //¼:188   lower right closed corner
  29. #define ECSI 201   //É:201   upper left closed corner
  30. #define ECII 200   //È:200   lower left closed corner
  31. #define TINV 202   //Ê:202   inverted open T
  32. #define TCOM 203   //Ë:203   open T
  33. #define PVAD 204   //Ì:204   vertical wall open to the right
  34. #define PHZN 205   //Í:205   horizontal wall
  35. #define CRUZ 206   //Î:206   + (open cross)
  36. #define SPFN 126   //~:126   spawn ghost <-requires an empty space above //counts as a wall //there can only be one per map
  37. #define PACO   2   //:2     paco /spawn paco //there can only be one per map
  38.  
  39. #if COMPATIVERSION=='a' //if we are on the compatibility mode for my home pc we'll define this simbols like this
  40. #define PAST 250   //ú:250   pill
  41. #define SPAS 155   //›:155   super pill
  42. #define FANT 169   //©:169   ghost
  43. #elif COMPATIVERSION=='b' //if we are on 'that place' notebook's mode instead
  44. #define PAST 249   //ù:249   pill
  45. #define SPAS 236   //ì:236   super pill
  46. #define FANT 234   //ê:234   ghost
  47. #endif
  48.  
  49. /* DATA STRUCTURES */
  50.  
  51. struct ente{
  52.         char q;          //it's symbol
  53.         int x;           //x coordinate
  54.         int y;           //y coordinate
  55.         int dir;         //direction of movement //0 does not move
  56.         int vel;         //current moving speed  //0 does not move
  57.         int def_vel;     //default moving speed
  58.         int dopa_vel;    //speed when doped //I'll often use the words "doped", or "highed" for when the pacoman is under the effects of the super pill, beware
  59.         int def_color;   //normal color
  60.         int color;       //current color
  61.         int dopa_color;  //doped color
  62.        };
  63.  
  64.  
  65. /* OTHER GLOBAL CONSTs */
  66.  
  67.  const int POS_INI_X=1; //start of the map on the screen
  68.  const int POS_INI_Y=3;
  69.  
  70.  const int PAREDES[12]={PVAI, PVER, ECSD, ECID, ECSI, ECII, TINV, TCOM, PVAD, PHZN, CRUZ, SPFN}; //all the symbols that cannot be walked thru
  71.  
  72.  enum DIRECCIONES{QUIETO=0,ARR,IZQ,ABA,DER}; //the 4 possible directions of movement
  73.  const int MAX_DIR=DER;
  74.  enum VELOCIDADES{        NORMAL=1,RAPIDO}; //the different speeds, 'STILL' direction can be used as speed or making the entity stop
  75.  const int MAX_VEL=RAPIDO;
  76.  
  77.  const char MOV_AR='w'; //command key for moving UP
  78.  const char MOV_IZ='a'; //left
  79.  const char MOV_AB='s'; //down
  80.  const char MOV_DE='d'; //right
  81.  const char SALIR='k';  //exit
  82.  const char PAUSA='p';  //pause
  83.  
  84.  enum  ERRORES{PSNF=0,FSNF,PANF,SPANF,MFDG,FMNE}; //error codes
  85.  
  86.  const int ANCHOMAPA=22; //map's width, with 1 extra space for placing the '\0' and printing it like a string for easy of use
  87.  const int ALTOMAPA=21;  //map's height, cannot be larger than 20 or it won't fit on the screen
  88.  
  89.  const int MAXFANTASMAS=4; //max amount of simultaneous ghosts //note, should not be higher than (MAX_FANT_COLOR-MIN_FANT_COLOR)
  90.  
  91.  const int MAX_VEL_FANT=NORMAL; //ghost's max possible speed
  92.  const int MIN_FANT_COLOR=GREEN; //lowest color asignable to a ghost
  93.  const int MAX_FANT_COLOR=DARKGRAY; //highest color asignable to a ghost
  94.  const int FANT_DOPA_COLOR=LIGHTBLUE+BLINK; //ghosts color when paco is doped
  95.  
  96.  const int PACO_DEF_COLOR=YELLOW; //paco's normal color
  97.  const int PACO_DOPA_COLOR=YELLOW; //paco's highed color
  98.  const int PACO_DEF_DIR=ARR; //direction of movement at the start of the game
  99.  const int PACO_DEF_VEL=NORMAL; //paco's default speed //note: given the lazyness of the code, the paco should not be assigned a higher speed than 1, or there will be collission problems, the ghosts on the other hand can move as fast as one want them to
  100.  
  101.  const int COLOR_TEXTO=WHITE;
  102.  const int COLOR_MAPA=RED;
  103.  const int COLOR_PASTILLAS=YELLOW;
  104.  const int COLOR_PUNTAJE_OBT=CYAN;
  105.  
  106.  const int PUNTAJE_PASTILLA=1; //score given by eating a pill, super pill, and ghost
  107.  const int PUNTAJE_SUP_PAST=10;
  108.  const int PUNTAJE_FANTASMA=100;
  109.  
  110.  const int LAPSO_ENTRE_FANTASMAS=13; //time in seconds between a ghost spawn and the next
  111.  const int DURACION_DOPA=30; //duration of the superpill's effect
  112.  const int DEF_VIDAS=1; //player starting lifes
  113.  const int HSFPS=75; //amount of cents of seconds to wait until next frame, given a bug in my time calculation system, this is never used and instead the game runs at a solid 1 FPS
  114.  
  115.  const char CHARNULO=' ';
  116.  
  117. #ifdef CONWAKA //constants used only for creating the *wakawaka* effect
  118.  const int FREC_DOPADO=100;     //default:100
  119.  const int MIN_FREC_WAKA=300;   //default:300
  120.  const int MAX_FREC_WAKA=600;   //default:600
  121.  const int MIN_INTER_WAKA=600;  //default:600
  122.  const int MAX_INTER_WAKA=1200; //default:1200
  123.  int g_sonando=0;
  124.  int g_suena_mueve=0; //so the sound only is heard when the paco is moving
  125. #endif
  126.  
  127. /* GLOBAL VARs */
  128.  int g_puntaje=0;       //player's score
  129.  int g_vidas=DEF_VIDAS; //player's lifes
  130.  int g_killing_spree=0; //stacked bonus per ghost eaten in a continuous doping session
  131.  int g_pastillas=0;     //amount of pills left to eat
  132.  int g_super_pastillas=0;
  133.  int g_cant_fant=0; //amount of ghosts swirming around
  134.  
  135.  int g_fin_del_juego=0; //when it's !=0 the game is over
  136.  int g_dopado=0; //to check and see if there's an active superpill
  137.  
  138.  struct time g_t_com_dopa; //start of dopage time
  139.  struct time g_t_com_juego; //start of game time
  140.  
  141.  int g_color_fantasma_muerto=-1; //to see wich was the last ghost to have died, I think, I didn't comments this right the first time around //this only works because there can't be 2 ghosts of the same color
  142.  
  143.  char pastillas[ALTOMAPA][ANCHOMAPA];
  144.  char MAPA001[ALTOMAPA][ANCHOMAPA]={ //the map loaded, I could've deined it as a const and then have diferent arrays ofr holding different stuff, but IDK WTF I was thinking ATM
  145.            /**/" ",/**/       "ÉÍÍÍÍÍ»       ÉÍÍÍÍÍ»", //NOTE, the extra line is added because a bizare bug would happen otherwise
  146.                       "º21111ÈÍÍÍÍÍÍͼ11112º",
  147.                       "º1ÉÍ»11111111111ÉÍ»1º",
  148.                       "º1º ÈÍÍÍÍ»1ÉÍÍÍͼ º1º",
  149.                       "º1ÈËÍËÍÍͼ1ÈÍÍÍËÍ˼1º",
  150.                       "º11Èͼ111121111Èͼ11º",
  151.                       "È»11111ÉÍÍ~ÍÍ»11111ɼ",
  152.                       " ÈÍÍÍ»1ÈÍÍÍÍͼ1ÉÍÍͼ ",
  153.                       "   Éͼ111111111ÈÍ»   ",
  154.                       " Éͼ111ÉÍ»1ÉÍ»111ÈÍ» ",
  155.                       "ɼ111ÉÍÎ͹1ÌÍÎÍ»111È»",
  156.                       "º11Éͼ º º1º º ÈÍ»11º",
  157.                       "º1ɼ   º º1º º   È»1º",
  158.                       "º1ÌÍÍÍÍÊ͹1ÌÍÊÍÍÍ͹1º",
  159.                       "º1º      º1º      º1º",
  160.                       "º1º      º1º      º1º",
  161.                       "º1ÈÍ»  Éͼ1ÈÍ»  Éͼ1º",
  162.                       "º111ÈÍͼ1111ÈÍͼ111º",
  163.                       "ÈÍ»211111ÉÍ»111112Éͼ",
  164.                       "  ÈÍÍÍÍÍͼ ÈÍÍÍÍÍͼ  ",};
  165.  
  166.  
  167. /* FUNCTIONS PROTOTIPES */
  168.  
  169. void muestra_titulo(); //clears the screen and shows the game title
  170. void muestra_mapa(); //prints the map on the screen
  171. void muestra_pastillas(); //updates the pills
  172. void muestra_puntaje_y_vidas(); //shows life and score of the player
  173. void muestra_instrucciones(); //shows command keys at the bottom of the screen
  174. void muestra_ente(struct ente); //draws a given entity at it's given place and color
  175.  
  176. struct ente buscar_ente(char);     //searches the map for a certain symbol and returns it as an entity
  177. int buscar_spawn_paco(struct ente&); //searches the character for spawning the paco
  178. int buscar_spawn_fantasma(struct ente&); //searches the character for spawning the ghosts
  179. void buscar_pastillas(); //searches the map for all the pills, counts and stores them in an array with the proper symbol depending on the compatibility version selected
  180.  
  181. void crear_paco(struct ente &paco, struct ente spawn); //creates the paco entity at the position given by it's spawn place
  182. void crear_fant(struct ente &fant, int&, struct ente spawn); //creates a ghost right above the ghost's spawn point
  183.  
  184. void recibir_input(struct ente&); //gets and interpretes keyboard input
  185. void modif_dir_ente(int nuevadir,struct ente&); //changes the direction of a given entity
  186. void mover_ente(struct ente&,struct ente otto); //moves an entity on it's given direction and speed (if possible) /note: hitting a wall terminates movement until the next frame //checks for collissions with the entity "otto" //note: I call it "otto" since I've already used "aux" for something else in other function, and I "dummy" as a generic empty entity, so I'm using this sort of naming convention of calling "otto" my generic auxiliar entity.
  187. int valid_mov(struct ente, struct ente otto); //verifies that it's valid to move the entity /returns the amount of spaces the entity can move in the given direction at the current speed //checks for collisions with entity "otto"
  188.  
  189. void comer_pastilla(int x, int y); //eats a pill, doing everything that it may require //note: spanish comment was longer, but cut me some slack will ya?
  190.  
  191. void err(int); //handles errors
  192.  
  193. /* MAIN */
  194.  
  195. int main()
  196. {
  197.  gettime(&g_t_com_juego);
  198.  /* MAIN LOCAL CONSTs*/
  199.  
  200.  /* MAIN LOCAL VARs*/
  201.  
  202.  int i=0,j=0; //generic indexes
  203.  
  204. #ifdef CONWAKA //if we are using the *waka* sound system..
  205.  int sonando;
  206. #endif
  207.  
  208.  struct ente punto_spawn_paco;
  209.  struct ente punto_spawn_fantasma;
  210.  struct ente paco;
  211.  struct ente fantasmas[MAXFANTASMAS];
  212.  struct ente dummy; dummy.q=CHARNULO; dummy.x=0; dummy.y=0; //check movement function for specifics on usage, mainly validation of movement and collissions
  213.  
  214.  int ult_color_fant=MIN_FANT_COLOR; //color assigned to last created ghost
  215.  int imprimir_puntaje=0;
  216.  
  217.  struct time t_com_bucle; //variable for looping waiting for user input
  218.  struct time t_ultimo_fant; //when the last ghost was created
  219.  struct time t_aux;
  220.  //int t_hudif=0;
  221.  int t_sedif=0;
  222.  
  223.  /* PROGRAM START */
  224.  
  225.  srand(time(0)); //initializes random number generator
  226.  
  227.  muestra_titulo(); //displays *PACOMAN vX.X*
  228.  if(buscar_spawn_paco(punto_spawn_paco))                 {err(PSNF);  return PSNF; }
  229.  if(buscar_spawn_fantasma(punto_spawn_fantasma))         {err(FSNF);  return FSNF; }
  230.  buscar_pastillas(); if(!g_pastillas)                    {err(PANF);  return PANF; }
  231.  if(MAXFANTASMAS > MAX_FANT_COLOR-MIN_FANT_COLOR)        {err(MFDG);  return MFDG; }
  232.  crear_paco(paco, punto_spawn_paco);
  233.  crear_fant(fantasmas[g_cant_fant],ult_color_fant,punto_spawn_fantasma);
  234.  gettime(&t_ultimo_fant);    /* creates a ghost if it's time */
  235.  do
  236.   {
  237.    gettime(&t_aux);
  238.    t_sedif=t_aux.ti_sec-g_t_com_dopa.ti_sec;
  239.    (t_sedif<0)?t_sedif=-t_sedif:1;
  240.    if(t_sedif>=DURACION_DOPA)
  241.     {
  242.      g_dopado=0;
  243.      g_killing_spree=0;
  244.     }
  245.    if(g_cant_fant<MAXFANTASMAS && !g_dopado)  //ghosts are only created if max ghost amount hasn't been reached, and if the paco is not doped. Non-scared ghosts while paco is doped are not supported.
  246.     {
  247.      gettime(&t_aux);
  248.      t_sedif=t_aux.ti_sec-t_ultimo_fant.ti_sec;
  249.      (t_sedif<0)?t_sedif=-t_sedif:1;
  250.      if(t_sedif>=LAPSO_ENTRE_FANTASMAS)
  251.       {
  252.        crear_fant(fantasmas[g_cant_fant],ult_color_fant,punto_spawn_fantasma);
  253.        gettime(&t_ultimo_fant);
  254.       }
  255.     }
  256.    muestra_titulo();
  257.    muestra_mapa();
  258.    muestra_pastillas();
  259.    muestra_ente(paco);
  260.    for (i=0;i<g_cant_fant;i++)
  261.     muestra_ente(fantasmas[i]);
  262.    muestra_puntaje_y_vidas();
  263.    muestra_instrucciones();
  264.    if(imprimir_puntaje)
  265.     {
  266.      textcolor(COLOR_PUNTAJE_OBT);
  267.      gotoxy(POS_INI_X+paco.y,POS_INI_Y+paco.x);
  268.      cprintf("%d!!",PUNTAJE_FANTASMA*g_killing_spree);
  269. #ifdef CONTWEET
  270.      printf("\a\a\a");
  271. #endif
  272.      textcolor(WHITE);
  273.      imprimir_puntaje=0;
  274.     }
  275.    gettime(&t_com_bucle);
  276.    // gotoxy(1,13); printf("\n%d %d",paco.x,paco.y); //shows paco's position on map for easier debugging
  277.    do //input-wating loop
  278.     {
  279. #ifdef CONWAKA
  280.      if (g_suena_mueve)
  281.       {
  282.        if(!g_dopado)
  283.     if(g_sonando>MIN_INTER_WAKA){sound(MIN_FREC_WAKA); g_sonando++; if(g_sonando>MAX_INTER_WAKA)g_sonando=0;}//wakawakawakawaka..
  284.      else{sound(MAX_FREC_WAKA); g_sonando++;}
  285.     else sound(FREC_DOPADO);
  286.       }
  287. #endif
  288.      recibir_input(paco);
  289.      gettime(&t_aux);
  290.      /*t_hudif=t_aux.ti_hund-t_com_bucle.ti_hund; //broken code that attempted to make the game run faster than 1 FPS
  291.      (t_hudif<0)?t_hudif=-t_hudif:1;*/
  292.      t_sedif=t_aux.ti_sec-t_com_bucle.ti_sec;
  293.      (t_sedif<0)?t_sedif=-t_sedif:1;
  294.     }while(/*t_hudif<HSFPS&&*/t_sedif<1);
  295.    mover_ente(paco, dummy); //I pass dummy because I have nothing to pass, the collissions between the paco and the ghosts are calculated when the ghosts attempt to move. //HOLY SHIT IT JUST HIT ME: it appears I had never heard of optional parameters. Or! I maybe had tried them, but the goat-blowing compiler I was using didn't suport optionals -as it didn't support many other things :/ -
  296.    for (i=0;i<g_cant_fant;i++)
  297.     {
  298.      mover_ente(fantasmas[i], paco);
  299.      if(g_color_fantasma_muerto!=-1) //deletes dead ghost, adds score and uses time to spawn a new ghost
  300.       {
  301.        i=-1;
  302.        do
  303.     {
  304.      i++;
  305.     }while(fantasmas[i].color!=g_color_fantasma_muerto /*&& i<g_cant_fant*/);  //broken code for forgotten reason
  306.        if(fantasmas[i].color!=g_color_fantasma_muerto) {err(FMNE); return FMNE;} //just in case?
  307.        for(j=i;j<g_cant_fant-1;j++) //moves all the ghosts that come after the dead one
  308.     {
  309.      fantasmas[j]=fantasmas[j+1];
  310.     }
  311.        g_cant_fant--;
  312.        fantasmas[g_cant_fant].color=WHITE; //an ilegal ghost code, just in case some trash was let behind (paranoia?)
  313.        g_killing_spree++;
  314.        g_puntaje+=PUNTAJE_FANTASMA*g_killing_spree;
  315.        g_color_fantasma_muerto=-1;
  316.        gettime(&t_ultimo_fant);
  317.        imprimir_puntaje=1;
  318.       }
  319.     }
  320.   }while(!g_fin_del_juego);
  321.  
  322. #ifdef CONWAKA
  323.  nosound(); //stops the wakawaka
  324. #endif
  325.  
  326. #ifdef CONMUSICAMUERTE
  327.  if(g_fin_del_juego==2)
  328.   {
  329.    for(i=500;i>100;i--)
  330.     {
  331.      sound(i);
  332.      delay(5);
  333.     }
  334.    nosound();
  335.    sound(700);
  336.    delay(300);
  337.    nosound();
  338.    sound(700);
  339.    delay(300);
  340.    nosound();
  341.   }
  342. #endif
  343.  
  344.  muestra_titulo();
  345.  muestra_mapa();
  346.  muestra_pastillas();
  347. // muestra_ente(paco); //no longer called here
  348.  for (i=0;i<g_cant_fant;i++)
  349.   muestra_ente(fantasmas[i]);
  350.  muestra_puntaje_y_vidas();
  351.  switch (g_fin_del_juego)
  352.   {
  353.    case 1: printf("\nGANASTE!!"); break;
  354.    case 2: printf("\nperdiste.. y bueh.."); break;
  355.    case 3: printf("\naah, cobarde, no lograste terminar la partida"); break;
  356.   }
  357.  
  358.  while(!kbhit());
  359.  return 0;
  360.  
  361.  /* END OF PROGRAM */
  362.  
  363. }
  364.  
  365. /* FUNCTIONS  */
  366.  
  367. /* MOVE ENTITY */ //moves the given entity on the proper direction and with the proper speed as much as possible. Also controls the movement of ghosts, the *wakawaka* and posibly something else. //note: this function definately got too large
  368. void mover_ente(struct ente &pj, struct ente otto)
  369. {
  370.  int cant=0, dir=0, ban=0;
  371.  char aux=CHARNULO;
  372.  if(pj.dir>QUIETO && pj.vel>QUIETO)
  373.   {
  374.    cant=valid_mov(pj, otto);
  375.    if (cant>0)
  376.    {
  377.     switch (pj.dir)
  378.      {
  379.       case ARR: pj.x-=cant; break;
  380.       case ABA: pj.x+=cant; break;
  381.       case IZQ: pj.y-=cant; break;
  382.       case DER: pj.y+=cant; break;
  383.      }
  384.    }
  385.   }
  386.  if(pj.q==(char)FANT)     /* controls the movement of ghosts, or makes them move stupidly, better said*/
  387.   {
  388.    switch (pj.dir)
  389.     {
  390.      case ARR: aux=MAPA001[pj.x-1][pj.y]; break;
  391.      case ABA: aux=MAPA001[pj.x+1][pj.y]; break;
  392.      case IZQ: aux=MAPA001[pj.x][pj.y-1]; break;
  393.      case DER: aux=MAPA001[pj.x][pj.y+1]; break;
  394.     }
  395.    if(cant<pj.vel || aux!=CHARNULO)
  396.     {
  397.      dir=pj.dir;
  398.      do
  399.       {
  400.        pj.dir=rand()%MAX_DIR +1;
  401.        switch (pj.dir)
  402.     {
  403.      case ARR: aux=MAPA001[pj.x-1][pj.y]; break;
  404.      case ABA: aux=MAPA001[pj.x+1][pj.y]; break;
  405.      case IZQ: aux=MAPA001[pj.x][pj.y-1]; break;
  406.      case DER: aux=MAPA001[pj.x][pj.y+1]; break;
  407.     }
  408.       }while(pj.dir==dir && aux!=CHARNULO);
  409.     }
  410.   }
  411. #ifdef CONWAKA
  412.  if(pj.q==(char)PACO)
  413.   if(cant>0)
  414.     g_suena_mueve=1;
  415.   else
  416.    {
  417.     nosound(); //should only sound if the paco is on the move
  418.     g_suena_mueve=0;
  419.    }
  420. #endif
  421.  return;
  422. }
  423.  
  424. /* VALIDATE MOVEMENT */ //returns how many squares can the entity move, clculates collissions if neccessary. /note: now this function is a bit of a monster too
  425. int valid_mov(struct ente pj, struct ente otto)
  426. {
  427.  char aux;
  428.  int i=0, cant=0, ban=0;
  429.  int x=pj.x, y=pj.y;
  430.  if(pj.q==(char)FANT && otto.q==(char)PACO)
  431.   if(x==otto.x && y==otto.y)
  432.    if(!g_dopado)
  433.     {
  434.      g_vidas--;
  435.      /* TERMINAR RONDA*/
  436.      if(g_vidas==0) g_fin_del_juego=2; //if I ever upgrade this thing (implementing several rounds and lifes) this shouldn't go here anymore
  437.      return 0; //if the collission happens without any movement done, return zero
  438.     }
  439.    else
  440.     {
  441.      g_color_fantasma_muerto=pj.color;
  442.     }
  443.  do
  444.   {
  445.    i++;
  446.    switch (pj.dir)
  447.     {
  448.      case ARR: x=pj.x-i; break;
  449.      case ABA: x=pj.x+i; break;
  450.      case IZQ: y=pj.y-i; break;
  451.      case DER: y=pj.y+i; break;
  452.     }
  453.    aux=MAPA001[x][y];
  454.    if(aux!=CHARNULO)  //an entity can only walk trhu empty space (or pills, but those are in another matrix)
  455.     ban=1;
  456.    else
  457.     {
  458.      cant=i;
  459.      if((pj.q==(char)PACO) && (pastillas[x][y]!=CHARNULO))
  460.       comer_pastilla(x,y);
  461.      if(pj.q==(char)FANT && otto.q==(char)PACO)
  462.       if(x==otto.x && y==otto.y)
  463.        if(!g_dopado)
  464.     {
  465.      g_vidas--;
  466.      /* To Do: END ROUND*/
  467.      if(g_vidas==0) g_fin_del_juego=2; //if I ever upgrade this thing (implementing several rounds and lifes) this shouldn't go here anymore
  468.      return 0; //just get out without doing any further
  469.     }
  470.        else
  471.     {
  472.      g_color_fantasma_muerto=pj.color;
  473.     }
  474.     }
  475.   }while(i<pj.vel && ban==0);
  476.  return (cant);
  477. }
  478.  
  479. /* EAT PILL */ //eats a pill and does whatever should be done
  480. void comer_pastilla(int x, int y)
  481. {
  482.  switch (pastillas[x][y])
  483.   {
  484.    case (char)PAST: g_puntaje+=PUNTAJE_PASTILLA; g_pastillas--; break;
  485.    case (char)SPAS: g_puntaje+=PUNTAJE_SUP_PAST; g_super_pastillas--; gettime(&g_t_com_dopa); g_dopado=1; /* DOPAJE!! */ break;
  486.   }
  487.  pastillas[x][y]=CHARNULO;
  488.  if (g_pastillas==0 && g_super_pastillas==0) g_fin_del_juego=1;
  489.  return;
  490. }
  491.  
  492. /* TAKE INPUT */ //checks to see if a key has been pressed and does what it should in response
  493. void recibir_input(struct ente &paco)
  494. {
  495.  int tecla;
  496.  if(kbhit())
  497.   {
  498.    tecla=getch();
  499.    if (tecla==MOV_AR||tecla==MOV_IZ||tecla==MOV_AB||tecla==MOV_DE)
  500.     modif_dir_ente(tecla, paco);
  501.    else
  502.     if (tecla==PAUSA)
  503.      /*to do..*/;
  504.     else
  505.      if (tecla==SALIR)
  506.       g_fin_del_juego=3;
  507.   }
  508.  return;
  509. }
  510.  
  511. /* CHANGE DIRECTION */ //changes the entity's direction by the given one
  512. void modif_dir_ente(int nuevadir, struct ente &pj)
  513. {
  514.  char aux=CHARNULO;
  515.  int dirant=pj.dir;
  516.  switch (nuevadir)
  517.   {
  518.    case MOV_AR: pj.dir=ARR; break;
  519.    case MOV_AB: pj.dir=ABA; break;
  520.    case MOV_IZ: pj.dir=IZQ; break;
  521.    case MOV_DE: pj.dir=DER; break;
  522.   }
  523.  switch (pj.dir)
  524.   {
  525.    case ARR: aux=MAPA001[pj.x-1][pj.y]; break;
  526.    case ABA: aux=MAPA001[pj.x+1][pj.y]; break;
  527.    case IZQ: aux=MAPA001[pj.x][pj.y-1]; break;
  528.    case DER: aux=MAPA001[pj.x][pj.y+1]; break;
  529.   }
  530.  if (aux!=CHARNULO) pj.dir=dirant;
  531.  return;
  532. } //I didn't notice this uneccessary double-switch until now, but I won't change it, as I promised
  533.  
  534. /* SEEK ENTITY */ //returns an entity with the symbol searched on its position (this sounded equally bad in spanish, probably wrote it at 4am or something)
  535. struct ente buscar_ente(char coso)
  536. {
  537.  struct ente esto;
  538.  int i=0, j=0;
  539.  for(i=0;i<ALTOMAPA;i++)
  540.   for(j=0;j<ANCHOMAPA;j++)
  541.    if(MAPA001[i][j]==coso)
  542.     {
  543.      esto.q=coso;
  544.      esto.x=i;
  545.      esto.y=j;
  546.      esto.dir=QUIETO;
  547.      esto.vel=esto.def_vel=QUIETO;
  548.      esto.dopa_color=esto.color=esto.def_color=WHITE;
  549.      return esto;
  550.     }
  551.  esto.q='0';
  552.  return esto;
  553. }
  554.  
  555. /* SEARCH SPAWN PACO */ //searches for the spawn point of the pacoman
  556. int buscar_spawn_paco(struct ente &paco)
  557. {
  558.  paco=buscar_ente((char)PACO);
  559.  if (paco.q=='0') return 1;
  560.   else
  561.    {
  562.     MAPA001[paco.x][paco.y]=CHARNULO; // removes the symbol from the map
  563.     return 0;
  564.    }
  565. }
  566.  
  567. /* SEARCH SPAWN GHOST */ //searches for the spawn point of the ghosts
  568. int buscar_spawn_fantasma(struct ente &fant)
  569. {
  570.  fant=buscar_ente((char)SPFN);
  571.  if (fant.q=='0') return 1;
  572.   else return 0;
  573. }
  574.  
  575. /* SEEK PILLS */ //searches for all the pills and loads them up
  576. void buscar_pastillas()
  577. {
  578.  int i=0,j=0;
  579.  char aux=CHARNULO;
  580.  for(i=0;i<=ALTOMAPA;i++)
  581.   for(j=0;j<ANCHOMAPA;j++)
  582.    {
  583.     switch(MAPA001[i][j])
  584.      {
  585.       case '1': pastillas[i][j]=(char)PAST;
  586.         g_pastillas++;
  587.         MAPA001[i][j]=CHARNULO;
  588.            break;
  589.       case '2': pastillas[i][j]=(char)SPAS;
  590.         g_super_pastillas++;
  591.         MAPA001[i][j]=CHARNULO;
  592.            break;
  593.       default:  pastillas[i][j]=CHARNULO;
  594.            break;
  595.      }
  596.    }
  597.  return;
  598. }
  599.  
  600. /* CREATE GHOST */ //creates and initializes a ghost entity
  601. void crear_fant(struct ente &fantasma, int& color, struct ente spawn)
  602. {
  603. char aux=MAPA001[spawn.x-1][spawn.y];
  604.  if (g_cant_fant<MAXFANTASMAS)
  605.   if(aux==CHARNULO)
  606.    {
  607.     fantasma.q=(char)FANT;
  608.     fantasma.x=spawn.x-1; //creates it in the position above the spawn point
  609.     fantasma.y=spawn.y;
  610.     fantasma.dir=rand()%4 +1; //without the +1 it could be 0, meaning it'll just stand still
  611.     fantasma.vel=fantasma.def_vel=rand()%MAX_VEL_FANT +1; //again, the +1
  612.     color++;
  613.     fantasma.color=fantasma.def_color=color;
  614.     fantasma.dopa_color=FANT_DOPA_COLOR;
  615.     g_cant_fant++;
  616.    }
  617.   //else /* ghost could not be created due to lack of space above */
  618.  //else /*ghost could not be created since the max ghosts amount has be reached */
  619.  return;
  620. }
  621.  
  622. /* CREATE PACO */ //creates and initializes the paco
  623. void crear_paco(struct ente &paco, struct ente spawn)
  624. {
  625.  paco.q=(char)PACO;
  626.  paco.x=spawn.x;
  627.  paco.y=spawn.y;
  628.  paco.dir=PACO_DEF_DIR;
  629.  paco.vel=paco.def_vel=PACO_DEF_VEL;
  630.  paco.color=paco.def_color=PACO_DEF_COLOR;
  631.  paco.dopa_color=PACO_DOPA_COLOR;
  632.  return;
  633. }
  634.  
  635. /* ERR */ //handles program erors
  636. void err(int horror)
  637. {
  638.  clrscr();
  639.  switch(horror)
  640.   {
  641.    case PSNF:  printf("\n\nERROR %d: no se encontro el punto de spawn del paco en el mapa",PSNF); break; //paco spawn point missing on map
  642.    case FSNF:  printf("\n\nERROR %d: no se encontro el punto de spawn del fantasma en el mapa",FSNF); break; //ghost spawn point missing on map
  643.    case PANF:  printf("\n\nERROR %d: no se encontro ninguna pastilla en el mapa",PANF); break; //no pill found on map
  644.    case SPANF: printf("\n\nERROR %d: no se encontro ninguna superpastilla en el mapa",SPANF); break; //no superpill found on map //not really an error?
  645.    case MFDG:  printf("\n\nERROR %d: el maximo de fantasmas existentes es mayor a la cantidad de colores posibles",MFDG); break; //max possible amount of ghosts higher than color range
  646.    case FMNE:  printf("\n\nERROR %d: sucedio fruta al tratar de matar un fantasma",FMNE); break; //something bad happened when attempting to create a ghost
  647.   }
  648.  getche();
  649.  return;
  650. }
  651. /*Note on graphic functions: yeah, this is almost as unefficient as possible, run it fullscreen in an not neccessariously old comp and you will wan't to kill yourself rather than play*/
  652.  
  653. /* SHOW TITLE */ //clears screen and shows game title
  654. void muestra_titulo()
  655. {
  656.  textbackground(BLACK);
  657.  textcolor(WHITE);
  658.  clrscr();
  659.  textbackground(BLUE);
  660.  textcolor(YELLOW);
  661.  //gotoxy(20,1); cprintf("*******************************"); //commented to make title thinner
  662.  gotoxy(22,2); cprintf("******                   ******");
  663.  //gotoxy(20,3); cprintf("*******************************"); //commented to make title thinner
  664.  textcolor(YELLOW + BLINK);
  665.  gotoxy(29,2); cprintf("PACOMAN %s",PACOVER);
  666.  gotoxy(POS_INI_X,POS_INI_Y);//leaves the cursor on a convenient place to keep on with the program
  667.  textbackground(BLACK); //resets background color
  668.  textcolor(WHITE);      //resets font color
  669.  return;
  670. }
  671.  
  672. /* SHOW MAP */ //displays the map on the screen
  673. void muestra_mapa()
  674. {
  675.  textcolor(COLOR_MAPA);
  676.  gotoxy(POS_INI_X,POS_INI_Y);
  677.  int i=0, j=0;
  678.  for(i=0;i<ALTOMAPA;i++)
  679.   {
  680.    for(j=0;j<ANCHOMAPA;j++)
  681.     {
  682.      cprintf("%c",MAPA001[i][j]);
  683.     }
  684.    printf("\n");
  685.   }
  686.  textcolor(WHITE);
  687.  return;
  688. }
  689.  
  690. /* SHOW PILLS */
  691. void muestra_pastillas()
  692. {
  693.  textcolor(COLOR_PASTILLAS);
  694.  int i=0, j=0;
  695.  for(i=0;i<ALTOMAPA;i++)
  696.   {
  697.    for(j=0;j<ANCHOMAPA;j++)
  698.     {
  699.      if(pastillas[i][j]!=CHARNULO) //at some point the program seemed to enter this 'if' even when the condition was alse, IDK if I ever fixed that -run it and tell me?-
  700.       {
  701.        gotoxy(POS_INI_X+j,POS_INI_Y+i);
  702.        cprintf("%c",pastillas[i][j]);
  703.       }
  704.     }
  705.   }
  706.  textcolor(WHITE);
  707.  return;
  708. }
  709.  
  710. /* SHOW SCORE AND LIFES */
  711. void muestra_puntaje_y_vidas()
  712. {
  713.  textcolor(COLOR_TEXTO);
  714.  gotoxy(POS_INI_X,POS_INI_Y+ALTOMAPA); //right below the map
  715.  printf("PUNTAJE: %d\t\tVIDAS: %d", g_puntaje, g_vidas);
  716.  textcolor(WHITE);
  717.  return;
  718. }
  719.  
  720. /* SHOW INSTRUCTIONS */ //better name would be "show keys" or "show commands" :/
  721. void muestra_instrucciones()
  722. {
  723.  textcolor(COLOR_TEXTO);
  724.  gotoxy(POS_INI_X,POS_INI_Y+ALTOMAPA+1); //right below the score
  725.  cprintf("Movimiento: Arriba:\'%c\' Izquierda:'\%c' Abajo:\'%c\' Derecha\'%c\' Salir:\'%c\'", MOV_AR, MOV_IZ, MOV_AB, MOV_DE, SALIR/*, PAUSA*/); //pause doesn't do anything yet, so why display the key?
  726.  textcolor(WHITE);
  727.  return;
  728. }
  729.  
  730. /* SHOW ENTITY */ //shows a given entity on it's position and with it's color
  731. void muestra_ente(struct ente pj)
  732. {
  733.  if(g_dopado)
  734.   textcolor(pj.dopa_color);
  735.  else
  736.   textcolor(pj.color); //set the right color
  737.  gotoxy(POS_INI_X+pj.y,POS_INI_Y+pj.x); //show in the right position
  738.  cprintf("%c",pj.q);
  739.  textcolor(WHITE); // set back to default so nothing funky happens later
  740.  return;
  741. }
  742.  
  743.  
  744. /* Teh End */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement