C++ Maze game

+2 Lachezar Peshterliev · August 14, 2015
Hi , guys I have started to do a maze game . I have done big part of it , but I cannot understand ho to enter into a new level and how to change the color of some elements in the array . Please help 


#include<iostream>
#include<windows.h>
#include<conio.h>

using namespace std;


    
char map[20][40] = {
 "#######################################",
 "#O         X    XXXXX             XX  #",
 "#          X      XX        XXXX   XXX#",
 "#  XXXXXXXXXXXXX  XXXXXXXX    XX      #",
 "#  XX        XXX              XXXXX   #",
 "#  XXXX      XXX  XXXX   X    XX  X   #",
 "#     X     XXXX  XX X   X    XX  X   #",
 "#P XX X     X     XX X   X    XX  X X #",
 "#  XX X     X     XX X   XX       X X #",
 "#  XX X           XX X   XXXXXXX    X #",
 "#   X          XXXXX           XXXXXX #",
 "#X  X    X     X      X     XXXXX   X #",
 "#X  X    XXX   X      X  XXXXX  X   X #",
 "#X  X      X   X      XXXX      X   X #",
 "#   X  XX  X   XXXX   X     XX  X   X #",
 "#  XXXXXX  X      X   X    XX         #",
 "#          X          X    XXXX X XXXX#",
 "#         XXXXXXXX    X    X    X    F#",
 "#######################################"

};

int x = 1, y = 1;
bool game_running = true;

int main()
{

    while(game_running == true)
         {
          system("cls");
          for(int display = 0; display < 20; display++)
             {
               cout<<map[display]<<endl;
             }    
            
            if(GetAsyncKeyState(VK_DOWN))
              {
               int y2 = y + 1;
               if(map[y2][x] == ' ')
                 {
                  map[y][x] = ' ';
                  y++;
                  map[y2][x] = 'O';   
                  }
              }     
            
         if(GetAsyncKeyState(VK_UP))
            {
             int y2 = y - 1;
             if(map[y2][x] == ' ')
               {
                map[y][x] = ' ';
                y--;
                map[y2][x] = 'O';   
               }   
            }
            
          if(GetAsyncKeyState(VK_RIGHT))
            {
             int x2 = x + 1;
             if(map[y][x2] == ' ')
               {
                map[y][x] = ' ';
                x++;
                map[y][x] = 'O';   
               }   
            }
            
          if(GetAsyncKeyState(VK_LEFT))
            {
             int x2 = x - 1;
             if(map[y][x2] == ' ')
               {
                map[y][x] = ' ';
                x--;
                map[y][x] = 'O';     
               }   
            } 
            
           if(GetAsyncKeyState(VK_ESCAPE))
             {
              game_running = false;       
             } 
                             
         }
         system("pause>nul"); 
         cout<<"GAME OVER"<<endl;
 
    return 0;   
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · August 15, 2015
http://stackoverflow.com/questions/9203362/c-color-text-in-terminal-applications-in-windows

good luck and have fun with the win api
+1 Franz Schmidt · August 16, 2015
What do you mean with "how to enter into a new level" ?
+1 Lachezar Peshterliev · August 17, 2015
Franz Scmidt I mean : That when you finish this level I want to star level 2 then when you finish level  2 start level 3 , But i cannot change the map :( and I want to set a different colors in the map 
+1 Franz Schmidt · August 17, 2015
Ok I understand your problem I would recommand to use the map matrix for the current activ map and do multiple other map matrixes as levels.

E.g:
char map[20][40] = {
....
}
char lvl_1[20][40] = {
...
}
and so on

And if you reach the target of lvl 1 you loop through the lvl_2 matrix and give every element to the map variable:
for(int i=0; i < 20; i++){
   for(int n=0; n<40; n++){
      map[.i][n]=lvl_2[.i][n];
   }
}

Hope you understand my solution:D

And for changing the color of the text cstudents solution is the best option
0 Lachezar Peshterliev · August 17, 2015
Franz  THANNK YOUUUU SOOO MUCH BROOO :)  
  • 1

C++

103,439 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator