如何用C ++制作迷宫中的可移动角色

时间:2015-04-29 02:20:34

标签: c++ user-controls

一旦用户按下1以便启动地图和其他变量,我就会打印出迷宫。我已经宣布玩家是一个笑脸或设置为等于1.然而,我想让这个角色在整个迷宫中移动,但我很难这样做。我创建了一个玩家移动作为空间,以便它移动而不会覆盖其他代码。我正在考虑使用switch语句并检测用户输入,然后根据它改变X和Y位置,但我不知道如何将它放在我的mapCreation函数中。我也想知道你们是否可以帮助我在整个地图中随机位置生成诸如宝藏之类的变量,如果它是墙或“|”然后不要打印。我的宝藏在同一个地方产卵。任何关于我的任何问题的输入都会有所帮助!谢谢!

**忽略之前刚刚使用的情况,但现在我没有用它们。

**更新 如果有人在某个地方有一个链接,那将有所帮助!谢谢!

// Header Files
#include <cstdlib>
#include <iostream>
#include <windows.h>


using namespace std; 

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()


#define LEVEL_COUNT (2)
const char* maze[LEVEL_COUNT][12] = 
{
        {
            "||||||||||||\n",
            "|       |  |\n",
            "|       |  |\n",
            "|    ||||  |",
            "|          |",
            "|          |",
            "||||||     |",
            "| |        |",
            "| |    |   |",
            "|      |   |",
            "|      |   |",
            "||||||||||||",
        },
       {
            "||||||||||||",
            "|       |  |",
            "|   |||||  |",
            "|       |  |",
            "|          |",
            "|       ||||",
            "|          |",
            "|   |      |",
            "|   |      |",
            "|||||||    |",
            "|          |",
            "||||||||||||",
        }, 
};

// Function Prototypes

void titleScreen(); // Prints Title and instructions

void mapCreation( char arr[][12], int level);

void drawMap(char arr[][12]);

bool update(char arr[][12], int &level, int &lives, int &score);

void loadMap( char arr[][12], int level);

// Player Struct
struct Player{
    int x;
    int y;
    char move;
};



// Main Program
int main ()
    {
    // initialize variables
    int option;
    char baseMap[12][12];
    int level = 1;
    int lives = 3;
    int score = 0;
    bool gameOver = false;
    bool levelCompleted = false;


    SetConsoleTextAttribute(console, 240); // change background to white

    system("CLS");// clears screen in order to remove black background

    titleScreen(); // Display Title

    do // do-while loop starts
    {

    cin >> option; // take in input

        if(option == 1) // temporary option to check for next screen
            {
            //Display Maze

            system("CLS");// clears screen in order to remove black background

                 mapCreation( baseMap, 1 ); // Create map, this one is map 1

                 drawMap(baseMap); // iterate throup the map and print it out
                    //   update(baseMap, level, lives, score);



             }
          if(option == 2)
            {
            //Display Maze

            system("CLS");// clears screen in order to remove black background

                 mapCreation( baseMap, 2 ); // Create map, this one is map 1

                 drawMap(baseMap); // iterate throup the map and print it out
                    //   update(baseMap, level, lives, score);
             }

        }
    while( option !=1); // condition of do-while loop

    system("pause"); // Pause for user, only temporary

    return 0;

}








void titleScreen(){

    cout << " Welcome to Treasure Hunter!\n\n";
    cout << "In order to beat this game you must find the treasure\n";
    cout << " that is located in the maze. You can move using the \n";
    cout << " arrow keys or WASD.\n\n";
    cout << " Warning! There are traps that will take life away as\n";
    cout << " well as add life! However, they are hidden so be careful!\n ";
    cout << " Goodluck and have fun!\n\n\n\n";
    cout << " Press Ctrl+C to quit\n";


}


void mapCreation( char arr[12][12], int level )
    {
      int traps = 0;   
      int lives = 0;
      int treasure = 0;
      int x ;
      int y ;
        for(int i = 0; i < 12; i++)
            {
             for(int j = 0; j < 12; j++)
                {
                 arr[i][j] = 0;
                }

            }

        // load the map:
        loadMap(arr,level);

        switch (level)
        {
        case 1:




        break;

        case 2: // Level 2 Map

        break;
        }
    }

void drawMap(char arr[12][12])
    {
        // Declare variables to be prtined out
      Player player;
      player.x = 1;
      player.y = 1;
      player.move = ' ';
      int traps = 0;   
      int lives = 0;
      int treasure = 0;
      // Lives,Traps, and Treasure x and y positions
      int Trap_Y,Trap_X,Lives_X,Lives_Y,Traps_X,Traps_Y; 
      // Randomly place variables
      Trap_X = (rand() % 10);
      Trap_Y = (rand() % 10);  
      Lives_X = (rand() % 12);
      Lives_Y = (rand() % 12);
      Traps_X = (rand() % 12);
      Traps_Y = (rand() % 12);    

     for(int i = 0; i < 12; i++)
        {
        cout << endl; // print out new line
         for(int j = 0; j < 12; j++ )
            {
                if(player.x == i && player.y == j)
                arr[player.x][player.y] = 1;                        




                // If variables are  not equal to a wall character print it out
                 if(arr[Trap_X][Trap_Y] != '|' )                         
                arr[Trap_X][Trap_Y] = 4; // treasure is 4
                treasure++;
                if(arr[Lives_X][Lives_Y] != '|')// traps are 2
                 arr[Lives_X][Lives_Y]= 2; 
                 traps++;
                if(arr[Traps_X][Traps_Y] != '|') // lives are 3
                 arr[Traps_X][Traps_Y]= 3;
                 lives++;


                cout << arr[i][j];

            }
        }
        cout << endl;
    }

/*bool update(char arr[][12], int &level, int &lives, int &score)
    {
     bool levelCompleted = false;
     bool gameOver = false;
    }
*/


void loadMap( char arr[][12], int level)
{
     if((level < 0) || (level >= LEVEL_COUNT))
         return;

     for(int i = 0; i < 12; i++)
     {
         const char* row = maze[level][i];
         for(int j = 0; j < 12; j++)
         {
             if(row[j] == 0)
                 break; // end of string
             if(row[j] == ' ')
                 arr[i][j] = 0;  // set spaces to zero
             else
                 arr[i][j] = row[j];
         }
     }
}

1 个答案:

答案 0 :(得分:1)

在Windows上,您可以使用ReadConsoleInput()在Windows控制台中使用键盘进行导航。此解决方案使用语句if (maze[player.y][player.x-1]==0)检查冲突。 0表示没有墙,1表示墙已被击中。

以下是ReadConsoleInput()的使用示例。

enter image description here

#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;

#define VK_W 0x57
#define VK_S 0x53
#define VK_A 0x41
#define VK_D 0x44


struct t_player{ int x,y;};
t_player player;
char playersymbol=219;
char mazewall=0;
int mazex=31,mazey=8;

int maze[16][24] =
{{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 { 1,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 { 1,0,0,0,0,1,0,0,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,1},
 { 1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,1},
 { 1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,1},
 { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1},
 { 1,0,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
 { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1},
 { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
 { 1,0,0,0,1,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1},
 { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
 { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};


void clrscr();
void gotoxy(int x, int y);
void setcolor(WORD color);
void textColor(unsigned char fColor,unsigned char bColor);
void moveleft();
void moveright();
void moveup();
void movedown();
void drawmaze(int px,int py);
void putmenu();
void putplayer();

int main()
{
    DWORD        mode;          /* Preserved console mode */
    INPUT_RECORD event;         /* Input event */
    BOOL         EXITGAME = FALSE;  /* Program termination flag */
    unsigned int counter = 0;   /* The number of times 'Esc' is pressed */


    /* Get the console input handle */
    HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );

    /* Preserve the original console mode */
    GetConsoleMode( hstdin, &mode );

    /* Set to no line-buffering, no echo, no special-key-processing */
    SetConsoleMode( hstdin, 0 );


    player.x=20;
    player.y=13;

    clrscr();
    setcolor(15);


    while (!EXITGAME)
    {
        if (WaitForSingleObject( hstdin, 0 ) == WAIT_OBJECT_0)  /* if kbhit */
        {
            DWORD count;  /* ignored */

            /* Get the input event */
            ReadConsoleInput( hstdin, &event, 1, &count );

            /* Only respond to key release events */
            if ((event.EventType == KEY_EVENT)
            &&  !event.Event.KeyEvent.bKeyDown)

            clrscr();
            putmenu();
            Sleep(100);


                switch (event.Event.KeyEvent.wVirtualKeyCode)
                {
                    case VK_ESCAPE:
                       EXITGAME = TRUE;
                     break;

                    case VK_LEFT:
                        // left key   move player left
                         moveleft();
                     break;

                    case VK_RIGHT:
                        // right key   move player right
                        moveright();

                     break;    

                    case VK_UP:
                        // up key   move player up
                        moveup();

                     break;

                    case VK_DOWN:
                        // down key   move player down
                         movedown();

                     break; 

                    case VK_A:
                        // left key   move player left
                        moveleft();

                     break; 

                    case VK_D:
                        // right key   move player right

                        moveright();
                     break; 

                    case VK_W:
                        // up key   move player up

                        moveup();
                     break; 

                    case VK_S:
                        // down key   move player down
                        movedown();
                     break; 


                }//switch

                putplayer();


        }
    }

    gotoxy(1,23);cout<<"  ";  
    SetConsoleMode( hstdin, mode );
    return 0;
}

void putplayer()
{
    setcolor(9);
    gotoxy( player.x +mazex,    player.y  +mazey); 
    cout<<playersymbol;
    setcolor(7);

}


void putmenu()
{
    gotoxy(1,1);cout<<"keyboard navigator ";

    setcolor(14);
    gotoxy(31,1);cout<<"Use keys W,S, A,D, Left,Right,Up,Down ";

    setcolor(7);

    gotoxy(31,2);cout<<"W or up key = move player up ";
    gotoxy(31,3);cout<<"S or down key = move player down ";
    gotoxy(31,4);cout<<"A or left key = move player left ";
    gotoxy(31,5);cout<<"D or right key = move player right ";

    setcolor(11);

    drawmaze(mazex,mazey);
    setcolor(7);

}


void drawmaze(int px,int py)
{
    for(int y =0; y<16;y++)
    {
        for(int x=0; x<24; x++)
        {
           if (maze[y][x]==1) mazewall=219;
           else mazewall=32;

           gotoxy(x+px,y+py);
           cout<< mazewall;
        }

    }

}

void moveleft()
{
    gotoxy(31,7); 
    cout<<"left key   move player left    \n\n";
     if (maze[player.y][player.x-1]==0)   player.x = player.x -1;
}

void moveright()
{
    gotoxy(31,7); 
    cout<<"right key   move player right   \n\n";
     if (maze[player.y][player.x+1]==0)   player.x = player.x +1;
}

void moveup()
{
    gotoxy(31,7); 
    cout<<"up key   move player up      \n\n";
     if (maze[player.y-1][player.x]==0)   player.y = player.y -1;
}

void movedown()
{
    gotoxy(31,7); 
    cout<<"down key   move player down   \n\n";
     if (maze[player.y+1][player.x]==0)   player.y = player.y +1;

}



void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}

void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}




void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
{
   int color=16*BackGroundColor+ForeGroundColor;
   setcolor(color);
}



void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    return;
}