Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 66.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <Windows.h>
  6. #include <iomanip>
  7. #include <math.h>
  8. #include <conio.h>
  9.  
  10. //TECLAS DE MOVIMIENTO
  11. #define ARRIBA 72
  12. #define IZQUIERDA 75
  13. #define DERECHA 77
  14. #define ABAJO 80
  15.  
  16. #define ye1 9
  17. #define ye2 34
  18.  
  19. //FILAS Y COLUMNAS
  20. #define FILA 45
  21. #define COLUMNA 80
  22. //USO POSTERIOR PARA UNA ACCIÓN CONCRETA
  23. #define ENTER 13
  24.  
  25. using namespace std;
  26. using namespace System;
  27.  
  28. //VARIABLES GLOBALES
  29. int nivel; //PARA POSTERIOR CAMBIO DE NIVEL DESPUÉS DE COMER TODOS LOS BLOQUES
  30. int mapa[FILA][COLUMNA]; //MATRIZ PRINCIPAL QUE DEFINE
  31.  
  32. int puntaje = 0;
  33. int vidas = 7;
  34.  
  35. //
  36. // CARGAR MATRICES DE DIBUJO DE LOS NUMEROS GRANDES
  37. //
  38.  
  39. int ***numeros;
  40.  
  41. void rellenarMatrizNumeros(int ***numeros, int n[5][6], int cod)
  42. {
  43.     for (int i = 0; i < 5; ++i)
  44.     {
  45.         for (int j = 0; j < 6; ++j)
  46.         {
  47.             numeros[cod][i][j] = n[i][j];
  48.         }
  49.     }
  50. }
  51.  
  52. void cargarNumeros()
  53. {
  54.     numeros = new int**[10];
  55.     for (int k = 0; k < 10; ++k)
  56.     {
  57.         numeros[k] = new int*[5];
  58.         for (int i = 0; i < 5; ++i)
  59.         {
  60.             numeros[k][i] = new int[6];
  61.         }
  62.     }
  63.     int n_0[5][6] = { { 0,1,1,1,1,0 },
  64.     { 0,1,0,0,1,0 },
  65.     { 0,1,0,0,1,0 },
  66.     { 0,1,0,0,1,0 },
  67.     { 0,1,1,1,1,0 } };
  68.     int n_1[5][6] = { { 0,1,1,0,0,0 },
  69.     { 0,1,1,0,0,0 },
  70.     { 0,1,1,0,0,0 },
  71.     { 0,1,1,0,0,0 },
  72.     { 0,1,1,0,0,0 } };
  73.     int n_2[5][6] = { { 0,1,1,1,1,0 },
  74.     { 0,0,0,0,1,0 },
  75.     { 0,1,1,1,1,0 },
  76.     { 0,1,0,0,0,0 },
  77.     { 0,1,1,1,1,0 } };
  78.     int n_3[5][6] = { { 0,1,1,1,1,0 },
  79.     { 0,0,0,0,1,0 },
  80.     { 0,1,1,1,1,0 },
  81.     { 0,0,0,0,1,0 },
  82.     { 0,1,1,1,1,0 } };
  83.     int n_4[5][6] = { { 0,1,0,0,1,0 },
  84.     { 0,1,0,0,1,0 },
  85.     { 0,1,1,1,1,0 },
  86.     { 0,0,0,0,1,0 },
  87.     { 0,0,0,0,1,0 } };
  88.     int n_5[5][6] = { { 0,1,1,1,1,0 },
  89.     { 0,1,0,0,0,0 },
  90.     { 0,1,1,1,1,0 },
  91.     { 0,0,0,0,1,0 },
  92.     { 0,1,1,1,1,0 } };
  93.     int n_6[5][6] = { { 0,1,1,1,1,0 },
  94.     { 0,1,0,0,0,0 },
  95.     { 0,1,1,1,1,0 },
  96.     { 0,1,0,0,1,0 },
  97.     { 0,1,1,1,1,0 } };
  98.     int n_7[5][6] = { { 0,1,1,1,1,0 },
  99.     { 0,0,0,0,1,0 },
  100.     { 0,1,1,1,1,0 },
  101.     { 0,0,0,0,1,0 },
  102.     { 0,0,0,0,1,0 } };
  103.     int n_8[5][6] = { { 0,1,1,1,1,0 },
  104.     { 0,1,0,0,1,0 },
  105.     { 0,1,1,1,1,0 },
  106.     { 0,1,0,0,1,0 },
  107.     { 0,1,1,1,1,0 } };
  108.     int n_9[5][6] = { { 0,1,1,1,1,0 },
  109.     { 0,1,0,0,1,0 },
  110.     { 0,1,1,1,1,0 },
  111.     { 0,0,0,0,1,0 },
  112.     { 0,0,0,0,1,0 } };
  113.     rellenarMatrizNumeros(numeros, n_0, 0);
  114.     rellenarMatrizNumeros(numeros, n_1, 1);
  115.     rellenarMatrizNumeros(numeros, n_2, 2);
  116.     rellenarMatrizNumeros(numeros, n_3, 3);
  117.     rellenarMatrizNumeros(numeros, n_4, 4);
  118.     rellenarMatrizNumeros(numeros, n_5, 5);
  119.     rellenarMatrizNumeros(numeros, n_6, 6);
  120.     rellenarMatrizNumeros(numeros, n_7, 7);
  121.     rellenarMatrizNumeros(numeros, n_8, 8);
  122.     rellenarMatrizNumeros(numeros, n_9, 9);
  123. }
  124.  
  125. // ************************************************
  126.  
  127. /* MATRICES DEL PERSONAJE*/
  128.  
  129. int **personaje;
  130.  
  131. void cargarPersonaje()
  132. {
  133.     personaje = new int*[3];
  134.     for (int i = 0; i < 3; ++i)
  135.     {
  136.         personaje[i] = new int[3];
  137.     }
  138. }
  139.  
  140. int personajeArriba[3][3] = { { 0,1,0 },
  141. { 2,4,3 },
  142. { 5,4,5 } };
  143.  
  144. int personajeAbajo[3][3] = { { 5,4,5 },
  145. { 3,4,2 },
  146. { 0,1,0 } };
  147.  
  148. int personajeIzquierda[3][3] = { { 0,2,6 },
  149. { 1,4,4 },
  150. { 0,3,6 } };
  151.  
  152. int personajeDerecha[3][3] = { { 6,3,0 },
  153. { 4,4,1 },
  154. { 6,2,0 } };
  155.  
  156. void copiarPersonajeArriba()
  157. {
  158.     for (int i = 0; i < 3; ++i)
  159.     {
  160.         for (int j = 0; j < 3; ++j)
  161.         {
  162.             personaje[i][j] = personajeArriba[i][j];
  163.         }
  164.     }
  165. }
  166.  
  167. void copiarPersonajeAbajo()
  168. {
  169.     for (int i = 0; i < 3; ++i)
  170.     {
  171.         for (int j = 0; j < 3; ++j)
  172.         {
  173.             personaje[i][j] = personajeAbajo[i][j];
  174.         }
  175.     }
  176. }
  177.  
  178. void copiarPersonajeIzquierda()
  179. {
  180.     for (int i = 0; i < 3; ++i)
  181.     {
  182.         for (int j = 0; j < 3; ++j)
  183.         {
  184.             personaje[i][j] = personajeIzquierda[i][j];
  185.         }
  186.     }
  187. }
  188.  
  189. void copiarPersonajeDerecha()
  190. {
  191.     for (int i = 0; i < 3; ++i)
  192.     {
  193.         for (int j = 0; j < 3; ++j)
  194.         {
  195.             personaje[i][j] = personajeDerecha[i][j];
  196.         }
  197.     }
  198. }
  199.  
  200. //MAPA 1// es necesario para el cambio de nivel
  201. }
  202.  
  203. //FUNCION PARA COLOREAR MAPA
  204. void imprimemapa()
  205. {
  206.     for (int f = 0; f < FILA; f++)
  207.     {
  208.         for (int c = 0; c < COLUMNA; c++)
  209.         {
  210.             Console::SetCursorPosition(c, f); //posiciona el cursor en un punto determinado x y
  211.  
  212.             if (mapa[f][c] == 1)
  213.             {
  214.                 Console::ForegroundColor = ConsoleColor::Red;
  215.                 cout << char(219);
  216.             }
  217.             if (mapa[f][c] == 2)
  218.             {
  219.                 Console::ForegroundColor = ConsoleColor::Yellow;
  220.                 cout << char(219);
  221.             }
  222.             if (mapa[f][c] == 3)
  223.             {
  224.                 Console::ForegroundColor = ConsoleColor::Cyan;
  225.                 cout << char(219);
  226.             }
  227.             if (mapa[f][c] == 4)
  228.             {
  229.                 Console::ForegroundColor = ConsoleColor::Green;
  230.                 cout << char(219);
  231.             }
  232.             if (mapa[f][c] == 5)
  233.             {
  234.                 Console::ForegroundColor = ConsoleColor::DarkMagenta;
  235.                 cout << char(219);
  236.             }
  237.             if (mapa[f][c] == 6)
  238.             {
  239.                 Console::ForegroundColor = ConsoleColor::White;
  240.                 cout << char(219);
  241.             }
  242.             if (mapa[f][c] == 7)
  243.             {
  244.                 Console::ForegroundColor = ConsoleColor::Magenta;
  245.                 cout << char(219);
  246.             }
  247.             if (mapa[f][c] == 8)
  248.             {
  249.                 Console::ForegroundColor = ConsoleColor::Blue;
  250.                 cout << char(219);
  251.             }
  252.             if (mapa[f][c] == 9)
  253.             {
  254.                 Console::ForegroundColor = ConsoleColor::DarkCyan;
  255.                 cout << char(219);
  256.             }
  257.  
  258.         }
  259.     }
  260. }
  261. //MAPA 2// es necesario para el cambio de nivel
  262. }
  263. //MAPA 3// es necesario para el cambio de nivel
  264. }
  265. //MAPA 4// es necesario para el cambio de nivel
  266. }
  267. //MAPA 5// es necesario para el cambio de nivel
  268. }
  269. //FUNCION PARA MANIPULAR COORDENADAS DE CONSOLA
  270. void gotoxy(int x, int y)
  271. {
  272.     HANDLE hcon;
  273.     hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  274.  
  275.     COORD Posicion;
  276.     Posicion.X = x;
  277.     Posicion.Y = y;
  278.  
  279.     SetConsoleCursorPosition(hcon, Posicion);
  280.  
  281. }
  282. //FUNCION PARA IMPRESION DE LA NAVE EN EL MAPA (NO SE MUEVE TODAVIA)
  283. /*void navecita()
  284. {
  285. Console::ForegroundColor = ConsoleColor::DarkGreen;
  286. int x = 39, y = 22;
  287. gotoxy(x, y);
  288. cout << "   ";
  289. gotoxy(x, y + 1);
  290. cout << "   " << char(223) << char(223);
  291. Console::CursorVisible = false;
  292. }*/
  293. //FUNCION PARA MOVER NAVECITA (AUN ESTA EN DESARROLLO) NO PRESIONAR FLECHAS
  294.  
  295. bool hayComida()
  296. {
  297.     for (int i = 0; i < FILA; ++i)
  298.     {
  299.         for (int j = 0; j < COLUMNA; ++j)
  300.         {
  301.             if (mapa[i][j] == 7)
  302.             {
  303.                 return true;
  304.             }
  305.         }
  306.     }
  307.     return false;
  308. }
  309.  
  310. void mostrarVidas()
  311. {
  312.     Console::ForegroundColor = ConsoleColor::Yellow;
  313.     string aux = to_string(vidas);
  314.     for (int k = 0; k < aux.size(); ++k)
  315.     {
  316.         for (int i = 0; i < 5; ++i)
  317.         {
  318.             for (int j = 0; j < 6; ++j)
  319.             {
  320.                 Console::SetCursorPosition(j + 5 * k + 73, i + 39);
  321.                 if ((int)numeros[aux.at(k) - 48][i][j])
  322.                 {
  323.                     cout << char(219);
  324.                 }
  325.                 else
  326.                 {
  327.                     cout << " ";
  328.                 }
  329.             }
  330.         }
  331.     }
  332. }
  333.  
  334. void dibujarNumero()
  335. {
  336.     Console::ForegroundColor = ConsoleColor::Yellow;
  337.     string aux = to_string(puntaje);
  338.     for (int k = 0; k < aux.size(); ++k)
  339.     {
  340.         for (int i = 0; i < 5; ++i)
  341.         {
  342.             for (int j = 0; j < 6; ++j)
  343.             {
  344.                 Console::SetCursorPosition(j + 5 * k + 3, i + 1);
  345.                 if ((int)numeros[aux.at(k) - 48][i][j])
  346.                 {
  347.                     cout << char(219);
  348.                 }
  349.                 else
  350.                 {
  351.                     cout << " ";
  352.                 }
  353.             }
  354.         }
  355.     }
  356. }
  357.  
  358. void dibujarPersonaje(int &x, int &y, char &c)
  359. {
  360.     Console::ForegroundColor = ConsoleColor::Cyan;
  361.     switch (c)
  362.     {
  363.     case ARRIBA:
  364.     {
  365.         copiarPersonajeArriba();
  366.         break;
  367.     }
  368.     case ABAJO:
  369.     {
  370.         copiarPersonajeAbajo();
  371.         break;
  372.     }
  373.     case IZQUIERDA:
  374.     {
  375.         copiarPersonajeIzquierda();
  376.         break;
  377.     }
  378.     case DERECHA:
  379.     {
  380.         copiarPersonajeDerecha();
  381.         break;
  382.     }
  383.     }
  384.     for (int i = 0; i < 3; ++i)
  385.     {
  386.         for (int j = 0; j < 3; ++j)
  387.         {
  388.             Console::SetCursorPosition(x + j, y + i);
  389.             if (personaje[i][j] == 0)
  390.             {
  391.                 cout << " ";
  392.             }
  393.             else if (personaje[i][j] == 1)
  394.             {
  395.                 cout << char(158);
  396.             }
  397.             else if (personaje[i][j] == 2)
  398.             {
  399.                 cout << char(47);
  400.             }
  401.             else if (personaje[i][j] == 3)
  402.             {
  403.                 cout << char(92);
  404.             }
  405.             else if (personaje[i][j] == 4)
  406.             {
  407.                 cout << char(219);
  408.             }
  409.             else if (personaje[i][j] == 5)
  410.             {
  411.                 cout << char(186);
  412.             }
  413.             else if (personaje[i][j] == 6)
  414.             {
  415.                 cout << char(205);
  416.             }
  417.         }
  418.     }
  419. }
  420.  
  421. void borrarPersonaje(int &x, int &y)
  422. {
  423.     for (int i = 0; i < 3; ++i)
  424.     {
  425.         for (int j = 0; j < 3; ++j)
  426.         {
  427.             Console::SetCursorPosition(x + j, y + i);
  428.             cout << " ";
  429.         }
  430.     }
  431. }
  432.  
  433. bool estaEnComida(int &x, int &y)
  434. {
  435.     for (int i = 0; i < 3; ++i)
  436.     {
  437.         for (int j = 0; j < 3; ++j)
  438.         {
  439.             if (mapa[i + y][j + x] == 7)
  440.             {
  441.                 return true;
  442.             }
  443.         }
  444.     }
  445.     return false;
  446. }
  447.  
  448. void moverPersonaje(int &x, int &y, char &c)
  449. {
  450.     switch (c)
  451.     {
  452.     case ARRIBA:
  453.     {
  454.         if (mapa[y - 1][x] != 1)
  455.         {
  456.             borrarPersonaje(x, y);
  457.             --y;
  458.             dibujarPersonaje(x, y, c);
  459.         }
  460.         break;
  461.     }
  462.     case ABAJO:
  463.     {
  464.         if (mapa[y + 3][x] != 1)
  465.         {
  466.             borrarPersonaje(x, y);
  467.             ++y;
  468.             dibujarPersonaje(x, y, c);
  469.         }
  470.         break;
  471.     }
  472.     case IZQUIERDA:
  473.     {
  474.         if (mapa[y][x - 1] != 1)
  475.         {
  476.             borrarPersonaje(x, y);
  477.             --x;
  478.             dibujarPersonaje(x, y, c);
  479.         }
  480.         break;
  481.     }
  482.     case DERECHA:
  483.     {
  484.         if (mapa[y][x + 3] != 1)
  485.         {
  486.             borrarPersonaje(x, y);
  487.             ++x;
  488.             dibujarPersonaje(x, y, c);
  489.         }
  490.         break;
  491.     }
  492.     }
  493.     if (estaEnComida(x, y))
  494.     {
  495.         puntaje += 50;
  496.         for (int i = 0; i < 3; ++i)
  497.         {
  498.             for (int j = 0; j < 3; ++j)
  499.             {
  500.                 mapa[i + y][j + x] = 0;
  501.             }
  502.         }
  503.         dibujarNumero();
  504.     }
  505. }
  506.  
  507. void dibujarEnemigos(int &xe1, int &xe2)
  508. {
  509.     Console::ForegroundColor = ConsoleColor::Green;
  510.     Console::SetCursorPosition(xe1, ye1);
  511.     cout << char(219) << char(219) << char(219);
  512.     Console::SetCursorPosition(xe1, ye1 + 1);
  513.     cout << char(219) << char(219) << char(219);
  514.     Console::SetCursorPosition(xe1, ye1 + 2);
  515.     cout << " " << char(219) << " ";
  516.     Console::SetCursorPosition(xe2, ye2);
  517.     cout << " " << char(219) << " ";
  518.     Console::SetCursorPosition(xe2, ye2 + 2);
  519.     cout << char(219) << char(219) << char(219);
  520.     Console::SetCursorPosition(xe2, ye2 + 1);
  521.     cout << char(219) << char(219) << char(219);
  522. }
  523.  
  524. void borrarEnemigos(int &xe1, int &xe2)
  525. {
  526.     for (int i = 0; i < 3; ++i)
  527.     {
  528.         for (int j = 0; j < 3; ++j)
  529.         {
  530.             Console::SetCursorPosition(xe1 + j, ye1 + i);
  531.             cout << " ";
  532.             Console::SetCursorPosition(xe2 + j, ye2 + i);
  533.             cout << " ";
  534.         }
  535.     }
  536. }
  537.  
  538. void moverEnemigos(int &xe1, int &xe2)
  539. {
  540.     static int dx1 = 1;
  541.     static int dx2 = -1;
  542.     if (xe1 + dx1 + 2 > 60 || xe1 + dx1 < 14)
  543.     {
  544.         dx1 *= -1;
  545.     }
  546.     if (xe2 + dx2 + 2 > 60 || xe2 + dx2 < 14)
  547.     {
  548.         dx2 *= -1;
  549.     }
  550.     borrarEnemigos(xe1, xe2);
  551.     xe1 += dx1;
  552.     xe2 += dx2;
  553.     dibujarEnemigos(xe1, xe2);
  554. }
  555.  
  556. void movimiento()
  557. {
  558.     int x = 40, y = 22;
  559.     int xe1 = 31;
  560.     int xe2 = 20;
  561.     char c = ARRIBA;
  562.     dibujarNumero();
  563.     dibujarPersonaje(x, y, c);
  564.     mostrarVidas();
  565.     do
  566.     {
  567.         moverEnemigos(xe1, xe2);
  568.         for (int i = 0; i < 10000000; ++i);
  569.         if (_kbhit())
  570.         {
  571.             c = getch();
  572.             moverPersonaje(x, y, c);
  573.         }
  574.     } while (hayComida());
  575. }
  576. void enemigo()
  577. {
  578.  
  579. }
  580. //FUNCION PRINCIPAL DONDE SE LLAMAN A LOS MAPAS
  581. //PRESIONAR ENTER PARA CAMBIAR DE MAPA, NO PRESIONAR FLECHAS
  582. int main()
  583. {
  584.     //Console::SetWindowSize(80, 45); //tamaño de consola
  585.     Console::CursorVisible = false;
  586.     cargarNumeros();
  587.     cargarPersonaje();
  588.     puntaje = 0;
  589.     vidas = 6;
  590.     copianivel();
  591.     imprimemapa();
  592.     movimiento();
  593.     //enemigo();
  594.     system("cls");
  595.     copianivel2();
  596.     imprimemapa();
  597.     movimiento();
  598.     system("cls");
  599.     copianivel3();
  600.     imprimemapa();
  601.     movimiento();
  602.     system("cls");
  603.     copianivel4();
  604.     imprimemapa();
  605.     movimiento();
  606.     system("cls");
  607.     copianivel5();
  608.     imprimemapa();
  609.     movimiento();
  610.     _getch();
  611.     return 0;
  612. }
  613. /*_getch();
  614. Console::Clear();
  615. copianivel2();
  616. imprimemapa();
  617. navecita();
  618. _getch();
  619. Console::Clear();
  620. copianivel3();
  621. imprimemapa();
  622. navecita();
  623. _getch();
  624. Console::Clear();
  625. copianivel4();
  626. imprimemapa();
  627. navecita();
  628. _getch();
  629. Console::Clear();
  630. copianivel5();
  631. imprimemapa();
  632. navecita();
  633. _getch();
  634. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement