From 7c737f4d1d21a8c666bc8b868f4f29cfe9a3cbab Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 4 Mar 2019 17:28:05 -0500 Subject: restructure project and add tspin checking --- frontend/ncurses/ncurses_ui.c | 101 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 frontend/ncurses/ncurses_ui.c (limited to 'frontend/ncurses') diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c new file mode 100644 index 0000000..1ae07b2 --- /dev/null +++ b/frontend/ncurses/ncurses_ui.c @@ -0,0 +1,101 @@ +#include +#include +#include + +#include "cetris.h" + +#define BLOCK "[]" +#define PLAY_FIELD_STR " /--------------------\\ /--------\\\n"\ + " | | | |\n"\ + " | | \\--------/\n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " | | \n"\ + " \\--------------------/" + +#define X_OFFSET 8 +#define Y_OFFSET 0 + +void curses_init() { + setlocale(LC_CTYPE, ""); + initscr(); + noecho(); + keypad(stdscr, TRUE); + curs_set(0); + timeout(1000 / CETRIS_HZ); + + start_color(); + init_pair(COLOR_NONE, COLOR_BLACK, COLOR_BLACK); + init_pair(COLOR_O, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_Z, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_S, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_T, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_L, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_I, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_J, COLOR_YELLOW, COLOR_BLACK); + clear(); +} + +void draw_board(struct cetris_game* g) { + mvaddstr(0, 0, PLAY_FIELD_STR); + for (int x = 0; x < BOARD_X; x++) { + for (int y = 0; y < BOARD_Y; y++) { + if (g->board[x][y].occupied) { + attron(COLOR_PAIR(g->board[x][y].c)); + if (g->board[x][y].remove_tick > 0) { + if (g->tick % 2 == 0) { + mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); + } + } else { + mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); + } + attroff(COLOR_PAIR(g->board[x][y].c)); + } + } + } +} + +int main(void) { + curses_init(); + + struct cetris_game game; + init_game(&game); + + int c; + while(1) { + c = getch(); + switch (c) { + case 'q': endwin(); exit(1); + case KEY_LEFT: + move_left(&game); break; + case KEY_RIGHT: + move_right(&game); break; + case KEY_DOWN: + move_down(&game); break; + case KEY_UP: + rotate_clockwise(&game); break; + case ' ': + move_hard_drop(&game); + } + update_game_tick(&game); + draw_board(&game); + refresh(); + } + return 0; +} + -- cgit v1.2.3-101-g0448