Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 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. /**/
  13. #define ALEATORIO(NUM1, NUM2) rand() % (NUM2 - NUM1 + 1) + NUM1;
  14. using namespace std;
  15. using namespace System;
  16. enum Color { NEGRO, ROJO, AZUL, BLANCO, VERDE, AMARILLO };
  17. void CambiarColorFondo(Color c) {
  18. switch (c)
  19. {
  20. case NEGRO: Console::BackgroundColor = ConsoleColor::Black; break;
  21. case ROJO: Console::BackgroundColor = ConsoleColor::Red; break;
  22. case AZUL: Console::BackgroundColor = ConsoleColor::Blue; break;
  23. case BLANCO: Console::BackgroundColor = ConsoleColor::White; break;
  24. case VERDE: Console::BackgroundColor = ConsoleColor::Green; break;
  25. case AMARILLO: Console::BackgroundColor = ConsoleColor::Yellow; break;
  26. }
  27. }
  28. void CambiarColorLetra(Color c) {
  29. switch (c)
  30. {
  31. {
  32. case NEGRO: Console::ForegroundColor = ConsoleColor::Black; break;
  33. case ROJO: Console::ForegroundColor = ConsoleColor::Red; break;
  34. case AZUL: Console::ForegroundColor = ConsoleColor::Blue; break;
  35. case BLANCO: Console::ForegroundColor = ConsoleColor::White; break;
  36. case VERDE: Console::ForegroundColor = ConsoleColor::Green; break;
  37. case AMARILLO: Console::ForegroundColor = ConsoleColor::Yellow; break;
  38. }
  39. }
  40. }
  41. typedef struct bala {
  42. short x;
  43. short y;
  44. bala(short a = 2, short b = 3) {
  45. x = a;
  46. y = b;
  47. }
  48. void animar() {
  49. Console::SetCursorPosition(x, y);
  50. cout << " ";
  51. if (y > 0) {
  52. y--;
  53. }
  54. Console::SetCursorPosition(x, y);
  55. cout << "*";
  56. }
  57. ~bala() {
  58. Console::SetCursorPosition(x, y);
  59. cout << " ";
  60. }
  61. };
  62. typedef struct personaje {
  63. short x;
  64. short y;
  65. int pasos;
  66. char imagen[ALTO][ANCHO];
  67. personaje(short a = 50, short b = 35, short c = 0) {
  68. x = a;
  69. y = b;
  70. pasos = c;
  71. imagen[0][0] = ' ';
  72. imagen[0][1] = ':';
  73. imagen[0][2] = '^';
  74. imagen[0][3] = ':';
  75. imagen[0][4] = ' ';
  76. imagen[1][0] = ':';
  77. imagen[1][1] = '^';
  78. imagen[1][2] = ':';
  79. imagen[1][3] = '^';
  80. imagen[1][4] = ':';
  81. }
  82. void animar(char direccion) {
  83. borrar();
  84. mover(direccion);
  85. restriccion();
  86. dibujar();
  87. }
  88. void dibujar() {
  89.  
  90. for (int i = 0; i < ALTO; i++) {
  91. Console::SetCursorPosition(x, y + i);
  92. for (int j = 0; j < ANCHO; j++) {
  93. if (j == 1 && i == 0 || j == 3 && i == 0 || j == 0 && i == 1 || j == 2 && i == 1 || j == 4 && i == 1) {
  94. CambiarColorLetra(ROJO);
  95. }
  96. else {
  97. CambiarColorLetra(BLANCO);
  98. }
  99. cout << imagen[i][j];
  100. }
  101. }
  102. }
  103. void borrar() {
  104. for (int i = 0; i < ALTO; i++) {
  105. Console::SetCursorPosition(x, y + i);
  106. for (int j = 0; j < ANCHO; j++) {
  107. cout << ' ';
  108. }
  109. }
  110. }
  111. void mover(char direccion) {
  112. switch (toupper(direccion)) {
  113. case ARRIBA: y--; pasos++; break;
  114. case ABAJO: y++; pasos++; break;
  115. case IZQUIERDA: x--; pasos++; break;
  116. case DERECHA: x++; pasos++; break;
  117. }
  118. }
  119. void restriccion() {
  120. if (x == -1) {
  121. x = 0;
  122. }
  123. else if (y == -1) {
  124. y = 0;
  125. }
  126. }
  127.  
  128. };
  129.  
  130.  
  131.  
  132. void game() {
  133. Random random;
  134. personaje* personajeA = new personaje[1];
  135. vector<bala*> disparar;
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. system("cls");
  143. bool continuar = true;
  144.  
  145. while (continuar) {
  146. if (kbhit()) {
  147. char direccion = getch();
  148. personajeA[0].animar(direccion);
  149. if (direccion == 32) {
  150. disparar.push_back(new bala(personajeA->x + 2, personajeA->y - 1));
  151. }
  152. }
  153. for (int i = 0; i < disparar.size(); i++) {
  154. disparar[i]->animar();
  155. if (disparar[i]->y == 0) {
  156. delete disparar[i];
  157. disparar.erase(disparar.begin() + i);
  158. i--;
  159.  
  160. }
  161. _sleep(50);
  162. }
  163.  
  164. }
  165. }
  166.  
  167. void main() {
  168.  
  169. Console::CursorVisible = false;
  170. Console::SetWindowSize(120, 50);
  171. game();
  172. return;
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement