Advertisement
Guest User

juego muy bien hecho

a guest
Nov 11th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. //BIBLIOTECAS :
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <time.h>
  6. using namespace std;
  7. using namespace System;
  8.  
  9.  
  10. //COMANDO MOVIMIENTOS :
  11. #define ARRIBA 'W'
  12. #define IZQUIERDA 'A'
  13. #define ABAJO 'S'
  14. #define DERECHA 'D'
  15.  
  16. #define DISPARAR 32
  17. #define LIMITEX 20
  18. #define ALTO 2
  19. #define ANCHO 3
  20. #define ALEATORIO(INI, FIN) rand() % (FIN - INI + 1) + INI
  21. enum Color { NEGRO, BLANCO, AZUL, ROJO, VERDE, AMARILLO };
  22.  
  23. //FUNCION DE COLOR :
  24.  
  25. void cambiarColorFondo(Color miColor) {
  26. switch (miColor)
  27. {
  28. case NEGRO: Console::BackgroundColor = ConsoleColor::Black; break;
  29. case BLANCO: Console::BackgroundColor = ConsoleColor::White; break;
  30. case AZUL: Console::BackgroundColor = ConsoleColor::Blue; break;
  31. case ROJO: Console::BackgroundColor = ConsoleColor::Red; break;
  32. case VERDE: Console::BackgroundColor = ConsoleColor::Green; break;
  33. case AMARILLO: Console::BackgroundColor = ConsoleColor::Yellow; break;
  34. }
  35. }
  36. void cambiarColorCaracter(Color miColor) {
  37. switch (miColor)
  38. {
  39. case NEGRO: Console::ForegroundColor = ConsoleColor::Black; break;
  40. case BLANCO: Console::ForegroundColor = ConsoleColor::White; break;
  41. case AZUL: Console::ForegroundColor = ConsoleColor::Blue; break;
  42. case ROJO: Console::ForegroundColor = ConsoleColor::Red; break;
  43. case VERDE: Console::ForegroundColor = ConsoleColor::Green; break;
  44. case AMARILLO: Console::ForegroundColor = ConsoleColor::Yellow; break;
  45. }
  46. }
  47. struct Piedra {
  48. short x = 0, y = 0;
  49. bool movimiento = false;
  50. bool colision(short x1, short y1, short ancho, short largo) {
  51. if (x <= x1 + ancho && x + 1 >= x1 && y <= y1 + largo && y + 1 >= y1)
  52. {
  53. return true;
  54. }
  55. return false;
  56. }
  57. void posicionar(short _x, short _y) {
  58. movimiento = true;
  59. x = _x;
  60. y = _y;
  61. }
  62. void animar() {
  63. if (movimiento) {
  64. borrar();
  65. mover();
  66. dibujar();
  67. }
  68. }
  69. void mover() {
  70. if (y - 1 >= 0)y--;
  71. }
  72. void dibujar() {
  73. cambiarColorCaracter(AMARILLO);
  74. Console::SetCursorPosition(x, y);
  75. cout << "-";
  76. }
  77. void borrar() {
  78. cambiarColorCaracter(NEGRO);
  79. Console::SetCursorPosition(x, y);
  80. if (y == 0 || y >= 0)
  81. cout << " ";
  82.  
  83. }
  84. };
  85.  
  86. //ESTRUCTURA DEL PERSONAJE
  87.  
  88. struct personaje {
  89. short x;
  90. short y;
  91. char p[2][3]{ { ' ', '^', ' ' },
  92. { '>', 'x', '<' } };
  93. short cantpiedra;
  94. Piedra* piedrita;
  95. personaje(short _x = 0, short _y = 0) {
  96. x = _x;
  97. y = _y;
  98. cantpiedra = 0;
  99. piedrita = new Piedra[cantpiedra];
  100. }
  101. void moverPiedritas() {
  102. for (short i = 0; i < cantpiedra; i++)
  103. {
  104. piedrita[i].animar();
  105. }
  106. }
  107. void eliminarPiedrita(short posicion) {
  108. Piedra* tem = new Piedra[cantpiedra - 1];
  109. for (short i = 0; i < cantpiedra; i++)
  110. {
  111. if (i == posicion)
  112. {
  113. for (short j = i + 1; j < cantpiedra; j++)
  114. {
  115. tem[j - 1] = piedrita[j];
  116. }
  117. break;
  118. }
  119. else
  120. {
  121. tem[i] = piedrita[i];
  122. }
  123. }
  124. delete piedrita;
  125. piedrita = tem;
  126. cantpiedra--;
  127. }
  128. void añadirPiedrita() {
  129. Piedra* tempo = new Piedra[cantpiedra + 1];
  130. Piedra nuevo;
  131. nuevo.posicionar(x + ANCHO - 2, y + ALTO - 3);
  132. for (short i = 0; i < cantpiedra; i++)
  133. {
  134. tempo[i] = piedrita[i];
  135. }
  136. tempo[cantpiedra] = nuevo;
  137. delete piedrita;
  138. piedrita = tempo;
  139. cantpiedra++;
  140. }
  141. void interactuar(char comando) {
  142. if (comando == DISPARAR) {
  143. añadirPiedrita();
  144. }
  145. else
  146. {
  147. borrar();
  148. mover(comando);
  149. dibujar();
  150. }
  151. }
  152. void animar(char direccion) {
  153. borrar();
  154. mover(direccion);
  155. dibujar();
  156. }
  157. void mover(char direccion) {
  158. switch (toupper(direccion)) {
  159. case ARRIBA: if (y - 1 >= 0) y--; break;
  160. case ABAJO: if (y + 1 <= 29) y++; break;
  161. case IZQUIERDA: if (x - 1 >= 0) x--; break;
  162. case DERECHA: if (x + 1 <= 60) x++; break;
  163. }
  164. }
  165. void dibujar() {
  166. cambiarColorCaracter(AZUL);
  167. for (short i = 0; i < 2; i++) {
  168. Console::SetCursorPosition(x, y + i);
  169. for (short j = 0; j < 3; j++) {
  170. cout << p[i][j];
  171. }
  172. }
  173. cambiarColorCaracter(NEGRO);
  174. }
  175. void borrar() {
  176. cambiarColorCaracter(NEGRO);
  177. for (short f = 0; f < ALTO; ++f) {
  178. Console::SetCursorPosition(x, y + f);
  179. for (short c = 0; c < ANCHO; ++c) {
  180. cout << ' ';
  181. }
  182. }
  183. }
  184. };
  185.  
  186. //ESTRUCTURA DE AVISPAS:
  187.  
  188. struct avispas {
  189. float x;
  190. float y;
  191. bool vive;
  192. char pi[2][3]{ { '<', '^', '>' },
  193. { ' ', '*', ' ' } };
  194. float dx;
  195. float dy;
  196. avispas(short _x = 4, short _y = 4) {
  197. x = _x;
  198. y = _y;
  199. dx = 1;
  200. dy = 1;
  201. vive = true;
  202. }
  203.  
  204. void animar() {
  205. borrar();
  206. mover();
  207. dibujar();
  208. }
  209.  
  210. void borrar() {
  211. cambiarColorCaracter(NEGRO);
  212. for (int i = 0; i < 2; i++) {
  213. Console::SetCursorPosition(x, y + i);
  214. for (int j = 0; j < 3; j++) {
  215. cout << " ";
  216. }
  217. }
  218. }
  219.  
  220. void mover() {
  221. if (x == 0 || x == 60) dx *= -1;
  222. if (y == 0 || y == 18) dy *= -1;
  223. x += dx;
  224. y += dy;
  225. }
  226. void dibujar() {
  227. cambiarColorCaracter(ROJO);
  228. for (int i = 0; i < 2; i++) {
  229. Console::SetCursorPosition(x, y + i);
  230. for (int j = 0; j < 3; j++) {
  231. cout << pi[i][j];
  232. }
  233. }
  234. cambiarColorCaracter(NEGRO);
  235. }
  236. };
  237.  
  238. // FUNCION JUGAR:
  239.  
  240. void jugar() {
  241. short cantavispas = 5;
  242. personaje p = personaje(35, 25);
  243. avispas* F = new avispas[cantavispas];
  244. for (int i = 0; i < 5; i++)
  245. {
  246. int yAux = rand() % 16 + 1;
  247. int xAux = rand() % 16 + 1;
  248. F[i] = avispas(xAux, yAux);
  249. }
  250. bool continuar = true;
  251. while (continuar) {
  252. if (_kbhit()) {
  253. char comando = getch();
  254. p.interactuar(comando);
  255. }
  256. p.moverPiedritas();
  257. for (short i = 0; i < p.cantpiedra; i++)
  258. {
  259. if (p.piedrita[i].y <= 0)
  260. {
  261. p.piedrita[i].borrar();
  262. p.eliminarPiedrita(i);
  263. i--;
  264. }
  265. }
  266. for (int i = 0; i < cantavispas; i++) {
  267. F[i].animar();
  268. }
  269. for (short i = 0; i < p.cantpiedra; i++)
  270. {
  271. for (short j = 0; j < cantavispas; j++)
  272. {
  273. if (p.piedrita[i].colision(F[j].x, F[j].y, 3, 2))
  274. {
  275. p.piedrita[i].borrar();
  276. p.eliminarPiedrita(i);
  277. F[j].borrar();
  278. avispas* tem = new avispas[cantavispas - 1];
  279. for (short a = 0; a < cantavispas; a++)
  280. {
  281. if (a == j)
  282. {
  283. for (short j = a + 1; j < cantavispas; j++)
  284. {
  285. tem[j - 1] = F[j];
  286. }
  287. break;
  288. }
  289. else
  290. {
  291. tem[a] = F[a];
  292. }
  293. }
  294. delete[] F;
  295. F = tem;
  296. cantavispas--;
  297. j--;
  298. i--;
  299. }
  300. }
  301. }
  302. _sleep(60);
  303. }
  304. delete[] F;
  305. }
  306.  
  307. //FUNCION PRINCIPAL:
  308.  
  309. int main() {
  310. Console::CursorVisible = false;
  311. Console::SetWindowSize(70, 30);
  312. char opcion;
  313. bool menu = true;
  314. Console::Clear();
  315. while (menu)
  316. {
  317. Console::Clear();
  318. cout << endl << endl << endl << endl << endl << endl;
  319.  
  320. cout << " * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
  321. cout << " * BIENVENIDO A GALAGA *" << endl;
  322. cout << " * -------------------- *" << endl;
  323. cout << " * PRESIONE 1 PARA JUGAR *" << endl;
  324. cout << " * PRESIONE 2 PARA CONTROLES *" << endl;
  325. cout << " * PRESIONE 3 PARA CREDITOS *" << endl;
  326. cout << " * PRESIONE 4 PARA SALIR *" << endl;
  327. cout << " * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
  328.  
  329. cout << endl << endl << endl << endl << endl;
  330. cout << "INGRESE UNA OPCION:\t\t"; cin >> opcion;
  331. switch (opcion)
  332. {
  333. case '1':
  334. Console::Clear();
  335. jugar();
  336. break;
  337. case '2':
  338. Console::Clear();
  339. cout << "W PARA ARRIBA, A PARA IZQUIERDA, D PARA DERECHA, S PARA ABAJO" << endl;
  340. cout << "Presiona cualquier tecla para salir" << endl;
  341. _getch();
  342. break;
  343. case '3':
  344. Console::Clear();
  345. cout << "JUEGO ELABORADO POR MILNER Y CRISTHIAN" << endl;
  346. cout << "Presiona cualquier tecla para salir" << endl;
  347. _getch();
  348. break;
  349. case '4':
  350. Console::Clear();
  351. menu = false;
  352. break;
  353. }
  354. }
  355. cout << "Gracias por jugar" << endl;
  356. system("pause>0");
  357. return 0;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement