Advertisement
Guest User

Juego Implementado con Clases

a guest
Aug 21st, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace System;
  5.  
  6. #define Arriba 'w'
  7. #define Abajo 's'
  8. #define Izquierda 'a'
  9. #define Derecha 'd'
  10.  
  11. #define Blanco 0
  12. #define Negro 1
  13. #define Rojo 2
  14.  
  15. void configurarColorFondo(short color) {
  16. switch (color)
  17. {
  18. case Blanco: Console::BackgroundColor = ConsoleColor::White; break;
  19. case Negro: Console::BackgroundColor = ConsoleColor::Black; break;
  20. }
  21. }
  22. void configurarColorTexto(short color) {
  23. switch (color)
  24. {
  25. case Blanco: Console::ForegroundColor = ConsoleColor::White; break;
  26. case Negro: Console::ForegroundColor = ConsoleColor::Black; break;
  27. case Rojo: Console::ForegroundColor = ConsoleColor::Red; break;
  28. }
  29. }
  30.  
  31. class CEscenario {
  32. //Caracteristicas
  33. const short Filas = 20;
  34. const short Columnas = 20;
  35. short Mapa[20][20] = {
  36. { 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1 },
  37. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  38. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  39. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  40. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  41. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  42. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  43. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  44. { 1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1 },
  45. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  46. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  47. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  48. { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1 },
  49. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  50. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  51. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  52. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  53. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  54. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  55. { 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1 }
  56. };
  57. public:
  58. //Funciones
  59. CEscenario() {
  60. this->mostrar();
  61. }
  62. bool esCamino(short x, short y) {
  63. return (x >= 0 && x < 20 &&
  64. y >= 0 && y < 20 &&
  65. Mapa[y][x] == Blanco);
  66. }
  67. private:
  68. void mostrar() {
  69. for (short fila = 0; fila < Filas; fila++) {
  70. for (short columna = 0; columna < Columnas; columna++) {
  71. Console::SetCursorPosition(columna, fila);
  72. short color = Mapa[fila][columna];
  73. configurarColorFondo(color);
  74. cout << " ";
  75. }
  76. }
  77. }
  78. };
  79. class CEnemigo {
  80. private:
  81. //Caracteristicas
  82. short x, y;
  83. short dx = 1;
  84. short retraso = 5000, cont = 0;
  85. //Funciones
  86. public:
  87. CEnemigo(CEscenario &esc) {
  88. this->asignarLugar(esc);
  89. _sleep(500);
  90. }
  91. void mover(CEscenario &esc) {
  92. if (cont == retraso) {
  93. borrar();
  94. if (!esc.esCamino(x + dx, y)) dx *= -1;
  95. x += dx;
  96. dibujar();
  97. cont = 0;
  98. }
  99. cont++;
  100. }
  101. short getX() {return this->x; }
  102. short getY() { return this->y; }
  103. private:
  104. void asignarLugar(CEscenario &esc) {
  105. Random r;
  106. do {
  107. x = r.Next(0, 20);
  108. y = r.Next(0, 20);
  109. } while (!esc.esCamino(x, y));
  110. }
  111. void borrar() {
  112. Console::SetCursorPosition(x, y);
  113. configurarColorFondo(Blanco);
  114. cout << " ";
  115. }
  116. void dibujar() {
  117. Console::SetCursorPosition(x, y);
  118. configurarColorTexto(Negro);
  119. cout << "*";
  120. }
  121. };
  122. class CProtagonista {
  123. //Caracteristicas
  124. short x = 1, y = 1;
  125. //Funciones
  126. public:
  127. CProtagonista(short x, short y) {
  128. this->x = x;
  129. this->y = y;
  130. this->dibujar();
  131. }
  132. void mover(CEscenario &esc, char mov) {
  133. borrar();
  134. if (mov == Arriba && esc.esCamino(x, y - 1)) y--;
  135. else if (mov == Abajo && esc.esCamino(x, y + 1)) y++;
  136. else if (mov == Derecha && esc.esCamino(x + 1, y)) x++;
  137. else if (mov == Izquierda && esc.esCamino(x - 1, y)) x--;
  138. dibujar();
  139. }
  140. void matar(CEnemigo* &e) {
  141. if (this->x == e->getX() && this->y == e->getY()) {
  142. delete e;
  143. e = NULL;
  144. }
  145. }
  146. private:
  147. void borrar() {
  148. Console::SetCursorPosition(x, y);
  149. configurarColorFondo(Blanco);
  150. cout << " ";
  151. }
  152. void dibujar() {
  153. Console::SetCursorPosition(x, y);
  154. configurarColorTexto(Rojo);
  155. cout << char(1);
  156. }
  157. };
  158.  
  159. void jugar() {
  160. Console::CursorVisible = false;
  161. CEscenario esc;
  162. CProtagonista prot = CProtagonista(1,1);
  163. CEnemigo *e1 = new CEnemigo(esc);
  164. CEnemigo *e2 = new CEnemigo(esc);
  165. configurarColorFondo(Blanco);
  166. while (true) {
  167. //Si se apretó alguna tecla
  168. if (kbhit())
  169. prot.mover(esc, getch());
  170. //Si el enemigo e1 está vivo
  171. if (e1) {
  172. e1->mover(esc);
  173. prot.matar(e1);
  174. }
  175. //Si el enemigo e2 está vivo
  176. if (e2) {
  177. e2->mover(esc);
  178. prot.matar(e2);
  179. }
  180. }
  181. }
  182.  
  183. int main() {
  184. jugar();
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement