Advertisement
ssr17

Maze runner

Mar 22nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const char width = 20, height = 10;
  5. unsigned char player = {1};
  6. int posX = 1, posY = 1;
  7. unsigned char action;
  8.  
  9. void playerAction ();
  10.  
  11. struct enemies
  12. {
  13.     char symbol;
  14.     bool active;
  15.     int x, y;
  16. };
  17.  
  18. unsigned char maze[height][width] = {
  19. {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',}, //0
  20. {'#',' ',' ',' ',' ',' ',' ',' ','#','#',' ',' ',' ',' ',' ',' ','#','#',' ','#',}, //1
  21. {'#',' ','#',' ','#','#','#',' ',' ',' ',' ','#','#',' ','#',' ',' ',' ',' ','#',}, //2
  22. {'#',' ','#',' ','#',' ','#',' ','#',' ','#','#',' ',' ',' ','#',' ','#','#','#',}, //3
  23. {'#',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ','#',' ','#',' ',' ',' ','#',}, //4
  24. {'#','#',' ','#',' ','#','#','#','#',' ','#',' ','#','#',' ',' ',' ','#',' ','#',}, //5
  25. {'#',' ',' ','#',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#',' ','#','#',' ','#',}, //6
  26. {'#',' ','#','#','#',' ','#',' ',' ',' ','#','#','#',' ',' ',' ',' ','#',' ','#',}, //7
  27. {'#',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ','#','#',' ',' ',' ','#',}, //8
  28. {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',},};//9
  29. //0   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5   6   7   8   9
  30.  
  31.  
  32. int main ()
  33. {
  34.     while (action != 'Q'||'q')
  35.     {    cout << "Control your player using WASD" << endl;
  36.          cout << endl;
  37.  
  38.          maze [posX][posY] = player;
  39.  
  40.          for (int y = 0; y < height; y++){
  41.             cout << endl;
  42.             for (int x = 0; x < width; x++)
  43.                 cout << maze[y][x];
  44.  
  45.             //1st enemy
  46.             enemies enemy1;
  47.             enemy1.symbol = 'X';
  48.             enemy1.active = true;
  49.             enemy1.x = 3;
  50.             enemy1.y = 6;
  51.             maze[enemy1.y][enemy1.x] = enemy1.symbol;
  52.  
  53.             //2nd enemy
  54.             enemies enemy2;
  55.             enemy2.symbol = 'Y';
  56.             enemy2.active = true;
  57.             enemy2.x = 8;
  58.             enemy2.y = 1;
  59.             maze[enemy2.y][enemy2.x] = enemy2.symbol;
  60.  
  61.             //3rd enemy
  62.             enemies enemy3;
  63.             enemy.symbol = 'Z';
  64.             enemy3.active = true;
  65.             enemy3.x = 17;
  66.             enemy3.y = 6;
  67.             maze[enemy1.y][enemy1.x] = enemy1.symbol;
  68.      
  69.         }//end 1st for loop
  70.    
  71.         playerAction ();
  72.     }//end while loop
  73.  
  74.     return 0;
  75. }
  76.  
  77. void playerAction ()
  78. {
  79.     cout << "\nAction: ";
  80.     cin >> action;
  81.  
  82.     int prevposX = posX;
  83.     int prevposY = posY;
  84.     unsigned char space = {32};
  85.  
  86.     switch (action)
  87.     {
  88.         case 'a' :
  89.        
  90.             if (maze [posX][posY-1] != '#')
  91.             {
  92.                 posY--;
  93.                 cout << posX << ',' << posY << endl;
  94.                 maze [prevposX][prevposY] = space;
  95.             }
  96.             system("cls");
  97.             break;
  98.         case 'd' :
  99.        
  100.             if (maze [posX][posY+1] != '#')
  101.             {
  102.                 posY++;
  103.                 cout << posX << ',' << posY << endl;
  104.                 maze [prevposX][prevposY] = space;
  105.             }
  106.             system("cls");
  107.             break;
  108.         case 's' :
  109.        
  110.             if (maze [posX+1][posY] != '#')
  111.             {
  112.                 posX++;
  113.                 cout << posX << ',' << posY << endl;
  114.                 maze [prevposX][prevposY] = space;
  115.             }
  116.             system("cls");
  117.             break;
  118.         case 'w' :
  119.        
  120.             if (maze [posX-1][posY] != '#')
  121.             {
  122.                 posX--;
  123.                 cout << posX << ',' << posY << endl;
  124.                 maze [prevposX][prevposY] = space;
  125.             }
  126.             system("cls");
  127.             break;
  128.         default:
  129.             cout << "Incorrect action!!!" << endl;
  130.             break;
  131.    }//end swtch
  132.  
  133. }//end func playerAct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement