Skip to content

Commit

Permalink
Add colors
Browse files Browse the repository at this point in the history
  • Loading branch information
tigertv committed Jan 30, 2020
1 parent 5a0d322 commit 36653b2
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <ncurses.h>
#include <unistd.h>

#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) {

}
Expand Down Expand Up @@ -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.
Expand All @@ -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() {
Expand Down Expand Up @@ -145,25 +149,36 @@ void Game::render() {
// render snake
std::list<std::pair<int, int>> *coords = snake.getCoordinates();
std::list<std::pair<int, int>>::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() {
Expand Down

0 comments on commit 36653b2

Please sign in to comment.