How can I refresh the console in the most efficient way? (snake game c++)

时间:2018-11-16 21:38:40

标签: c++ winapi console

This is my code for snake. system("cls") is not efficient at all, the console flickers...

#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

bool status = false, win = false;

struct Snake {
    int index_i;
    int index_j;
};

class Game {
private:
    enum eDir { UP, RIGHT, DOWN, LEFT };
    eDir direction;
    const int height = 25, width = 50, max_size = (height - 2)*(width - 2);
    int snake_size = 1, food_x, food_y, snake_x, snake_y, score, speed;
    char snake = '@', food = '*', frame = '#';
    Snake *snake_body = new Snake[max_size];
public:
    Game() {
        snake_x = height / 2;
        snake_y = width / 2;
        snake_body[0].index_i = snake_x;
        snake_body[0].index_j = snake_y;
        PutFood();
    }
    ~Game() {
        delete[] snake_body;
    }
    void DrawTable() {
        system("cls");
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (!i || i == height - 1 || !j || j == width - 1) {
                    cout << frame;
                }
                else if (i == food_x && j == food_y) {
                    cout << food;
                }
                else if (Check(i, j)) {
                    cout << snake;
                }
                else {
                    cout << " ";
                }
            }
            cout << endl;
        }
        cout << "Your current score is: " << score;
    }
    void Control() {
        if (_kbhit()) {
            switch (_getch()) {
            case 'w':
                direction = UP;
                break;
            case 'a':
                direction = LEFT;
                break;
            case 's':
                direction = DOWN;
                break;
            case 'd':
                direction = RIGHT;
                break;
            }
        }
    }
    void Process() {
        switch (direction) {
        case UP:
            snake_x--;
            Move();
            break;
        case LEFT:
            snake_y--;
            Move();
            break;
        case DOWN:
            snake_x++;
            Move();
            break;
        case RIGHT:
            snake_y++;
            Move();
            break;
        }
    }
    void Move() {
        /*for (int i = 0; i < snake_size; i++) {   tail collision logic (if you try to reverse your move, you die). Optional.
            if (snake_body[i].index_i == snake_x && snake_body[i].index_j == snake_y) {
                status = true;
                return;
            }
        }*/
        snake_body[snake_size].index_i = snake_x;
        snake_body[snake_size].index_j = snake_y;
        if (!snake_x || snake_x == height - 1 || !snake_y || snake_y == width - 1) { // collision logic
            status = true;
        }
        else if (snake_x == food_x && snake_y == food_y) {
            snake_size++;
            score++;
            if (snake_size == max_size) {
                win = true;
                return;
            }
            PutFood();
        }
        else {
            for (int index = 0; index < snake_size; index++) {
                snake_body[index].index_i = snake_body[index + 1].index_i;
                snake_body[index].index_j = snake_body[index + 1].index_j;
            }
            snake_body[snake_size].index_i = 0;
            snake_body[snake_size].index_j = 0;
        }
        Sleep(speed);
    }
    void PutFood() {
        srand(time(NULL));
        food_x = rand() % (height - 2) + 2;
        food_y = rand() % (width - 2) + 2;
    }
    bool Check(int i, int j) {
        for (int k = 0; k < snake_size; k++) {
            if (i == snake_body[k].index_i && j == snake_body[k].index_j) {
                return true;
            }
        }
        return false;
    }
    int getScore() {
        return score;
    }
    void setSpeed(int s) {
        speed = s;
    }
};

int main() {
    Game snake_game;
    char exit;
    string error = "Invalid choice, please choose 1-3";
    int speed, choice;
    cout << "Contol: WASD" << endl << "Set the difficulty level: " << endl << "1. Easy" << endl << "2. Normal" << endl << "3. Hard" << endl;
label:
    cin >> choice;
    try {
        if (choice < 1 || choice > 3) throw error;
    }
    catch (char *error) {
        cout << error << endl;
        goto label;
    }
    switch (choice) {
    case 1:
        speed = 250;
        break;
    case 2:
        speed = 75;
        break;
    case 3:
        speed = 0;
        break;
    }
    snake_game.setSpeed(speed);
    while (!status && !win) {
        snake_game.DrawTable();
        snake_game.Control();
        snake_game.Process();
    }
    if (status && !win) {
        system("cls");
        cout << "YOU LOST! Your score is: " << snake_game.getScore() << endl;
    }
    if (win) {
        system("cls");
        cout << "Congratulations! You won the game!" << endl << "Your score is: " << snake_game.getScore() << endl;
    }
    cin >> exit;
    return 0;
}

5 个答案:

答案 0 :(得分:3)

system(“ cls”)速度很慢。另外,您无需刷新整个屏幕,因为大多数屏幕不会更改每一帧。我看到您已经包含windows.h,所以我猜您只需要在Windows上运行即可。因此,我建议使用Windows API中的函数Output

这是一个例子

   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {10, 10});
   std::cout << ' ';

此代码会将光标位置更改为坐标(10,10),并输出一个空格。 您可以为每个帧要更改的每个“像素”执行此操作。

答案 1 :(得分:2)

在Unix系统上, curses 是实现像您这样的基于文本的程序的经典方法:

  

使用诅咒,程序员能够编写基于文本的应用程序   无需直接编写任何特定的终端类型。诅咒   执行系统上的库发送正确的控制字符   根据终端类型。它提供一个或多个的抽象   映射到终端屏幕的窗口。每个窗口都代表   通过字符矩阵。程序员设置所需的外观   ,然后告诉curses程序包更新屏幕。   该库确定了所需的最少更改集   更新显示,然后使用终端的   具体功能和控制顺序。 [维基百科]

很显然,正在开发一个名为PDCurses的Windows端口。您可以查看它是否满足您的需求。

答案 2 :(得分:0)

system("cls")实际上是运行整个Windows程序(cmd.exe)来清除控制台。这当然不是很有效。取而代之的是,我们只需要执行相同的操作,即 cmd.exe 中的 cls 命令。为了获得清晰的屏幕,我们可以使用ScrollConsoleScreenBuffer-将控制台屏幕缓冲区的内容替换为空格

BOOL cls()
{
    HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
    {
        CHAR_INFO fi = { ' ', csbi.wAttributes };
        csbi.srWindow.Left = 0;
        csbi.srWindow.Top = 0;
        csbi.srWindow.Right = csbi.dwSize.X - 1;
        csbi.srWindow.Bottom = csbi.dwSize.Y - 1;
        return ScrollConsoleScreenBufferW(hConsoleOutput, &csbi.srWindow, 0, csbi.dwSize, &fi);
    }
    return FALSE;
}

答案 3 :(得分:0)

system("cls")的效率太低。这是清理屏幕的类似方法:

    //First get the console handle and its info.
    HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);

    //Fill with ' ' in the whole console(number = X*Y).
    FillConsoleOutputCharacter(hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, home, &dummy);
    csbiInfo.dwCursorPosition.X = 0;
    csbiInfo.dwCursorPosition.Y = 0;

    //Set the Cursor Position to the Beginning.
    SetConsoleCursorPosition(hConsoleOut, csbiInfo.dwCursorPosition);

答案 4 :(得分:-1)

As you are using conio.h, you can use gotoxy(x, y) to go to the coordinate, which you want to delete and just perform a printf(" ") with a whitespace.