-LIR-

GameKindOfThing

Jun 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <vector>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #define UP 1
  8. #define RIGHT 2
  9. #define DOWN 3
  10. #define LEFT 4
  11.  
  12. using namespace std;
  13.  
  14. char Map[500][500], EmptySpaceChar = ' ', WallChar = '#', PlayerChar = 'O', AIChar = 'X', BulletChar = (char)249;
  15. int MapWidth, MapHeight;
  16. int PlayerX, PlayerY, PlayerX_prev, PlayerY_prev, PlayerSpeed, PlayerDirection, PlayerHealth;
  17. bool needDrawMap = true;
  18.  
  19. class Wall
  20. {
  21. public:
  22.     int X, Y;
  23. };
  24.  
  25. class Bullet
  26. {
  27. public:
  28.     int X, Y, Direction, X_prev, Y_prev, Speed = 1;
  29.  
  30.     void MakeMove()
  31.     {
  32.  
  33.         X_prev = X;
  34.         Y_prev = Y;
  35.  
  36.         if ( Direction == UP ) {
  37.             for( int i=0 ; i < Speed ; i++)
  38.                 if( Y-1 > 0 )
  39.                     Y--;
  40.         }
  41.  
  42.         else if ( Direction == DOWN ) {
  43.             for( int i=0 ; i < Speed ; i++)
  44.                 if( Y+1 < MapHeight )
  45.                     Y++;
  46.         }
  47.  
  48.         else if ( Direction == LEFT ) {
  49.             for( int i= 0 ; i < Speed ; i++ )
  50.                 if( X-1 > 0 )
  51.                     X--;
  52.         }
  53.  
  54.         else if ( Direction == RIGHT ) {
  55.             for( int i=0 ; i < Speed ; i++)
  56.                 if( X+1 < MapWidth )
  57.                     X++;
  58.         }
  59.     }
  60.  
  61.     bool CheckDelete()
  62.     {
  63.         /// daca map[][] e altceva
  64.     }
  65.  
  66.     void Update()
  67.     {
  68.         Map[Y_prev][X_prev] = EmptySpaceChar;
  69.         Map[Y][X] = BulletChar;
  70.  
  71.         if( Y_prev != Y || X_prev != X )
  72.             needDrawMap = true;
  73.     }
  74. };
  75.  
  76. class AI
  77. {
  78. public:
  79.     int X, Y, X_prev, Y_prev, Speed=1, Direction, Health = 100;
  80.     void MakeMove()
  81.     {
  82.         Direction = rand() % 8 + 1;
  83.  
  84.         X_prev = X;
  85.         Y_prev = Y;
  86.  
  87.         if ( Direction == UP ) {
  88.             for( int i=0 ; i < Speed ; i++)
  89.                 if( Y-1 > 0 )
  90.                     if( Map[Y-1][X] == EmptySpaceChar)
  91.                         Y--;
  92.         }
  93.  
  94.         else if ( Direction == DOWN ) {
  95.             for( int i=0 ; i < Speed ; i++)
  96.                 if( Y+1 < MapHeight )
  97.                     if( Map[Y+1][X] == EmptySpaceChar)
  98.                         Y++;
  99.         }
  100.  
  101.         else if ( Direction == LEFT ) {
  102.             for( int i= 0 ; i < Speed ; i++ )
  103.                 if( X-1 > 0 )
  104.                     if( Map[Y][X-1] == EmptySpaceChar)
  105.                         X--;
  106.         }
  107.  
  108.         else if ( Direction == RIGHT ) {
  109.             for( int i=0 ; i < Speed ; i++)
  110.                 if( X+1 < MapWidth )
  111.                     if( Map[Y][X+1] == EmptySpaceChar)
  112.                         X++;
  113.         }
  114.     }
  115.  
  116.     void Update()
  117.     {
  118.         Map[Y_prev][X_prev] = EmptySpaceChar;
  119.         Map[Y][X] = AIChar;
  120.  
  121.         if( Y_prev != Y || X_prev != X )
  122.             needDrawMap = true;
  123.     }
  124. };
  125.  
  126. vector<Wall> WallList;
  127. vector<AI> AIList;
  128. vector<Bullet> BulletList;
  129.  
  130. void PrepareForTheAwesomeness();
  131. void KeyInput();
  132. void UpdatePlayer();
  133. void DrawMapAroundPlayer();
  134.  
  135. int main()
  136. {
  137.     PrepareForTheAwesomeness();
  138.  
  139.     while(1)
  140.     {
  141.         if( needDrawMap )
  142.             DrawMapAroundPlayer();
  143.         KeyInput();
  144.         UpdatePlayer();
  145.         for( int i=0 ; i<AIList.size() ; i++)
  146.         {
  147.             AIList[i].MakeMove();
  148.             AIList[i].Update();
  149.             if( AIList[i].Health <= 0 )
  150.                 AIList.erase( AIList.begin() + i );
  151.         }
  152.     }
  153.     return 0;
  154. }
  155.  
  156. void PrepareForTheAwesomeness()
  157. {
  158.     cin >> MapWidth >> MapHeight;
  159.  
  160.     PlayerX = 3;
  161.     PlayerY = 3;
  162.     PlayerSpeed = 1;
  163.     PlayerDirection = UP;
  164.     PlayerHealth = 100;
  165.     srand(time(NULL));
  166.  
  167.     for( int y=0 ; y<=MapHeight ; y++ )
  168.         for( int x=0 ; x<=MapWidth ; x++ )
  169.             Map[y][x] = EmptySpaceChar;
  170.  
  171.     for( int i=0 ; i<=MapHeight ; i++ )
  172.     {
  173.         Wall wall;
  174.         wall.X = 0;
  175.         wall.Y = i;
  176.         WallList.push_back(wall);
  177.         Map[wall.Y][wall.X] = WallChar;
  178.         wall.X = MapWidth;
  179.         wall.Y = i;
  180.         WallList.push_back(wall);
  181.         Map[wall.Y][wall.X] = WallChar;
  182.     }
  183.     for( int i=0 ; i<=MapWidth ; i++ )
  184.     {
  185.         Wall wall;
  186.         wall.X = i;
  187.         wall.Y = 0;
  188.         WallList.push_back(wall);
  189.         Map[wall.Y][wall.X] = WallChar;
  190.         wall.X = i;
  191.         wall.Y = MapHeight;
  192.         WallList.push_back(wall);
  193.         Map[wall.Y][wall.X] = WallChar;
  194.     }
  195.  
  196.     Wall wall;
  197.     for( int y=5 ; y<=MapHeight ; y+=5 )
  198.         for( int x=1 ; x<=MapWidth-8 ; x++ )
  199.             if( y % 10 == 5 )
  200.             {
  201.                 wall.X = MapWidth-x;
  202.                 wall.Y = y;
  203.                 WallList.push_back(wall);
  204.                 Map[wall.Y][wall.X] = WallChar;
  205.             }
  206.             else
  207.             {
  208.                 wall.X = x;
  209.                 wall.Y = y;
  210.                 WallList.push_back(wall);
  211.                 Map[wall.Y][wall.X] = WallChar;
  212.             }
  213.  
  214.     AI ai;
  215.     ai.X = 17;
  216.     ai.Y = 3;
  217.     Map[ai.Y][ai.X] = AIChar;
  218.     AIList.push_back(ai);
  219.     ai.X = 17;
  220.     ai.Y = 8;
  221.     Map[ai.Y][ai.X] = AIChar;
  222.     AIList.push_back(ai);
  223.     ai.X = 17;
  224.     ai.Y = 13;
  225.     Map[ai.Y][ai.X] = AIChar;
  226.     AIList.push_back(ai);
  227.     ai.X = 17;
  228.     ai.Y = 18;
  229.     Map[ai.Y][ai.X] = AIChar;
  230.     AIList.push_back(ai);
  231.     ai.X = 3;
  232.     ai.Y = 8;
  233.     Map[ai.Y][ai.X] = AIChar;
  234.     AIList.push_back(ai);
  235.     ai.X = 3;
  236.     ai.Y = 13;
  237.     Map[ai.Y][ai.X] = AIChar;
  238.     AIList.push_back(ai);
  239.     ai.X = 3;
  240.     ai.Y = 18;
  241.     Map[ai.Y][ai.X] = AIChar;
  242.     AIList.push_back(ai);
  243.  
  244.     Map[PlayerY][PlayerX] = PlayerChar;
  245. }
  246.  
  247. void KeyInput()
  248. {
  249.     PlayerX_prev = PlayerX;
  250.     PlayerY_prev = PlayerY;
  251.  
  252.     if (GetAsyncKeyState(38) || GetAsyncKeyState(87)) { //UP
  253.         for( int i=0 ; i < PlayerSpeed ; i++)
  254.             if( PlayerY-1 > 0 )
  255.                 if( Map[PlayerY-1][PlayerX] == EmptySpaceChar)
  256.                 {
  257.                     PlayerDirection = UP;
  258.                     PlayerY--;
  259.                 }
  260.     }
  261.  
  262.     else if (GetAsyncKeyState(40) || GetAsyncKeyState(83)) { //DOWN
  263.         for( int i=0 ; i < PlayerSpeed ; i++)
  264.             if( PlayerY+1 < MapHeight )
  265.                 if( Map[PlayerY+1][PlayerX] == EmptySpaceChar)
  266.                 {
  267.                     PlayerDirection = DOWN;
  268.                     PlayerY++;
  269.                 }
  270.     }
  271.  
  272.     else if (GetAsyncKeyState(37) || GetAsyncKeyState(65)) { //LEFT
  273.         for( int i= 0 ; i < PlayerSpeed ; i++ )
  274.             if( PlayerX-1 > 0 )
  275.                 if( Map[PlayerY][PlayerX-1] == EmptySpaceChar)
  276.                 {
  277.                     PlayerDirection = LEFT;
  278.                     PlayerX--;
  279.                 }
  280.     }
  281.  
  282.     else if (GetAsyncKeyState(39) || GetAsyncKeyState(68)) { //RIGHT
  283.         for( int i=0 ; i < PlayerSpeed ; i++)
  284.             if( PlayerX+1 < MapWidth )
  285.                 if( Map[PlayerY][PlayerX+1] == EmptySpaceChar)
  286.                 {
  287.                     PlayerDirection = RIGHT;
  288.                     PlayerX++;
  289.                 }
  290.     }
  291.  
  292.     if( GetAsyncKeyState(16) ) //SHIFT RIGHT
  293.     {
  294.         Map[1][1] = BulletChar;
  295.     }
  296.     Sleep(70);
  297. }
  298.  
  299. void UpdatePlayer()
  300. {
  301.     Map[PlayerY_prev][PlayerX_prev] = EmptySpaceChar;
  302.     Map[PlayerY][PlayerX] = PlayerChar;
  303.  
  304.     if( PlayerY_prev != PlayerY || PlayerX_prev != PlayerX )
  305.         needDrawMap = true;
  306. }
  307.  
  308. void DrawMapAroundPlayer()
  309. {
  310.     system("cls");
  311.  
  312.     cout << endl << endl << endl;
  313.  
  314.     cout << "       Game Play       " << endl;
  315.     cout << "    ";
  316.     for( int i=0 ; i<=14 ; i++ )
  317.         cout << (char)219;
  318.     cout << endl;
  319.  
  320.     for( int y=-6 ; y<=6 ; y++ )
  321.     {
  322.         cout << "    " << (char)219;
  323.         for( int x=-6 ; x<=6 ; x++ )
  324.         {
  325.             if( PlayerX+x < 0 || PlayerX+x > MapWidth || PlayerY+y > MapHeight || PlayerY+y < 0 )
  326.                 cout << WallChar;
  327.             else
  328.                 cout << Map[PlayerY+y][PlayerX+x];
  329.         }
  330.  
  331.         cout << (char)219 << endl;
  332.     }
  333.  
  334.     cout << "    ";
  335.     for( int i=0 ; i<=14 ; i++ )
  336.         cout << (char)219;
  337.     cout << endl;
  338.  
  339.     cout << endl << "    Player's Direction: ";
  340.     if( PlayerDirection == UP )
  341.         cout << "UP" << endl;
  342.     else if( PlayerDirection == DOWN )
  343.         cout << "DOWN" << endl;
  344.     else if( PlayerDirection == LEFT )
  345.         cout << "LEFT" << endl;
  346.     else
  347.         cout << "RIGHT" << endl;
  348.  
  349.     cout << "    Player's Position: (" << PlayerX << "," << PlayerY << ")" << endl;
  350.     cout << "    Player's Health: " << PlayerHealth << endl;
  351.     needDrawMap = false;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment