Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.75 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <ctime>
  4. #include <stdlib.h>
  5. using namespace std;
  6. using namespace System;
  7.  
  8. #define LIMITE_ANCHO 100
  9. #define LIMITE_ALTO 25
  10. #define LIMITER 20
  11. #define DISPARO 32
  12.  
  13. enum Direccion { Arriba = 72, Abajo = 80, Izquierda = 75, Derecha = 77 };
  14. enum Color { Negro, Rojo, Verde, Azul, Blanco, Celeste, Amarillo, Gris, Magenta, DarkRed, DarkBlue, DarkGreen, DarkCyan, DarkYellow, DarkGray, DarkMagenta };
  15. int posicionx = 0;
  16. int posiciony = 0;
  17. int posicionnavex = 0;
  18. int posicionnavey = 0;
  19. int posicionestrellax = 0;
  20. int posicionestrellay = 0;
  21. short aleartorio(short ini, short fin) {
  22. return rand() % (fin - ini + 1) + ini;
  23. }
  24. void colorearCaracter(Color color) {
  25. switch (color) {
  26. case Negro: Console::ForegroundColor = ConsoleColor::Black; break;
  27. case Rojo: Console::ForegroundColor = ConsoleColor::Red; break;
  28. case Verde: Console::ForegroundColor = ConsoleColor::Green; break;
  29. case Azul: Console::ForegroundColor = ConsoleColor::Blue; break;
  30. case Blanco: Console::ForegroundColor = ConsoleColor::White; break;
  31. case Celeste:Console::ForegroundColor = ConsoleColor::Cyan; break;
  32. case Amarillo:Console::ForegroundColor = ConsoleColor::Yellow; break;
  33. case Gris:Console::ForegroundColor = ConsoleColor::Gray; break;
  34. case Magenta:Console::ForegroundColor = ConsoleColor::Magenta; break;
  35. case DarkRed:Console::ForegroundColor = ConsoleColor::DarkRed; break;
  36. case DarkBlue:Console::ForegroundColor = ConsoleColor::DarkBlue; break;
  37. case DarkGreen:Console::ForegroundColor = ConsoleColor::DarkGreen; break;
  38. case DarkCyan:Console::ForegroundColor = ConsoleColor::DarkCyan; break;
  39. case DarkYellow:Console::ForegroundColor = ConsoleColor::DarkYellow; break;
  40. case DarkGray:Console::ForegroundColor = ConsoleColor::DarkGray; break;
  41. case DarkMagenta:Console::ForegroundColor = ConsoleColor::DarkMagenta; break;
  42. }
  43. }
  44.  
  45. void colorearFondo(Color color) {
  46. switch (color) {
  47. case Negro: Console::BackgroundColor = ConsoleColor::Black; break;
  48. case Rojo: Console::BackgroundColor = ConsoleColor::Red; break;
  49. case Verde: Console::BackgroundColor = ConsoleColor::Green; break;
  50. case Azul: Console::BackgroundColor = ConsoleColor::Blue; break;
  51. case Blanco: Console::BackgroundColor = ConsoleColor::White; break;
  52. case Celeste:Console::BackgroundColor = ConsoleColor::Cyan; break;
  53. case Amarillo:Console::BackgroundColor = ConsoleColor::Yellow; break;
  54. case Gris:Console::BackgroundColor = ConsoleColor::Gray; break;
  55. case Magenta:Console::BackgroundColor = ConsoleColor::Magenta; break;
  56. case DarkRed:Console::BackgroundColor = ConsoleColor::DarkRed; break;
  57. case DarkBlue:Console::BackgroundColor = ConsoleColor::DarkBlue; break;
  58. case DarkGreen:Console::BackgroundColor = ConsoleColor::DarkGreen; break;
  59. case DarkCyan:Console::BackgroundColor = ConsoleColor::DarkCyan; break;
  60. case DarkYellow:Console::BackgroundColor = ConsoleColor::DarkYellow; break;
  61. case DarkGray:Console::BackgroundColor = ConsoleColor::DarkGray; break;
  62. case DarkMagenta:Console::BackgroundColor = ConsoleColor::DarkMagenta; break;
  63. }
  64. }
  65.  
  66.  
  67. /************************************************************************************************************************/
  68.  
  69. typedef struct Bala {
  70. short _x, _y;
  71. char _img;
  72.  
  73. Bala(short x = 0, short y = 0) {
  74. _x = x;
  75. _y = y;
  76. _img = '|';
  77. }
  78. void animar() {
  79. borrar();
  80. mover();
  81. dibujar();
  82. }
  83. void borrar() {
  84. Console::SetCursorPosition(_x, _y);
  85. cout << ' ';
  86. }
  87. void mover() {
  88. if (_y > 1) _y--;
  89. }
  90. void dibujar() {
  91. Console::SetCursorPosition(_x, _y);
  92. if (_y == 1) {
  93. cout << " ";
  94. }
  95. else
  96. cout << _img;
  97. }
  98. };
  99.  
  100.  
  101. typedef struct Nave {
  102. short _x, _y;
  103. short _n;
  104. short retraso = 0;
  105. short _vidas = 3;
  106. Bala** balas;
  107. const short _alto = 2, _ancho = 5;
  108. const char _img[2][5] = { { ' ', ' ', char(250), ' ', ' ' },
  109. { '(', '-', 'o', '-', ')' } };
  110. Nave(short x, short y, short n) {
  111. _x = x;
  112. _y = y;
  113. _n = n;
  114. cargarBalas();
  115. }
  116. ~Nave() {
  117. delete[] balas;
  118. }
  119. void animar(Direccion dir) {
  120.  
  121. borrar();
  122. mover(dir);
  123. dibujar();
  124. }
  125. void borrar() {
  126. for (short f = 0; f < _alto; ++f) {
  127. Console::SetCursorPosition(_x, _y + f);
  128. for (short c = 0; c < _ancho; ++c) {
  129. cout << " ";
  130. }
  131. }
  132. }
  133. void mover(Direccion dir) {
  134. switch (dir) {
  135. case Arriba: if (_y - 1 > 0) _y--; break;
  136. case Abajo: if (_y + 1 < LIMITE_ALTO) _y++; break;
  137. case Izquierda: if (_x - 1 > 0) _x--; break;
  138. case Derecha: if (_x + 1 < LIMITE_ANCHO) _x++; break;
  139. posicionnavey = _y;
  140. posicionnavex = _x;
  141. }
  142. }
  143. void dibujar() {
  144. for (short f = 0; f < _alto; ++f) {
  145. Console::SetCursorPosition(_x, _y + f);
  146. for (short c = 0; c < _ancho; ++c) {
  147. cout << _img[f][c];
  148. }
  149. }
  150. }
  151. void cargarBalas() {
  152. balas = new Bala*[_n];
  153. for (short i = 0; i < _n; ++i)
  154. balas[i] = new Bala();
  155. }
  156. Bala* disparar() {
  157. if (_n == 0)
  158. return NULL;
  159. else {
  160. Bala* bala = balas[_n - 1];
  161. bala->_x = _x + 2;
  162. bala->_y = _y - 1;
  163. balas[_n - 1] = NULL;
  164. --_n;
  165. return bala;
  166. }
  167. }
  168. };
  169. typedef struct NaveEnemiga {
  170. short _x, _y, _dx, _dy, _retraso;
  171. char _img[1][5] = { '<','|','*', '|' ,'>' };
  172. short _vidas = 1;
  173. short _n;
  174. public:
  175. NaveEnemiga(short x = 0, short y = 0, short dx = 0, short dy = 0) {
  176. _x = x;
  177. _y = y;
  178. _dx = dx;
  179. _dy = dy;
  180. _retraso = 0;
  181. }
  182. void animar() {
  183. if (_retraso == 20) {
  184. borrar();
  185. mover();
  186. dibujar();
  187. _retraso = 0;
  188. }
  189. _retraso++;
  190. }
  191. void borrar() {
  192. for (short f = 0; f < 1; ++f) {
  193. Console::SetCursorPosition(_x, _y + f);
  194. for (short c = 0; c < 5; ++c) {
  195. cout << " ";
  196. }
  197. }
  198. }
  199. void mover() {
  200. if (_x + _dx < 1 || _x + _dx > LIMITE_ANCHO - 1) _dx *= -1;
  201. if (_y + _dy < 1 || _y + _dy > LIMITE_ALTO - 1) _dy *= -1;
  202. _x += _dx;
  203. }
  204. void dibujar() {
  205. for (short f = 0; f < 1; ++f) {
  206. Console::SetCursorPosition(_x, _y + f);
  207. for (short c = 0; c < 5; ++c) {
  208. cout << _img[f][c];
  209. }
  210. }
  211. }
  212. };
  213.  
  214. void Marco() {
  215. colorearCaracter(Magenta);
  216. for (int i = 0; i <= LIMITE_ALTO + 1; ++i) {
  217. for (int j = 0; j <= LIMITE_ANCHO + 4; ++j)
  218. if (i == 0 || i == LIMITE_ALTO + 1 || j == 0 || j == LIMITE_ANCHO + 4)
  219. cout << "*";
  220. else
  221. cout << " ";
  222. cout << endl;
  223. }
  224. colorearCaracter(Blanco);
  225. }
  226. void imprimirMensaje(Nave &nave, int & contador, short &score) {
  227. Console::SetCursorPosition(LIMITE_ANCHO + 5, 0);
  228. cout << "Vidas: " << nave._vidas;
  229. Console::SetCursorPosition(LIMITE_ANCHO + 5, 2);
  230. if (contador <= 3)
  231. colorearCaracter(Rojo);
  232. cout << "Municion: " << contador;
  233. colorearCaracter(Blanco);
  234. Console::SetCursorPosition(LIMITE_ANCHO + 5, 4);
  235. cout << "Score: " << score;
  236. }
  237. inline bool choque(Nave &nave, NaveEnemiga &enemiga) {
  238. return nave._x == enemiga._x && nave._y == enemiga._y;
  239. }
  240.  
  241.  
  242. typedef struct Estrellas {
  243. short _x, _y, _dy, _retraso, _n;
  244. char _img = '*';
  245.  
  246. public:
  247. Estrellas() {
  248. _x = aleartorio(1,99);
  249. _y = aleartorio(1,24);
  250. _dy = 1;
  251. _retraso = 0;
  252. }
  253. void animar() {
  254. if (_retraso == 3) {
  255. borrar();
  256. mover();
  257. dibujar();
  258. _retraso = 0;
  259. }
  260. _retraso++;
  261. }
  262. void borrar() {
  263. Console::SetCursorPosition(_x, _y);
  264. cout << " ";
  265. }
  266. void mover() {
  267. if (_y + _dy < 1 || _y + _dy > LIMITE_ALTO) _y = 1;
  268.  
  269. _y += _dy;
  270. }
  271. void dibujar() {
  272. Console::SetCursorPosition(_x, _y);
  273. cout << _img;
  274. }
  275.  
  276. };
  277.  
  278.  
  279. void Pantalla_de_Incio() {
  280.  
  281.  
  282. colorearCaracter(DarkCyan);
  283. Console::SetCursorPosition(11, 4);
  284. cout << "*************** ********** **** ********** *************** **********" << endl;
  285. Console::SetCursorPosition(10, 5);
  286. cout << "*************** *********** **** *********** *************** ***********" << endl;
  287. Console::SetCursorPosition(9, 6);
  288. cout << "**** **** **** **** **** **** **** **** ****" << endl;
  289. Console::SetCursorPosition(8, 7);
  290. cout << "**** **** **** **** **** **** **** **** ****" << endl;
  291. Console::SetCursorPosition(7, 8);
  292. cout << "**** **** **** **** **** **** **** **** ****" << endl;
  293. Console::SetCursorPosition(6, 9);
  294. cout << "*************** *************** **** *************** *************** ***************" << endl;
  295. Console::SetCursorPosition(5, 10);
  296. cout << "*************** **************** **** **************** *************** ****************" << endl;
  297. Console::SetCursorPosition(4, 11);
  298. cout << "**** **** **** **** **** **** **** **** **** **** ****" << endl;
  299. Console::SetCursorPosition(3, 12);
  300. cout << "**** **** **** **** **** **** **** **** **** **** ****" << endl;
  301. Console::SetCursorPosition(2, 13);
  302. cout << "*************** **** **** **************** **** **** *************** **** ****" << endl;
  303. Console::SetCursorPosition(1, 14);
  304. cout << "*************** **** **** **************** **** **** *************** **** ****" << endl << endl;
  305. colorearCaracter(Blanco);
  306. Console::SetCursorPosition(38, 16);
  307. cout << "Press any key to continue...";
  308. system("pause>0");
  309. system("cls");
  310. _getch;
  311. }
  312. /*************************************************************/
  313. void jugar() {
  314. int a;
  315. short Nivel1 = 0, Nivel2 = 0, Nivel3 = 0, Nivel4 = 0;
  316. short retraso = 0;
  317. short score = 0;
  318. short nEstrellas = 10;
  319. short nBalas = 10;
  320. int municion = nBalas;
  321. short nBalasActual = 0;
  322. Bala** balas = new Bala*[nBalas];
  323. for (short i = 0; i < nBalas; ++i)
  324. balas[i] = NULL;
  325. Nave* nave = new Nave(LIMITE_ANCHO / 2 - 3, LIMITE_ALTO - 3, nBalas);
  326. NaveEnemiga enemiga1 = NaveEnemiga(3, 9, -1, +1);
  327. NaveEnemiga enemiga2 = NaveEnemiga(9, 17, +1, -1);
  328. NaveEnemiga ene1 =NaveEnemiga (3,3,1,1);
  329. NaveEnemiga ene2 = NaveEnemiga(80, 15, 1, 1);
  330. Estrellas* es = new Estrellas[30];
  331.  
  332. nave->animar(Direccion(Arriba));
  333. while (true) {
  334. if (Nivel1 == 1) {
  335. ene1._retraso = 10;
  336. ene1.animar();
  337. ene2.animar();
  338.  
  339. }
  340.  
  341.  
  342. if (score==200&& Nivel1==0) { //Nivel 1 a Nivel 2
  343. Console::SetCursorPosition(LIMITE_ANCHO / 2-7, LIMITE_ALTO / 2);
  344. cout << "Nivel 2";
  345. if (Nivel2 == 100) {
  346. Console::SetCursorPosition(LIMITE_ANCHO / 2-7, LIMITE_ALTO / 2);
  347. cout << " ";
  348. Nivel1 = 1;;
  349. }
  350. Nivel2++;
  351.  
  352. }
  353. {for (int i = 0; i < 30; ++i) {
  354.  
  355. if ((nave->_x == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 1 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 2 == es[i]._x&& nave->_y == es[i]._y)
  356. || (nave->_x + 3 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 4 == es[i]._x&& nave->_y == es[i]._y) || (nave->_x + 2 == es[i]._x&& nave->_y - 1 == es[i]._y)
  357. || (nave->_x + 1 == es[i]._x&& nave->_y - 1 == es[i]._y) || (nave->_x + 3 == es[i]._x&& nave->_y - 1 == es[i]._y) ||
  358. (nave->_x + 4 == es[i]._x&& nave->_y - 1 == es[i]._y) || (nave->_x == es[i]._x&& nave->_y - 1 == es[i]._y) ||
  359. (enemiga1._x == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x + 1 == es[i]._x && enemiga1._y - 1 == es[i]._y) ||
  360. (enemiga1._x + 2 == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x + 3 == es[i]._x && enemiga1._y - 1 == es[i]._y) ||
  361. (enemiga1._x + 4 == es[i]._x && enemiga1._y - 1 == es[i]._y) || (enemiga1._x == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 1 == es[i]._x && enemiga1._y == es[i]._y) ||
  362. (enemiga1._x + 2 == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 3 == es[i]._x && enemiga1._y == es[i]._y) || (enemiga1._x + 4 == es[i]._x && enemiga1._y == es[i]._y) ||
  363. (enemiga2._x == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x + 1 == es[i]._x && enemiga2._y - 1 == es[i]._y) ||
  364. (enemiga2._x + 2 == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x + 3 == es[i]._x && enemiga2._y - 1 == es[i]._y) ||
  365. (enemiga2._x + 4 == es[i]._x && enemiga2._y - 1 == es[i]._y) || (enemiga2._x == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 1 == es[i]._x && enemiga2._y == es[i]._y) ||
  366. (enemiga2._x + 2 == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 3 == es[i]._x && enemiga2._y == es[i]._y) || (enemiga2._x + 4 == es[i]._x && enemiga2._y == es[i]._y))
  367. es[i].borrar();
  368. else
  369. es[i].animar();
  370.  
  371.  
  372. }
  373.  
  374. imprimirMensaje(*nave, municion, score);
  375.  
  376. if (municion == 0) {
  377. system("cls"); break;
  378.  
  379. }
  380. if (enemiga1._vidas > 0) {
  381. enemiga1.animar();
  382. for (int i = 0; i < nBalas; ++i) {
  383. if (balas[i] != NULL) {
  384. if ((balas[i]->_x == enemiga1._x + 2 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 1 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 3 && balas[i]->_y == enemiga1._y) || (balas[i]->_x == enemiga1._x + 4 && balas[i]->_y == enemiga1._y)) {
  385. enemiga1._vidas = 0;
  386.  
  387. score += 100;
  388. retraso = 0;
  389. }
  390. }
  391.  
  392. }
  393. }
  394. if (enemiga1._vidas == 0) {
  395. enemiga1.borrar();
  396. Console::SetCursorPosition(enemiga1._x + 1, enemiga1._y);
  397. cout << "100";
  398.  
  399. if (retraso == 30) {
  400. Console::SetCursorPosition(enemiga1._x + 1, enemiga1._y);
  401. cout << " ";
  402. enemiga1._vidas = -1;
  403. }
  404. retraso++;
  405. if (enemiga1._vidas == -1)
  406. retraso = 0;
  407.  
  408. }
  409.  
  410. if (enemiga2._vidas > 0) {
  411. enemiga2.animar();
  412. for (int i = 0; i < nBalas; ++i) {
  413. if (balas[i] != NULL) {
  414. if ((balas[i]->_x == enemiga2._x + 2 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 1 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 3 && balas[i]->_y == enemiga2._y) || (balas[i]->_x == enemiga2._x + 4 && balas[i]->_y == enemiga2._y)) {
  415. enemiga2._vidas = 0;
  416.  
  417. score += 100;
  418. retraso = 0;
  419. }
  420. }
  421. }
  422. }
  423. if (enemiga2._vidas == 0) {
  424. enemiga2.borrar();
  425. Console::SetCursorPosition(enemiga2._x + 1, enemiga2._y);
  426. cout << "100";
  427.  
  428. if (retraso == 30) {
  429. Console::SetCursorPosition(enemiga2._x + 1, enemiga2._y);
  430. cout << " ";
  431. enemiga2._vidas = -1;
  432. }
  433. retraso++;
  434. if (enemiga2._vidas == -1)
  435. retraso = 0;
  436.  
  437. }
  438.  
  439.  
  440. if (choque(*nave, enemiga1) || choque(*nave, enemiga2)) {
  441. if (enemiga1._vidas <= 0)
  442. nave->_vidas = nave->_vidas;
  443. else
  444. nave->_vidas--;
  445. }
  446. if (kbhit()) {
  447. char tecla = getch();
  448. if (tecla == DISPARO) {
  449. balas[nBalasActual] = nave->disparar();
  450. municion--;
  451. nBalasActual++;
  452. }
  453. if (Direccion(tecla) == Direccion::Arriba ||
  454. Direccion(tecla) == Direccion::Abajo ||
  455. Direccion(tecla) == Direccion::Izquierda ||
  456. Direccion(tecla) == Direccion::Derecha) {
  457. nave->animar(Direccion(tecla));
  458. }
  459. if (tecla == 13) {
  460. Console::SetCursorPosition(LIMITE_ANCHO / 2 - 4, LIMITE_ALTO / 2);
  461. cout << "PAUSE";
  462.  
  463. system("pause>0");
  464. if (tecla == 13) {
  465. Console::SetCursorPosition(LIMITE_ANCHO / 2 - 4, LIMITE_ALTO / 2);
  466. cout << " ";
  467. }
  468. else
  469. system("pause>0");
  470.  
  471. }
  472. }
  473. for (short i = 0; i < nBalasActual; ++i) {
  474. balas[i]->animar();
  475. }
  476. _sleep(10);
  477. }
  478.  
  479.  
  480. }
  481. delete[] es;
  482.  
  483. Console::SetCursorPosition(LIMITE_ANCHO / 2, LIMITE_ALTO / 2);
  484. cout << "GAME OVER";
  485. Console::SetCursorPosition(LIMITE_ANCHO, LIMITE_ALTO);
  486. cin >> a;
  487. if (a == 1890)
  488. system("cls");
  489. Console::SetCursorPosition(LIMITE_ANCHO / 2, LIMITE_ALTO / 2);
  490. cout << "You win";
  491. system("pause>0");
  492.  
  493. }
  494. /*****************************************************************/
  495. int main() {
  496. srand(time(NULL));
  497. Console::CursorVisible = false;
  498. Pantalla_de_Incio();
  499. Marco();
  500. jugar();
  501.  
  502. return 0;
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement