Inconsistency with mvwprintw in ncurses

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.

New contributor

mikeTwoTimes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật