简体   繁体   English

Snake游戏中drawGame函数的问题

[英]Problem with drawGame function in a Snake Game

I'm programming a Snake Game in C++ in order to learn more about C ++. 我正在用C ++编写Snake Game,以了解有关C ++的更多信息。 I made the game using the paradigms of object-oriented programming, but the design of the drawGame function is not working properly. 我使用面向对象编程的范例制作了游戏,但drawGame函数的设计无法正常工作。

Testing the drawGame function, I'm getting this as result: 测试drawGame函数,结果如下:

在此处输入图片说明

void Game::drawGame(int fruitxpos, int fruitypos, std::vector<int>& xposition, std::vector<int>& yposition, int snakesize){
    system("cls");
    int printedflag = 0;
    for(int j = 1; j <= ysize; j++){
        if(j == 1 || j == ysize){
            for(int i = 1; i <= xsize; i++){
                std::cout << "#";
            }
            std::cout << "\n";
        }       
        else{
            for(int i = 1; i <= xsize; i++){
                if(i == 1 || i == xsize){
                    std::cout << "#";
                }
                else{
                    for(int n = 0; n <= snakesize; n++){
                        if(i == xposition[n] && j == yposition[n]){
                            std::cout << "o";
                            printedflag = 1;
                        }
                        else{
                            printedflag = 0; 
                        }
                    }
                    if(!printedflag){
                        if(i == fruitxpos && j == fruitypos){
                            std::cout << "F";
                        }
                        else{
                            std::cout << " ";
                        }
                    }   
                }
            }
            std::cout << "\n";
        }
    }
}

As you can see, It is printing a blank space after every snake block. 如您所见,它在每个蛇形块之后打印一个空白区域。 Could someone explain me what is wrong? 有人可以解释给我什么事吗?

IMHO, your program would be better by using a 2d matrix of characters. 恕我直言,您的程序将使用2D字符矩阵会更好。 Your main program will write into the matrix. 您的主程序将写入矩阵。 A print function would print the matrix. 打印功能将打印矩阵。 This eliminates the worry of having to use X,Y positioning on the console. 这样就不必担心必须在控制台上使用X,Y定位。

If you design the matrix as a {contiguous} array of characters, you can add an extra column for the newline character. 如果将矩阵设计为字符的{连续}数组,则可以为换行符添加额外的列。 The last cell of the matrix would be the nul character, '\\0'. 矩阵的最后一个单元格为nul字符“ \\ 0”。 This allows you to print the matrix as if it was one long C-Style string. 这使您可以像打印一个长C样式字符串一样打印矩阵。

Some example code: 一些示例代码:

const unsigned int MAX_COLUMNS = 20U + 1U; // +1 column for newline
const unsigned int MAX_ROWS    = 20U;
char game_board[MAX_ROWS][MAX_COLUMNS];

void Clear_Board()
{
    // Fill the board with spaces (blanks).
    memset((char *) &game_board[0][0], ' ', sizeof(game_board));

    // Draw the top and bottom borders
    for (unsigned int column = 0; column < MAX_COLUMNS; ++column)
    {
        game_board[0][column] = '#';
        game_board[MAX_ROWS - 1][column] = '#';
    }

    // Draw the side borders
    const unsigned int LEFT_COLUMN = 0U;
    const unsigned int RIGHT_COLUMN = MAX_COLUMNS - 2U;
    const unsigned int NEWLINE_COLUMN = MAX_COLUMNS - 1U;
    for (unsigned int row = 0; row < MAX_ROWS; ++row)
    {
        game_board[row][LEFT_COLUMN] = '#';
        game_board[row][RIGHT_COLUMN] = '#';
        game_board[row][NEWLINE_COLUMN] = '\n';
    }

    // Set the terminating nul character
    game_board[MAX_ROWS - 1][MAX_COLUMNS - 1] = '\0';
}

Printing the board: 印制板:

std::cout << game_board;

or 要么

std::cout.write(&game_board[0][0], sizeof(game_board) - 1U); // Don't print terminating nul.

Checking for fruit encounter 检查是否遇到水果

unsigned int snake_head_row = 10U;    // Example position.
unsigned int snake_head_column = 5u;  
const char FRUIT_CHAR = 'F';
//...
if (game_board[snake_head_row][snake_head_column] == FRUIT_CHAR)
{
  //...
}

Notice that the fruit encounter doesn't need printing. 请注意,遇到的水果不需要打印。

IMHO, you should have the snake as a container of coordinates (row, column). 恕我直言,您应该将蛇作为坐标(行,列)的容器。 Each body cell is an item in the container. 每个人体细胞都是容器中的一个物品。 If the snake grows, append a coordinate to the container. 如果蛇长大,请将坐标附加到容器中。 Drawing the snake involves traversing the container, placing snake characters at the appropriate game_board positions (then drawing the board). 绘制蛇涉及遍历容器,将蛇角色放置在适当的game_board位置(然后绘制棋盘)。

A game board helps remember the position of the snake body and any other items on the board. 游戏板可帮助记住蛇体和板上其他任何物品的位置。 You can use a console positioning library and put the characters at their coordinates, too. 您可以使用控制台定位库,也可以将字符放在其坐标处。

Suggesting to split your Game::drawGame into sub-functions. 建议将Game::drawGame为子功能。 Also, probably the method should not get all the parameters you pass to it as they all should better be class members. 同样,该方法可能不应该获取传递给它的所有参数,因为它们最好都是类成员。

void Game::drawGame() {
    drawBoard();
    drawSnake();
    drawFruits();
}

Then you may want to use gotoxy that can be found on the web, eg here , eg for windows: 然后,你可能需要使用gotoxy可以在网上找到,例如在这里 ,例如,用于窗口:

void gotoxy(int x, int y) {
    HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE);  // get console handle 
    COORD cursor = { x, y };
    SetConsoleCursorPosition(hc, cursor);  // set new cursor position
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM