I have written a Snake clone in C++ using the ncurses library. I have encountered a bug where at random the apple will not be printed to the screen. Here is the code:
void Screen::print_apple(const Point apple) {
wattron(this->m_game_screen, COLOR_PAIR(Paint::red));
mvwprintw(this->m_game_screen, apple.second, apple.first, "()");
wattroff(this->m_game_screen, COLOR_PAIR(Paint::red));
wrefresh(this->m_game_screen);
}
Pretty self-explanatory stuff. Paint is an enumerator defined here:
enum Paint : uint8_t {
red = 11, green, blue, black, yellow, gray
};
And finally, here are where the color attributes are initialized:
void Screen::start_paint() const {
const RGB background = {0, 150, 250};
const RGB snake = {0, 500, 0};
const RGB apple = {500, 0, 0};
const RGB congrats = {700, 700, 0};
const RGB highlight = {500, 500, 500};
start_color();
init_color(Paint::red, apple.r, apple.g, apple.b);
init_color(Paint::green, snake.r, snake.g, snake.b);
init_color(Paint::blue, background.r, background.g, background.b);
init_color(Paint::yellow, congrats.r, congrats.g, congrats.b);
init_color(Paint::gray, highlight.r, highlight.g, highlight.b);
init_pair(Paint::red, Paint::red, Paint::red);
init_pair(Paint::green, Paint::green, Paint::green);
init_pair(Paint::blue, COLOR_WHITE, Paint::blue);
init_pair(Paint::black, COLOR_WHITE, COLOR_BLACK);
init_pair(Paint::yellow, Paint::yellow, COLOR_BLACK);
init_pair(Paint::gray, Paint::gray, Paint::gray);
}
Now that all that is out of the way, let’s get to the meat of the problem. Here is what I know so far:
-
The print will fail at complete random. I can play the game up to 30 times with no issue.
-
When the apple fails to be displayed, the ‘()’ at the passed in Point is still loaded into the WINDOW’s memory. This was tested by calling mvwinch at the coordinates of the Point parameter to check for a ‘(‘ which would pass even when no apple was being displayed.
-
Once the bug begins, the only way to fix it in the game is to either hit the invisible apple, or pause and unpause the game. Resuming the game will call wclear and restore the snake and apple to the state they were in prior to the pause.
-
The game refreshes the WINDOW every 80 milliseconds even though the apple is loaded into memory of the WINDOW it will still not be displayed unless the previous two cases have occurred.
-
In print_apple neither wattron or mvwprintw return the ncurses ERR macro when the bug happens.
Here is some other information you might find useful:
- I am on macOS
- On zsh
- Using xterm-256color
- The rest of the source code is located at: https://github.com/mikeTwoTimes/snake
If you need anything else to diagnose what is going wrong, please let me know.
mikeTwoTimes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.