Advertisement
seseboye

Top-Down Game

Jan 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <string>
  4. #include <conio.h>
  5. #include <time.h>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
  11. // Keyboard input
  12.     #define KEY_W 119
  13.     #define KEY_A 97
  14.     #define KEY_S 115
  15.     #define KEY_D 100
  16. //--------------------
  17. // Variables
  18.     int const columns=10,rows=10,maxgoals=(columns+rows)/10; // Game Parameters
  19.     int goals;
  20.     int grid[columns][rows];
  21.     int X,Y; // Grid Counters
  22.     int PX,PY; // Player Coordinates
  23.     int GX[maxgoals],GY[maxgoals]; // Goal Coordinates
  24.     int i;
  25.     int wall,WX,WY,LR; // Wall Variables
  26.     int DX[rows/2],DY[rows/2],d; // Door Variables
  27.  
  28. void walls(); // Function for generating walls. (declaration)
  29.  
  30. void draw() // Function for drawing the board
  31. {
  32.     cout<<endl;
  33.  
  34.     for ( Y=0 ; Y<rows ; Y++ )
  35.     {
  36.         for ( X=0 ; X<columns ; X++ )
  37.         {
  38.  
  39.             cout<<"  ";
  40.             if ( grid [Y][X] == 0) // Empty Spots
  41.             {
  42.                 if ( ( grid [Y+1][X] == grid [PY][PX] ) || ( grid [Y-1][X] == grid [PY][PX] ) || ( grid [Y][X+1] == grid [PY][PX] ) || ( grid [Y][X-1] == grid [PY][PX] ) )
  43.                     SetConsoleTextAttribute(h, 7);
  44.                 else
  45.                     SetConsoleTextAttribute(h, 0);
  46.                 cout<<".";
  47.             }
  48.             else if ( grid [Y][X] == 1) // The Player
  49.             {
  50.                 SetConsoleTextAttribute(h, 11);
  51.                 cout<<"@";
  52.                 SetConsoleTextAttribute(h, 10);
  53.             }
  54.             else if ( grid [Y][X] == 2) // Goal
  55.             {
  56.                 if ( ( grid [Y+1][X] == grid [PY][PX] ) || ( grid [Y-1][X] == grid [PY][PX] ) || ( grid [Y][X+1] == grid [PY][PX] ) || ( grid [Y][X-1] == grid [PY][PX] ) )
  57.                     SetConsoleTextAttribute(h, 14);
  58.                 else
  59.                     SetConsoleTextAttribute(h, 0);
  60.                 cout<<"$";
  61.                 SetConsoleTextAttribute(h, 10);
  62.             }
  63.             else if ( grid [Y][X] == 3) // Walls
  64.             {
  65.                 SetConsoleTextAttribute(h, 8);
  66.                 cout<<"+";
  67.                 SetConsoleTextAttribute(h, 10);
  68.             }
  69.             else if ( grid [Y][X] == 4) // Ladder
  70.                 cout<<"H";
  71.             else if ( grid [Y][X] == 5) // Closed Door
  72.             {
  73.                 SetConsoleTextAttribute(h, 4);
  74.                 cout<<"X";
  75.                 SetConsoleTextAttribute(h, 10);
  76.             }
  77.             else if ( grid [Y][X] == 6) // Open Door
  78.             {
  79.                 SetConsoleTextAttribute(h, 4);
  80.                 cout<<"/";
  81.                 SetConsoleTextAttribute(h, 10);
  82.             }
  83.         }
  84.  
  85.     cout<<endl;
  86.     }
  87.     SetConsoleTextAttribute(h, 15);
  88.     cout<<"\n You have to collect "<<goals<<" more!"<<endl;
  89. }
  90.  
  91.  
  92. int main()
  93. {
  94.     SetConsoleTextAttribute(h, 10);
  95.     walls();
  96.     srand(time(NULL));
  97.     do
  98.     {
  99.     Y=rand()%rows;
  100.     X=rand()%3;
  101.     }
  102.     while (grid[Y][X]!=0);
  103.  
  104.     grid[Y][X]=1;
  105.     PX=X;
  106.     PY=Y;
  107.     while ( goals < maxgoals )
  108.     {
  109.         do
  110.         {
  111.         Y=rand()%rows;
  112.         X=rand()%columns;
  113.         }
  114.         while (grid[Y][X]!=0);
  115.  
  116.         grid[Y][X]=2;
  117.         GX[goals]=X;
  118.         GY[goals]=Y;
  119.         goals++;
  120.     }
  121.     _sleep(800);
  122.     system("cls");
  123.  
  124.     while ( goals > 0 )
  125.     {
  126.         draw();
  127.         cout<<"\n You are the ";
  128.         SetConsoleTextAttribute(h, 12);
  129.         cout<<"'@'";
  130.         SetConsoleTextAttribute(h, 15);
  131.         cout<<", you are supposed to collect the goals signed with ";
  132.         SetConsoleTextAttribute(h, 14);
  133.         cout<<"'$'"<<endl;
  134.         SetConsoleTextAttribute(h, 15);
  135.         cout<<"\n Which way do you want to go? (w for up, a for left, s for Down and d for right)"<<endl;
  136.         char key=getch();
  137.         int value=key;
  138.         switch (value)
  139.         {
  140.         case KEY_W:
  141.             if ( ((PY-1) != (rows-rows-1)) && ( grid[PY-1][PX]!=3) )
  142.             {
  143.                 if (grid[PY-1][PX]!=5)
  144.                 {
  145.                     if (grid[PY][PX]==grid[DY[d]][DX[d]])
  146.                         grid[PY][PX]=6;
  147.                     else
  148.                         grid[PY][PX]=0;
  149.                     grid[PY-1][PX]=1;
  150.                     PY-=1;
  151.                 }
  152.                 else if (grid[PY-1][PX]==5)
  153.                     grid[PY-1][PX]=6;
  154.             }
  155.         break;
  156.  
  157.         case KEY_A:
  158.             if ( ((PX-1) != (columns-columns-1)) && ( grid[PY][PX-1]!=3) )
  159.             {
  160.                 if (grid[PY][PX-1]!=5)
  161.                 {
  162.                     if (grid[PY][PX]==grid[DY[d]][DX[d]])
  163.                         grid[PY][PX]=6;
  164.                     else
  165.                         grid[PY][PX]=0;
  166.  
  167.                     grid[PY][PX-1]=1;
  168.                     PX-=1;
  169.                 }
  170.                 else if (grid[PY][PX-1]==5)
  171.                     grid[PY][PX-1]=6;
  172.             }
  173.         break;
  174.  
  175.         case KEY_S:
  176.             if ( ((PY+1) != (rows)) && ( grid[PY+1][PX]!=3) )
  177.             {
  178.                 if (grid[PY+1][PX]!=5)
  179.                 {
  180.                     if (grid[PY][PX]==grid[DY[d]][DX[d]])
  181.                         grid[PY][PX]=6;
  182.                     else
  183.                         grid[PY][PX]=0;
  184.  
  185.                     grid[PY+1][PX]=1;
  186.                     PY+=1;
  187.                 }
  188.                 else if (grid[PY+1][PX]==5)
  189.                     grid[PY+1][PX]=6;
  190.             }
  191.  
  192.         break;
  193.  
  194.         case KEY_D:
  195.             if ( ((PX+1) != (columns)) && ( grid[PY][PX+1]!=3) )
  196.             {
  197.                 if (grid[PY][PX+1]!=5)
  198.                 {
  199.                     if (grid[PY][PX]==grid[DY[d]][DX[d]])
  200.                         grid[PY][PX]=6;
  201.                     else
  202.                         grid[PY][PX]=0;
  203.  
  204.                     grid[PY][PX+1]=1;
  205.                     PX+=1;
  206.                 }
  207.                 else if (grid[PY][PX+1]==5)
  208.                     grid[PY][PX+1]=6;
  209.             }
  210.  
  211.         break;
  212.  
  213.         default:
  214.             cout<<" Invalid input. Only lowercase w,a,s and d inputs are valid. Press any key to continue.";
  215.             _getch();
  216.         break;
  217.         }
  218.         while (i<maxgoals)
  219.         {
  220.             if ( ( PX == GX[i] ) && ( PY == GY[i] ) )
  221.             {
  222.                 GX[i]=0;
  223.                 GY[i]=0;
  224.                 goals--;
  225.             }
  226.             i++;
  227.         }
  228.         i=0;
  229.         system("cls");
  230.     }
  231.  
  232.         cout<<" You win! Congratulations!"<<endl;
  233.         _getch();
  234.     return 0;
  235. }
  236.  
  237. void walls() // Function for generating walls . (definition)
  238. {
  239.     srand(time(NULL));
  240.     WX=rand()%(columns-7)+(columns/5+1);
  241.     grid[WY][WX]=3;
  242.     grid[WY+1][WX]=3;
  243.     grid[WY+2][WX]=3;
  244.     WY+=3;
  245.     wall+=3;
  246.     while (wall <= rows)
  247.     {
  248.         if (WX >= (columns-3))
  249.             LR=rand()%(2)+(-1);
  250.         else if (WX <= 3)
  251.             LR=rand()%(3);
  252.         else
  253.             LR=rand()%(3)+(-1);
  254.  
  255.         if (wall==columns)
  256.             LR=0;
  257.         if (LR==0)
  258.         {
  259.             grid[WY][WX]=3;
  260.             DY[d]=WY-1;
  261.             DX[d]=WX;
  262.             d++;
  263.             grid[WY+1][WX]=3;
  264.             WY+=2;
  265.             wall+=2;
  266.         }
  267.         else if (LR==(-1))
  268.         {
  269.             grid[WY-1][WX-1]=3;
  270.             DY[d]=WY;
  271.             DX[d]=WX-1;
  272.             d++;
  273.             grid[WY][WX-1]=3;
  274.             grid[WY+1][WX-1]=3;
  275.             WX-=1;
  276.             WY+=2;
  277.             wall+=2;
  278.         }
  279.         else if (LR==(1))
  280.         {
  281.             grid[WY-1][WX+1]=3;
  282.             DY[d]=WY;
  283.             DX[d]=WX+1;
  284.             d++;
  285.             grid[WY][WX+1]=3;
  286.             grid[WY+1][WX+1]=3;
  287.             WX+=1;
  288.             WY+=2;
  289.             wall+=2;
  290.         }
  291.     }
  292.     d=rand()%((rows/2)-1);
  293.  
  294.     grid[DY[d]][DX[d]]=5;
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement