Advertisement
Guest User

Untitled

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