Xavistian

Ejercicio virtual con arreglos

Aug 29th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <conio.h>
  4. #include <iostream>
  5. #include "CJugador.h"
  6. using namespace std;
  7. using namespace System;
  8.  
  9. // CJugador.h
  10.  
  11. #pragma once
  12. #include "stdafx.h"
  13.  
  14. class CJugador
  15. {
  16. private:
  17.     int x, y, dx, col;
  18.     char car;
  19. public:
  20.     CJugador(int _x, int _y, int _col, int _dx, char _car);
  21.     int getx();
  22.     void setdx(int _dx);
  23.     void Mostrar();
  24.     void Ocultar();
  25.     void Mover();
  26.     ~CJugador();
  27. };
  28.  
  29. // CJugador.cpp
  30.  
  31.  
  32. #include "stdafx.h"
  33.  
  34. void CJugador::Mostrar()
  35. {  
  36.     Console::ForegroundColor = (ConsoleColor)col;
  37.     Console::SetCursorPosition(x, y);
  38.     cout << car;
  39. }
  40. void CJugador::Ocultar()
  41. {
  42.     Console::SetCursorPosition(x, y);
  43.     cout << ' ';
  44. }
  45. void CJugador::Mover()
  46. {
  47.     x += dx;
  48. }
  49. CJugador::CJugador(int _x, int _y, int _dx, int _col, char _car)
  50. {
  51.     x = _x; y = _y; dx = _dx; col = _col; car = _car;
  52. }
  53. void CJugador::setdx(int _dx) { dx = _dx; };
  54. int CJugador::getx() { return x; }
  55. CJugador::~CJugador()
  56. {}
  57.  
  58. // Main.cpp
  59.  
  60. #include "stdafx.h"
  61.  
  62. int main()
  63. {
  64.     Console::WindowHeight = 50;
  65.     Console::WindowWidth = 100;
  66.     Random x;
  67.     CJugador ** Arreglo = new CJugador *[3];
  68.     for (int i = 0; i < 3; i++)
  69.         Arreglo[i] = new CJugador(5, 5 + 5 * i, x.Next(1, 14), x.Next(1, 4), (char)62 + i);
  70.     /*CJugador*obj1 = new CJugador(5, 5, x.Next(1, 14), x.Next(1, 4), '@');
  71.     CJugador*obj2 = new CJugador(5, 5, x.Next(1, 14), x.Next(1, 4), '*');
  72.     CJugador*obj3 = new CJugador(5, 5, x.Next(1, 14), x.Next(1, 4), '#');*/
  73.     bool jugar = true;
  74.     for (int i = 0; i < 16; i++)
  75.     {
  76.         Console::SetCursorPosition(74, i);
  77.         cout << '|';
  78.     }
  79.     for (int i = 0; i < 3; i++)
  80.         Arreglo[i]->Mostrar();
  81.     while (jugar)
  82.     {
  83.         _sleep(300);
  84.         for (int i = 0; i < 3; i++)
  85.             Arreglo[i]->Ocultar();
  86.         for (int i = 0; i < 3; i++)
  87.             Arreglo[i]->Mover();
  88.         for (int i=0; i<3;i++)
  89.             if (Arreglo[i]->getx()>=74)
  90.         {
  91.             jugar = false;
  92.             Arreglo[i]->setdx(0);
  93.             Arreglo[i]->Mostrar();
  94.         }
  95.         for (int i = 0; i < 3; i++)
  96.             Arreglo[i]->Mostrar();
  97.     }
  98.     Console::SetCursorPosition(35, 20);
  99.     cout << "Fin del juego";
  100.     _getch();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment