首页 > 解决方案 > How can I refresh the console in the most efficient way? (snake game c++)

问题描述

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;
}

标签: c++winapiconsole

解决方案


系统(“cls”)很慢。此外,您不需要刷新整个屏幕,因为其中大部分不会改变每一帧。我看到你已经包含 windows.h 所以我猜你只需要它在 Windows 上工作。因此,我建议使用 Windows API 中的函数SetConsoleCursorPosition

这是一个例子

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

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


推荐阅读