From 36653b27cb7664c67f8455254d3d9507f474aef9 Mon Sep 17 00:00:00 2001 From: tigertv Date: Thu, 30 Jan 2020 20:27:11 +0200 Subject: [PATCH] Add colors --- Game.cpp | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/Game.cpp b/Game.cpp index 8af7722..9baaf52 100644 --- a/Game.cpp +++ b/Game.cpp @@ -2,6 +2,11 @@ #include #include +#define SNAKE_HEAD_PAIR 1 +#define SNAKE_BODY_PAIR 2 +#define WALL_PAIR 3 +#define FOOD_PAIR 4 + Game::Game() : foodBuilder(&borderFrame), borderFrame(0, 0, 64, 32) { } @@ -46,8 +51,6 @@ void Game::init() { noecho(); // Wont print the keys received through input nodelay(stdscr, TRUE); // Wont wait for input keypad(stdscr, TRUE); // Support for extra keys (life F1, F2, ... ) - //timeout(400); - //timeout(0); // Ncurses' global variable meaning number of milliseconds // to wait after the user presses ESC. @@ -58,11 +61,12 @@ void Game::init() { refresh(); // Refresh the layout + start_color(); + init_pair(SNAKE_HEAD_PAIR, COLOR_YELLOW, COLOR_BLACK); + init_pair(WALL_PAIR, COLOR_CYAN, COLOR_MAGENTA); + init_pair(SNAKE_BODY_PAIR, COLOR_GREEN, COLOR_BLACK); + init_pair(FOOD_PAIR, COLOR_RED, COLOR_BLACK); - //int max_y = 0, max_x = 0; - - // Global var `stdscr` is created by the call to `initscr()` - //getmaxyx(stdscr, max_y, max_x); } void Game::processInput() { @@ -145,25 +149,36 @@ void Game::render() { // render snake std::list> *coords = snake.getCoordinates(); std::list>::iterator it = coords->begin(); + // render head - mvprintw(it->second, it->first, "@"); + attron(COLOR_PAIR(SNAKE_HEAD_PAIR)); + mvaddch(it->second, it->first, '@'); + attroff(COLOR_PAIR(SNAKE_HEAD_PAIR)); + // render body + attron(COLOR_PAIR(SNAKE_BODY_PAIR)); for(it++; it != coords->end(); it++) { - mvprintw(it->second, it->first, "o"); + //mvprintw(it->second, it->first, "o"); + mvaddch(it->second, it->first, 'o'); } + attroff(COLOR_PAIR(SNAKE_BODY_PAIR)); // render boarders + attron(COLOR_PAIR(WALL_PAIR)); for(int x = borderFrame.x; x <= borderFrame.width; x++) { - mvprintw(borderFrame.y, x, "x"); - mvprintw(borderFrame.height, x, "x"); + mvaddch(borderFrame.y, x, 'x'); + mvaddch(borderFrame.height, x, 'x'); } for(int y = borderFrame.y; y <= borderFrame.height; y++) { - mvprintw(y, borderFrame.x, "x"); - mvprintw(y, borderFrame.width, "x"); + mvaddch(y, borderFrame.x, 'x'); + mvaddch(y, borderFrame.width, 'x'); } - + attroff(COLOR_PAIR(WALL_PAIR)); + // render food - mvprintw(this->food->y, this->food->x, "F"); + attron(COLOR_PAIR(FOOD_PAIR)); + mvaddch(this->food->y, this->food->x, 'F'); + attroff(COLOR_PAIR(FOOD_PAIR)); } int Game::getScore() {