Advertisement
Mastercpp

pacman+login

Oct 14th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 KB | None | 0 0
  1. #ifndef PACMA_CPP_INCLUDED
  2. #define PACMA_CPP_INCLUDED
  3.  
  4. #include <allegro.h>
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <string>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #define NAME "tito" // para el login
  11. #define PASSWORD "123456" // para el login
  12. #define MAXFILAS 20 // para el eje y
  13. #define MAXCOLS 31 //para el eje x
  14.  
  15. using namespace std;
  16.  
  17.  
  18. BITMAP * buffer;
  19. BITMAP *roca;
  20. BITMAP *pacbmp;
  21. BITMAP *pacman;
  22. BITMAP *comida;
  23. BITMAP *muertebmp;
  24. //musica
  25. MIDI   *musica1;
  26. SAMPLE *bolita;
  27. SAMPLE *caminando;
  28. SAMPLE *muerte;
  29.  
  30. int px = 30*14,py=30*17;
  31. int dir =4;
  32. int anteriorpx,anteriorpy;
  33.  
  34. bool login_correct = false;
  35.  
  36. char mapa[MAXFILAS][MAXCOLS]={
  37.   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  38.   "X  o |o o o XXXXX o o o| o  X",
  39.   "X XXX XXXXX XXXXX XXXXX XXX X",
  40.   "XoXXX XXXXX XXXXX XXXXX XXXoX",
  41.   "X      o|o   o o   o|o      X",
  42.   "XoXXXoXX XXXXXXXXXXX XXoXXXoX",
  43.   "X    |XX    |XXX|    XX     X",
  44.   "XoXXXoXXXXXX XXX XXXXXXoXXXoX",
  45.   "X XXXoXX ooo|ooo|ooo XXoXXX X",
  46.   " o   |XX XXXXXXXXXXX XX|   o ",
  47.   "X XXXoXX XXXXXXXXXXX XXoXXX X",
  48.   "XoXXXoXX oo |ooo|ooo XXoXXXoX",
  49.   "X XXXoXXXXXX XXX XXXXXXoXXX X",
  50.   "X     XX     XXX     XX     X",
  51.   "X XXXoXX XXXXXXXXXXX XXoXXX X",
  52.   "XoXXX| o| o o o o o |o |XXXoX",
  53.   "X XXXoXXXX XXXXXXXX XXX XXX X",
  54.   "XoXXXoXXXX          XXX XXXoX",
  55.   "X  o |o o  XXXXXXXX o o| o  X",
  56.   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  57. };
  58. void dibujar_mapa(){
  59.     int row,col;
  60.     for(row =0; row<MAXFILAS; row++){
  61.         for(col =0; col<MAXCOLS; col++){
  62.             if(mapa[row][col] == 'X'){
  63.                 draw_sprite(buffer,roca, col*30 , row*30);
  64.             }
  65.             else if(mapa[row][col] == 'o'){
  66.                 draw_sprite(buffer,comida, col*30 , row*30);
  67.                 if(py/30 == row && px/30 == col){
  68.                     //play_sample(bolita,300, 150, 100,0);
  69.                     mapa[row][col] = ' ';
  70.                 }
  71.             }
  72.         }
  73.  
  74.     }
  75. }
  76.  
  77. void pantalla(){
  78.     blit(buffer,screen , 0,0,0,0,880,600);
  79. }
  80.  
  81. void dibujar_personaje(){
  82.     blit(pacbmp,pacman,dir*33,0,0,0,33,33);
  83.     draw_sprite(buffer,pacman,px,py);
  84. }
  85.  
  86. bool game_over(){
  87.      int row,col;
  88.     for(row =0; row<MAXFILAS; row++){
  89.         for(col =0; col<MAXCOLS; col++){
  90.             if(mapa[row][col] == 'o')return true;
  91.         }
  92.     }
  93.     return false;
  94. }
  95.  
  96. class Fantasma{
  97. private:
  98.     BITMAP *enemigobmp;
  99.     BITMAP *enemigo;
  100.     int fdir = 0;
  101.     int _x, _y;
  102. public:
  103.     Fantasma(int x , int y);
  104.     void dibujar_fantasma() const;
  105.     void mover_fantasma();
  106.     void choque_pacman();
  107. };
  108.  
  109. Fantasma::Fantasma(int x, int y){
  110.     _x = x;
  111.     _y = y;
  112.     fdir = rand()%4;
  113.     enemigo = create_bitmap(30,30);
  114.     enemigobmp = load_bitmap("enemigo.bmp",NULL);
  115. }
  116. void Fantasma::dibujar_fantasma() const{
  117.     blit(enemigobmp,enemigo,0,0,0,0,30,30);
  118.     draw_sprite(buffer,enemigo,_x,_y);
  119. }
  120. void Fantasma::choque_pacman(){
  121.     if((py == _y &&  px == _x) || (_y == anteriorpy && anteriorpy == _x)) {
  122.         play_sample(muerte,100,150,1000,0);
  123.         for(int j =0;  j<=5; j++){
  124.             clear(pacman);
  125.             clear(buffer);
  126.             dibujar_mapa();
  127.  
  128.             blit(muertebmp,pacman,j*33,0,0,0,33,333);
  129.             draw_sprite(buffer,pacman,px,py);
  130.  
  131.             pantalla();
  132.             rest(80);
  133.         }
  134.         px = 30*14;
  135.         py = 30*17;
  136.         dir = 4;
  137.     }
  138. }
  139.  
  140. void Fantasma::mover_fantasma(){
  141.     dibujar_fantasma();
  142.     choque_pacman();
  143.     if(mapa[_y/30][_x/30] == '|'){
  144.         fdir = rand()%4;
  145.     }
  146.  
  147.     if(fdir == 0){
  148.         if(mapa[_y/30][(_x-30)/30] != 'X') _x-=30;
  149.         else fdir = rand()%4;
  150.     }
  151.     if(fdir == 1){
  152.         if(mapa[_y/30][(_x+30)/30] != 'X') _x+=30;
  153.         else fdir = rand()%4;
  154.     }
  155.  
  156.     if(fdir == 2){
  157.         if(mapa[(_y-30)/30][(_x)/30] != 'X') _y-=30;
  158.         else fdir = rand()%4;
  159.     }
  160.  
  161.       if(fdir == 3){
  162.         if(mapa[(_y+30)/30][(_x)/30] != 'X') _y+=30;
  163.         else fdir = rand()%4;
  164.     }
  165.  
  166.     //rutina atajo
  167.     if(_x <= -30 ) _x = 870;
  168.     else if(_x >= 870) _x= -30;
  169. }
  170.  
  171.  
  172. void start_game(){
  173.     allegro_init();using namespace std;
  174.     install_keyboard();
  175.  
  176.     set_color_depth(32);
  177.     set_gfx_mode(GFX_AUTODETECT_WINDOWED,880,600,0,0);
  178.  
  179.     if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
  180.         allegro_message("Error: inicializando sistema de sonido\n%s\n", allegro_error);
  181.  
  182.     }
  183.  
  184.     //ajustamo el volumen
  185.     set_volume(70, 70);
  186.     musica1 = load_midi("mario.mid");
  187.     bolita = load_wav("coin.wav");
  188.     caminando = load_wav("jump.wav");
  189.     muerte = load_wav("Muerte.wav");
  190.  
  191.     buffer = create_bitmap(880,600);
  192.     roca = load_bitmap("roca.bmp",NULL);
  193.     pacbmp  = load_bitmap("pacman.bmp",NULL);
  194.     pacman = create_bitmap(33,33);
  195.     comida = load_bitmap("Comida.bmp",NULL);
  196.  
  197.     muertebmp = load_bitmap("muerte.bmp",NULL);
  198.  
  199.     Fantasma A(30*2,30*3);
  200.     Fantasma B(30*10,30*11);
  201.     Fantasma C(30*15,30*16);
  202.     Fantasma D(30*21,30*22);
  203.     Fantasma E(30*23,30*25);
  204.     Fantasma F(30*25,30*12);
  205.     Fantasma G(30*20,30*8);
  206.     Fantasma H(30*20,30*8);
  207.  
  208.     play_midi(musica1,1);
  209.     while(!key[KEY_ESC] && game_over() ){
  210.  
  211.         if(dir != 4) play_sample(caminando,100,150,100,0);
  212.  
  213.  
  214.         anteriorpx = px;
  215.         anteriorpy = py;
  216.         if(key[KEY_RIGHT]) dir=1; // rutina para mover a pacman
  217.         else if(key[KEY_LEFT]) dir=0;
  218.         else if(key[KEY_UP]) dir = 2;
  219.         else if(key[KEY_DOWN]) dir = 3;
  220.  
  221.         if(dir == 0){
  222.             if(mapa[py/30][(px-30)/30] != 'X'){
  223.                 px -= 30;
  224.             }
  225.             else dir =4;
  226.         }
  227.         if(dir == 1){
  228.            if(mapa[py/30][(px+30)/30] != 'X'){
  229.                  px += 30;
  230.            }
  231.             else dir =4;
  232.         }
  233.         if(dir == 2){
  234.             if(mapa[(py-30)/30][(px)/30] != 'X'){
  235.                  py -= 30;
  236.              }
  237.             else dir =4;
  238.         }
  239.         if(dir == 3){
  240.             if(mapa[(py+30)/30][(px)/30] != 'X'){
  241.                 py += 30;
  242.              }
  243.             else dir =4;
  244.         }
  245.         //rutina para atajo
  246.         if(px <= -30 ) px = 870;
  247.         else if(px >= 870) px= -30;
  248.  
  249.  
  250.  
  251.         clear(buffer);
  252.         dibujar_mapa();
  253.         dibujar_personaje();
  254.         A.mover_fantasma();
  255.         B.mover_fantasma();
  256.         C.mover_fantasma();
  257.         D.mover_fantasma();
  258.         E.mover_fantasma();
  259.         F.mover_fantasma();
  260.         G.mover_fantasma();
  261.         H.mover_fantasma();
  262.  
  263.         pantalla();
  264.         rest(70);
  265.  
  266.         clear(pacman);
  267.         blit(pacbmp,pacman,4*33,0,0,0,33,33);
  268.         draw_sprite(buffer,pacman,px,py);
  269.         pantalla();
  270.         rest(90);
  271.    }
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  void login(){
  278.     string nombre = "",password="";
  279.     char aux;
  280.     int intentos = 1;
  281.  
  282.     while(true){
  283.         intentos++;
  284.         cout << "\t\tNombre: ";
  285.         cin>>nombre;
  286.  
  287.         cout << "\t\tContrasena: ";
  288.         aux= getch();
  289.  
  290.         while(aux != 13){
  291.             cout << "*";
  292.             password.push_back(aux);
  293.             aux = getch();
  294.         }
  295.  
  296.         if(PASSWORD == password && NAME == nombre){
  297.             cout << "\n\nBienvenido " << nombre << " :) " << endl;
  298.             login_correct = true;
  299.             break;
  300.         }
  301.         else{
  302.             if(intentos > 3){
  303.                 cout << "\n\n\t\thaz tratado de ingresar 3 veces y \n\t\tno haz podido" << endl;
  304.                 break;
  305.             }
  306.             else{
  307.                 cout << "\n\n\t\tcontraseña y/o usuario incorrecto \n\t\tintentelo de nuevo " << endl << endl;
  308.                 rest(50);
  309.                 system("cls");
  310.  
  311.             }
  312.         }
  313.     }
  314.  
  315.  }
  316.  
  317.  
  318.  
  319. #endif // PACMA_CPP_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement