Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. #include <conio.h>
  5. #include <vector>
  6. #define ARRIBA 72
  7. #define IZQUIERDA 75
  8. #define ABAJO 80
  9. #define DERECHA 77
  10. #define ALTO 2
  11. #define ANCHO 5
  12. int vidas = 3;
  13.  
  14. /**/
  15. #define ALEATORIO(NUM1, NUM2) rand() % (NUM2 - NUM1 + 1) + NUM1;
  16. using namespace std;
  17. using namespace System;
  18. enum Color { NEGRO, ROJO, AZUL, BLANCO, VERDE, AMARILLO };
  19. void CambiarColorFondo(Color c) {
  20. switch (c)
  21. {
  22. case NEGRO: Console::BackgroundColor = ConsoleColor::Black; break;
  23. case ROJO: Console::BackgroundColor = ConsoleColor::Red; break;
  24. case AZUL: Console::BackgroundColor = ConsoleColor::Blue; break;
  25. case BLANCO: Console::BackgroundColor = ConsoleColor::White; break;
  26. case VERDE: Console::BackgroundColor = ConsoleColor::Green; break;
  27. case AMARILLO: Console::BackgroundColor = ConsoleColor::Yellow; break;
  28. }
  29. }
  30. void CambiarColorLetra(Color c) {
  31. switch (c)
  32. {
  33. {
  34. case NEGRO: Console::ForegroundColor = ConsoleColor::Black; break;
  35. case ROJO: Console::ForegroundColor = ConsoleColor::Red; break;
  36. case AZUL: Console::ForegroundColor = ConsoleColor::Blue; break;
  37. case BLANCO: Console::ForegroundColor = ConsoleColor::White; break;
  38. case VERDE: Console::ForegroundColor = ConsoleColor::Green; break;
  39. case AMARILLO: Console::ForegroundColor = ConsoleColor::Yellow; break;
  40. }
  41. }
  42. }
  43. typedef struct personaje {
  44. short x;
  45. short y;
  46. int pasos;
  47. char imagen[ALTO][ANCHO];
  48. personaje(short a = 50, short b = 35, short c = 0) {
  49. x = a;
  50. y = b;
  51. pasos = c;
  52. imagen[0][0] = ' ';
  53. imagen[0][1] = ':';
  54. imagen[0][2] = '^';
  55. imagen[0][3] = ':';
  56. imagen[0][4] = ' ';
  57. imagen[1][0] = ':';
  58. imagen[1][1] = '^';
  59. imagen[1][2] = ':';
  60. imagen[1][3] = '^';
  61. imagen[1][4] = ':';
  62. }
  63. void animar(char direccion) {
  64.  
  65. borrar();
  66. mover(direccion);
  67. restriccion();
  68. dibujar();
  69. }
  70. void dibujar() {
  71.  
  72. for (int i = 0; i < ALTO; i++) {
  73. Console::SetCursorPosition(x, y + i);
  74. for (int j = 0; j < ANCHO; j++) {
  75. if (j == 1 && i == 0 || j == 3 && i == 0 || j == 0 && i == 1 || j == 2 && i == 1 || j == 4 && i == 1) {
  76. CambiarColorLetra(ROJO);
  77. }
  78. else {
  79. CambiarColorLetra(BLANCO);
  80. }
  81. cout << imagen[i][j];
  82. }
  83. }
  84. }
  85. void borrar() {
  86. for (int i = 0; i < ALTO; i++) {
  87. Console::SetCursorPosition(x, y + i);
  88. for (int j = 0; j < ANCHO; j++) {
  89. cout << ' ';
  90. }
  91. }
  92. }
  93. void mover(char direccion) {
  94. switch (toupper(direccion)) {
  95. case ARRIBA: y -= 3; pasos++; break;
  96. case ABAJO: y += 3; pasos++; break;
  97. case IZQUIERDA: x -= 3; pasos++; break;
  98. case DERECHA: x += 3; pasos++; break;
  99. }
  100. }
  101. void restriccion() {
  102. if (x == -1) {
  103. x = 0;
  104. }
  105. else if (y == -1) {
  106. y = 0;
  107. }
  108. }
  109.  
  110. };
  111. typedef struct comandante {
  112.  
  113. short x;
  114. short y;
  115. int pasos;
  116. short dx;
  117. short dy;
  118. short retraso;
  119. int xbala;
  120. int ybala;
  121. int dybala;
  122. short retraso1 = 0;
  123. int disparar;
  124. char imagen[3][6];
  125. comandante(short a = 30, short b = 15, short c = 0) {
  126. x = a;
  127. y = b;
  128. pasos = c;
  129. dx = 1;
  130. dy = 1;
  131. retraso = 0;
  132. xbala = ALEATORIO(30, 60);
  133. ybala = y;
  134. dybala = 1;
  135. imagen[0][0] = ' ';
  136. imagen[0][1] = ' ';
  137. imagen[0][2] = '*';
  138. imagen[0][3] = '*';
  139. imagen[0][4] = ' ';
  140. imagen[0][5] = ' ';
  141. imagen[1][0] = ' ';
  142. imagen[1][1] = ' ';
  143. imagen[1][2] = '*';
  144. imagen[1][3] = '*';
  145. imagen[1][4] = ' ';
  146. imagen[1][5] = ' ';
  147. imagen[2][0] = '{';
  148. imagen[2][1] = '¨';
  149. imagen[2][2] = ' ';
  150. imagen[2][3] = ' ';
  151. imagen[2][4] = '¨';
  152. imagen[2][5] = '}';
  153. }
  154. ~comandante() {
  155. borrar();
  156. Console::SetCursorPosition(xbala, ybala);
  157. cout << " ";
  158. }
  159. void animar(personaje* &personajeA) {
  160.  
  161. if (retraso == 1000) {
  162. scoreVidas(personajeA);
  163. borrar();
  164. mover();
  165. restriccion();
  166. dibujar();
  167. animarBala();
  168. if (ybala == 45) {
  169. borrarBala();
  170. }
  171. retraso = 0;
  172. }
  173. retraso++;
  174. }
  175. void dibujar() {
  176. for (int i = 0; i < 3; i++) {
  177. Console::SetCursorPosition(x, y + i);
  178. for (int j = 0; j < 6; j++) {
  179. if (i < 2) {
  180. CambiarColorLetra(AZUL);
  181.  
  182. }
  183. else {
  184. CambiarColorLetra(AMARILLO);
  185. }
  186. cout << imagen[i][j];
  187. }
  188. }
  189. }
  190. void borrar() {
  191. for (int i = 0; i < 3; i++) {
  192. Console::SetCursorPosition(x, y + i);
  193. for (int j = 0; j < 6; j++) {
  194. cout << ' ';
  195. }
  196. }
  197. }
  198. void mover() {
  199. if (x == 1 || x == 100) {
  200. dx *= -1;
  201. }
  202. x += dx;
  203. }
  204. void restriccion() {
  205. if (x == -1) {
  206. x = 0;
  207. }
  208. else if (y == -1) {
  209. y = 0;
  210. }
  211. }
  212. void scoreVidas(personaje* &personajeA) {
  213. Console::SetCursorPosition(3, 1);
  214. cout << "Vidas: " << vidas;
  215. for (int i = 0; i < 5; ++i) {
  216. if (xbala == personajeA->x + i && ybala == personajeA->y) {
  217. vidas--;
  218.  
  219. }
  220. if (vidas == 1 || vidas == 2) {
  221. personajeA->dibujar();
  222. }
  223.  
  224. }
  225.  
  226. }
  227. void crearBala() {
  228. Console::SetCursorPosition(xbala, ybala);
  229. cout << "*";
  230. }
  231. void borrarBala() {
  232. Console::SetCursorPosition(xbala, ybala);
  233. cout << " ";
  234. }
  235. void animarBala() {
  236. borrarBala();
  237. if (ybala == 45) {
  238. disparar = ALEATORIO(30, 60);
  239. }
  240. if (ybala == 45) {
  241. borrarBala();
  242. }
  243. if (ybala < 45) {
  244. ybala += dybala;
  245. }
  246. if (x == disparar) {
  247. xbala = x + 3;
  248. ybala = y + 3;
  249. }
  250. crearBala();
  251. }
  252.  
  253.  
  254. };
  255. typedef struct bala {
  256. short x;
  257. short y;
  258. bala(short a = 2, short b = 3) {
  259. x = a;
  260. y = b;
  261. }
  262. void animar(comandante* &comandanteA) {
  263. Console::SetCursorPosition(x, y);
  264. cout << " ";
  265. if (y > 0) {
  266. y--;
  267. }
  268.  
  269. Console::SetCursorPosition(x, y);
  270. cout << "*";
  271. eliminarEnemigo(comandanteA);
  272. }
  273. void eliminarEnemigo(comandante* &comandanteA) {
  274.  
  275. }
  276. ~bala() {
  277. Console::SetCursorPosition(x, y);
  278. cout << " ";
  279. }
  280. };
  281. void game() {
  282. int retraso = 0;
  283. int retrasov = 0;
  284. Random random;
  285. personaje* personajeA = new personaje[1];
  286. comandante* comandanteA = new comandante[2];
  287. vector<bala*> disparar;
  288. system("cls");
  289. bool continuar = true;
  290. while (continuar) {
  291.  
  292.  
  293.  
  294.  
  295. if (kbhit()) {
  296. char direccion = getch();
  297. personajeA[0].animar(direccion);
  298. if (direccion == 32) {
  299. disparar.push_back(new bala(personajeA->x + 2, personajeA->y - 1));
  300. }
  301.  
  302. }
  303.  
  304. for (int e = 0; e < 2; e++) {
  305. if (comandanteA[e].y >= 0) {
  306. comandanteA[1].animar(personajeA);
  307. }
  308.  
  309.  
  310.  
  311. for (int i = 0; i < disparar.size(); i++, retraso++) {
  312. if (retraso == 300) {
  313. disparar[i]->animar(comandanteA);
  314. retraso = 0;
  315. }
  316. if (disparar[i]->x == comandanteA[e].x + 1 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 2 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 3 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 4 && disparar[i]->y == comandanteA[e].y || disparar[i]->x == comandanteA[e].x + 5 && disparar[i]->y == comandanteA[e].y) {
  317. delete[e] comandanteA;
  318. }
  319. if (disparar[i]->y == 0) {
  320. delete disparar[i];
  321. disparar.erase(disparar.begin() + i);
  322. i--;
  323. }
  324.  
  325.  
  326. retraso++;
  327. }
  328. }
  329.  
  330.  
  331. }
  332. }
  333.  
  334.  
  335. void main() {
  336.  
  337. Console::CursorVisible = false;
  338. Console::SetWindowSize(120, 50);
  339. game();
  340. return;
  341.  
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement