Advertisement
Guest User

Juego con POO a terminar

a guest
Apr 20th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <conio.h>
  5. using namespace std;
  6. using namespace System;
  7.  
  8. #define FILAS 20
  9. #define COLUMNAS 20
  10. enum Terreno { Camino, Pared };
  11. enum Movimiento {ARRIBA=72, ABAJO=80, IZQUIERDA=75, DERECHA=77};
  12.  
  13. //Las balas del protagonista matan al enemigo
  14. //El enemigo mata al protagonista si lo choca
  15. //Definir a criterio propio, cuando se gana y cuando se pierde
  16.  
  17. class CEscenario {
  18. //Definir las paredes para que nadie salga de la consola
  19. short mapa[FILAS][COLUMNAS]={
  20. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  21. {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  22. {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  23. {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  24. {1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1},
  25. {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  26. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  27. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  28. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  29. {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
  30. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  31. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  32. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  33. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  34. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  35. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  36. {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
  37. {1,0,0,0,0,0,0,0,1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  40. };
  41. public:
  42. CEscenario() {
  43. this->dibujar();
  44. }
  45. bool esCamino(short x, short y) {
  46. //suponiendo que en el mapa el valor 0 es camino
  47. return mapa[y][x] == Camino;
  48. }
  49. private:
  50. void dibujar() {
  51. for (short f = 0; f < FILAS; f++) {
  52. for (short c = 0; c < COLUMNAS; c++)
  53. cout << (this->mapa[f][c]==Pared ? "*" : " ");
  54. cout << endl;
  55. }
  56. }
  57. };
  58.  
  59. class CBala {
  60.  
  61. };
  62.  
  63. class CEnemigo {
  64. short x, y;
  65. public:
  66. CEnemigo(short x, short y) {
  67. this->x = x;
  68. this->y = y;
  69. this->dibujar();
  70. }
  71. ~CEnemigo() { }
  72. void mover() {
  73. Random aleatorio;
  74. this->borrar();
  75. this->x += aleatorio.Next(-1, 2);
  76. this->y += aleatorio.Next(-1, 2);
  77. this->dibujar();
  78. }
  79. private:
  80. void borrar() {
  81. Console::SetCursorPosition(this->x, this->y);
  82. cout << " ";
  83. }
  84. void dibujar() {
  85. Console::SetCursorPosition(this->x, this->y);
  86. cout << "X";
  87. }
  88. };
  89.  
  90. class CProtagonista {
  91. short x, y;
  92. vector<CBala*> balas;
  93. public:
  94. CProtagonista()
  95. : x(1), y(1) {
  96. this->dibujar();
  97. }
  98. ~CProtagonista(){
  99. for (CBala* bala : balas)
  100. delete bala;
  101. }
  102. void mover(CEscenario* esc, Movimiento mov) {
  103. this->borrar();
  104. //Evaluas si se mueve a pared o no
  105. switch (mov) {
  106. case ARRIBA : if (esc->esCamino(this->x, this->y - 1)) this->y--; break;
  107. case ABAJO : if (esc->esCamino(this->x, this->y + 1)) this->y++; break;
  108. case IZQUIERDA: if (esc->esCamino(this->x - 1, this->y)) this->x--; break;
  109. case DERECHA : if (esc->esCamino(this->x + 1, this->y)) this->x++; break;
  110. }
  111. this->dibujar();
  112. }
  113. void disparar() {
  114. //disparar balas hacia arriba(w)/abajo(s)/izquierda(a)/derecha(d)
  115. }
  116. private:
  117. void borrar() {
  118. Console::SetCursorPosition(this->x, this->y);
  119. cout << " ";
  120. }
  121. void dibujar() {
  122. Console::SetCursorPosition(this->x, this->y);
  123. cout << "O";
  124. }
  125. };
  126.  
  127. class CJuego {
  128. CEscenario* escenario;
  129. CProtagonista* prot;
  130. vector<CEnemigo*> enemigos;
  131. public:
  132. CJuego() {
  133. this->escenario = new CEscenario;
  134. this->prot = new CProtagonista();
  135. this->inicializarEnemigos(5);
  136. }
  137. ~CJuego() {
  138. delete this->prot;
  139. for (CEnemigo* enemigo : this->enemigos)
  140. delete enemigo;
  141. }
  142. bool jugar() {
  143. char delay = 0;
  144. while (true) {
  145. if (kbhit())
  146. prot->mover(this->escenario, Movimiento(getch()));
  147. if (!delay++) this->moverEnemigos();
  148. }
  149. return true;
  150. }
  151. private:
  152. void inicializarEnemigos(short n) {
  153. Random aleatorio;
  154. for (short i = 0; i < n; i++) {
  155. short x = aleatorio.Next(15, 21);
  156. short y = aleatorio.Next(15, 21);
  157. CEnemigo* nuevoEnemigo = new CEnemigo(x, y);
  158. this->enemigos.push_back(nuevoEnemigo);
  159. }
  160. }
  161. void moverEnemigos() {
  162. for (CEnemigo* enemigo : this->enemigos)
  163. enemigo->mover();
  164. }
  165. };
  166.  
  167. int main() {
  168. Console::CursorVisible = false;
  169. CJuego juego;
  170. juego.jugar();
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement