Advertisement
Guest User

Jueguito

a guest
Aug 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. using namespace std;
  5. using namespace System;
  6.  
  7. #define Filas 20
  8. #define Columnas 50
  9.  
  10. #define Arriba 'w'
  11. #define Abajo 's'
  12. #define Izquierda 'a'
  13. #define Derecha 'd'
  14.  
  15. struct enemigo {
  16. short x, y;
  17. short cont = 0, retraso = 100;
  18. short dy = 1;
  19. void asignarPosicion() {
  20. Random r;
  21. do {
  22. x = r.Next(2, Columnas);
  23. y = r.Next(2, Filas);
  24. } while (!(x > 1 && x < Columnas &&
  25. y > 1 && y < Filas));
  26. }
  27. void mover() {
  28. if (cont == retraso) {
  29. borrar();
  30. if (y + dy == 1 || y + dy == Filas) dy *= -1;
  31. y += dy;
  32. dibujar();
  33. cont = 0;
  34. }
  35. cont++;
  36. }
  37. void dibujar() {
  38. Console::SetCursorPosition(x, y);
  39. cout << "*";
  40. }
  41. void borrar() {
  42. Console::SetCursorPosition(x, y);
  43. cout << " ";
  44. }
  45. };
  46.  
  47. struct protagonista {
  48. short x=2, y=2;
  49. void mover(char mov) {
  50. borrar();
  51. if (mov == Arriba && y - 1 > 1 ) y--;
  52. if (mov == Abajo && y + 1 < Filas ) y++;
  53. if (mov == Izquierda && x - 1 > 1 ) x--;
  54. if (mov == Derecha && x + 1 < Columnas) x++;
  55. dibujar();
  56. }
  57. void dibujar() {
  58. Console::SetCursorPosition(x, y);
  59. cout << char(1);
  60. }
  61. void borrar() {
  62. Console::SetCursorPosition(x, y);
  63. cout << " ";
  64. }
  65. void matar(enemigo* &e) {
  66. if (this->x == e->x &&
  67. this->y == e->y) {
  68. delete e;
  69. e = NULL;
  70. }
  71. }
  72. };
  73.  
  74. void pintarMapa() {
  75. for (short columna = 1; columna <= Columnas; columna++) {
  76. for (short fila = 1; fila <= Filas; fila++) {
  77. if (fila == 1 || fila == Filas ||
  78. columna == 1 || columna == Columnas) {
  79. Console::SetCursorPosition(columna, fila);
  80. cout << char(219);
  81. }
  82. }
  83. }
  84. }
  85.  
  86. void jugar() {
  87. Console::CursorVisible = false;
  88. pintarMapa();
  89. protagonista prot;
  90. enemigo *e1, *e2;
  91. e1 = new enemigo;
  92. e2 = new enemigo;
  93. e1->asignarPosicion(); _sleep(500);
  94. e2->asignarPosicion();
  95. prot.dibujar();
  96. while (true) {
  97. if (kbhit())
  98. prot.mover(getch());
  99. if (e1) {
  100. e1->mover();
  101. prot.matar(e1);
  102. }
  103. if (e2) {
  104. e2->mover();
  105. prot.matar(e2);
  106. }
  107. }
  108. }
  109.  
  110. int main() {
  111. jugar();
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement