Skip to content

Instantly share code, notes, and snippets.

@crsaracco
Created December 25, 2014 00:27
Show Gist options
  • Save crsaracco/3fb2881e54cc3d98fb1b to your computer and use it in GitHub Desktop.
Save crsaracco/3fb2881e54cc3d98fb1b to your computer and use it in GitHub Desktop.
colors
#include <ncurses.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
void print_block(WINDOW *win, int x, int y);
void print_block_color(WINDOW *win, int x, int y, int color);
void print_board(WINDOW *win);
static sig_atomic_t resize;
int main()
{
srand(time(NULL));
int key;
WINDOW *win;
initscr();
win = newwin(10, 10, 0, 0);
wresize(win, LINES, COLS);
curs_set(0);
noecho();
{
start_color();
use_default_colors();
init_pair(1, COLOR_RED, -1); // Z blocks
init_pair(2, COLOR_GREEN, -1); // S blocks
init_pair(3, COLOR_YELLOW, -1); // O blocks
init_pair(4, COLOR_BLUE, -1); // J blocks
init_pair(5, COLOR_MAGENTA, -1); // T blocks
init_pair(6, COLOR_CYAN, -1); // I blocks
init_pair(7, COLOR_WHITE, -1); // L blocks
}
while (1)
{
if (resize)
{
endwin();
refresh();
wresize(win, LINES, COLS);
resize = 0;
}
//werase(win);
print_board(win);
print_block_color(win, rand()%10+2, rand()%20+1, COLOR_PAIR(rand()%7+1));
//print_block(win, rand()%10, rand()%10);
wnoutrefresh(win);
wrefresh(win);
doupdate();
usleep(2);
}
delwin(win);
endwin();
return 0;
}
void print_block(WINDOW *win, int x, int y)
{
mvwaddch(win, y, x*2, ACS_BLOCK);
mvwaddch(win, y, x*2+1, ACS_BLOCK);
}
void print_block_color(WINDOW *win, int x, int y, int color)
{
wattron(win, color);
mvwaddch(win, y, x*2, ACS_BLOCK);
mvwaddch(win, y, x*2+1, ACS_BLOCK);
wattroff(win, color);
}
void print_board(WINDOW *win)
{
int i;
for (i=1; i<21; i++)
{
mvwaddch(win, i, 3, ACS_VLINE);
mvwaddch(win, i, 24, ACS_VLINE);
}
mvwaddch(win, 21, 3, ACS_LLCORNER);
mvwaddch(win, 21, 24, ACS_LRCORNER);
for (i=4; i<24; i++)
{
mvwaddch(win, 21, i, ACS_HLINE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment