尝试着创造一款原始的主机蛇游戏.0警告/论述.为什么我的程序崩溃了?(c++)

Trying to make a primitive console snake game. 0 warnings/erros. Why does my program crash? (C++)

本文关键字:我的 为什么 论述 警告 程序 c++ 崩溃 创造 一款 主机 原始      更新时间:2023-10-16

感谢您的阅读。我是一个开始的程序员,我试图用c++做一个简单的蛇游戏。它还没有完成,但我想我有一个很好的开始。然而,当我试图运行该程序时,它立即崩溃。(编译器说没有警告和错误。我正在使用Code::Blocks IDE。有人知道为什么我的程序不工作吗?我认为这可能与"矢量坐标历史"有关,但我不能确定。至少这是我在程序中添加的最后一件事。

这是我的代码:

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cstdio>
    #include <windows.h>
    #include <conio.h>
    #include <vector>
    #define MAXX 156 //Number of columns that fits on my screen
    #define MAXY 62 //Number of rows that fits on my screen
    using namespace std;
    // This function clears the console window
    void clearConsole()
    {
        system("cls"); //empties console window
    };
    // This function returns the x position of the cursor
    int getcursorX()
    {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
            return csbi.dwCursorPosition.X;
        }
    };
    // This function returns the y position of the cursor
    int getcursorY()
    {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
            return csbi.dwCursorPosition.Y;
        }
    };
    // This function sets the x position of the cursor
    void setcursorX(int x)
    {
        COORD coord = {x, getcursorY()};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    // This function sets the y position of the cursor
    void setcursorY(int y)
    {
        COORD coord = {getcursorX(), y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    // The snake class contains the coordinates of the snake and direction         in which it is moving
    class Snake
    {
    private:
        bool isAlive;
        int snakexCoord;
        int snakeyCoord;
        char snakeDirection;
        int snakeLength;
    public:
        //getters
        int getsnakexCoord() { return snakexCoord; };
        int getsnakeyCoord() { return snakeyCoord; };
        char getsnakeDirection() { return snakeDirection; };
        bool getisAlive() { return isAlive; };
        int getsnakeLength() { return snakeLength; };
        //setters
        void setsnakexCoord(int newsnakexCoord) { snakexCoord = newsnakexCoord;};
        void setsnakeyCoord(int newsnakeyCoord) { snakeyCoord = newsnakeyCoord;};
        void setsnakeDirection(char newsnakeDirection) { snakeDirection = newsnakeDirection;};
        void setisAlive(bool newisAlive) { isAlive = newisAlive; };
        void setsnakeLength(int newsnakeLength) { snakeLength = newsnakeLength; };
        //constructor
        Snake()
        {
            snakexCoord = MAXX / 2;
            snakeyCoord = MAXY / 2;
            snakeDirection = 'E';
            isAlive = true;
            snakeLength = 1;
        };
        //destructor
        ~Snake(){};
    };
    int main()
    {
        int i; //iterator
        system("mode 650"); //makes console window full-screen
        Snake snake; //initializes Snake object snake
        char c; //char that stores user input to change snake direction
        vector<int[2]> coordHistory; //vector of arrays that stores previous         locations of snake
        while (snake.getisAlive())
        {
            //Adds snake coordinates to coordHistory
            coordHistory[coordHistory.size()][0] = snake.getsnakexCoord();
            coordHistory[coordHistory.size()-1][1] = snake.getsnakeyCoord();
            //Iterates backwards through coordHistory and draws an "O" until the snake is as long as it should be
            for(i = coordHistory.size() - 1; i > coordHistory.size() - 1 - snake.getsnakeLength(); i--)
            {
                setcursorX(coordHistory[i][0]);
                setcursorY(coordHistory[i][1]);
                cout << "O";
            }
            //Allows user to change snake direction
            c = _getch();
            switch (c){
            case 'w':
                snake.setsnakeDirection('N');
                break;
            case 'd':
                snake.setsnakeDirection('E');
                break;
            case 's':
                snake.setsnakeDirection('S');
                break;
            case 'a':
                snake.setsnakeDirection('W');
                break;
            }
            //Checks in which direction snake is going and changes coordinates accordingly
            switch (snake.getsnakeDirection())
            {
            case 'N':
                snake.setsnakeyCoord(snake.getsnakeyCoord()-1);
                break;
            case 'E':
                snake.setsnakexCoord(snake.getsnakexCoord()+1);
                break;
            case 'S':
                snake.setsnakeyCoord(snake.getsnakeyCoord()+1);
                break;
            case 'W':
                snake.setsnakexCoord(snake.getsnakexCoord()-1);
                break;
            }
            //Checks if snake goes out of boundaries
            if ((snake.getsnakexCoord() > MAXX) || (snake.getsnakexCoord() < 0) || (snake.getsnakeyCoord() > MAXY) || (snake.getsnakeyCoord() < 0))
            {
                snake.setisAlive(false);
            }
            //Sleep(200); Ignore WIP
            clearConsole();
        }
        return 0;
    }

你的vector用法是错误的。

coordHistory[coordHistory.size()][0] = snake.getsnakexCoord();
coordHistory[coordHistory.size()-1][1] = snake.getsnakeyCoord();

您似乎假设vector[n-1]将自动创建尽可能多的元素,以产生大小为n的向量。

你访问了两次向量coordHistory的第一个元素,这个向量实际上是空的。

要添加元素,一般使用push_backemplace_back成员函数。但是,您确实会遇到数组向量的问题,因为数组既不可赋值也不可复制。这就是为什么我不能在这里使用push_back;我必须显式地调整vector的大小,然后像之前一样访问新创建的元素:

coordHistory.resize(1);
coordHistory[0][0] = snake.getsnakexCoord();
coordHistory[0][1] = snake.getsnakeyCoord();

这很尴尬。为什么不使用xy成员存储一个漂亮的类类型呢?

std::vector<deliciousClassType> coordHistory;
coordHistory.emplace_back(
   snake.getsnakexCoord(),
   snake.getsnakeyCoord()
);