From c2d0d1b15cb19940c9ef065ae21dc1fd78f73772 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 6 Mar 2019 20:59:45 -0500 Subject: change a lot of stuff and flesh out sdl ui --- frontend/ncurses/ncurses_ui.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'frontend/ncurses') diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c index d3d0e36..b1407f6 100644 --- a/frontend/ncurses/ncurses_ui.c +++ b/frontend/ncurses/ncurses_ui.c @@ -58,6 +58,8 @@ #define X_OFFSET 8 #define Y_OFFSET 0 +struct cetris_game game; + void curses_init() { setlocale(LC_CTYPE, ""); initscr(); @@ -78,29 +80,29 @@ void curses_init() { clear(); } -void draw_board(struct cetris_game* g) { +void draw_board() { mvaddstr(0, 0, PLAY_FIELD_STR); for (int x = 0; x < BOARD_X; x++) { for (int y = BOARD_VISABLE; 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) { + if (game.board[x][y].occupied) { + attron(COLOR_PAIR(game.board[x][y].c)); + if (game.board[x][y].remove_tick > 0) { + if (game.tick % 2 == 0) { mvaddstr((y - BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK); } } else { mvaddstr((y - BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK); } - attroff(COLOR_PAIR(g->board[x][y].c)); + attroff(COLOR_PAIR(game.board[x][y].c)); } } - int index = g->current_index; - attron(COLOR_PAIR(g->piece_queue[index].c)); + int index = game.current_index; + attron(COLOR_PAIR(game.piece_queue[index].c)); for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { - if (g->piece_queue[index].mat[y][x]) { - if (g->piece_queue[index].t == I) { + if (game.piece_queue[index].mat[y][x]) { + if (game.piece_queue[index].t == I) { mvaddstr(6 + y, (x * 2) + 36, BLOCK); } else { mvaddstr(6 + y, (x * 2) + 37, BLOCK); @@ -108,19 +110,24 @@ void draw_board(struct cetris_game* g) { } } } - attroff(COLOR_PAIR(g->piece_queue[index].c)); + attroff(COLOR_PAIR(game.piece_queue[index].c)); attron(A_BOLD); char score[50]; - sprintf(score, "%li", g->score); + sprintf(score, "%li", game.score); mvaddstr(1, (39 + X_OFFSET) - strlen(score), score); char level[20]; - sprintf(level, "%i", g->level); + sprintf(level, "%i", game.level); mvaddstr(3, 37, "Level"); mvaddstr(4, 40 - strlen(level), level); + if (game.game_over) { + mvaddstr(10, 6 + X_OFFSET, "GAME OVER"); + mvaddstr(11, 5 + X_OFFSET, "r to restart"); + } + attroff(A_BOLD); } } @@ -128,7 +135,6 @@ void draw_board(struct cetris_game* g) { int main(void) { curses_init(); - struct cetris_game game; init_game(&game); int c; @@ -145,9 +151,15 @@ int main(void) { case KEY_UP: rotate_clockwise(&game); break; case ' ': - move_hard_drop(&game); + move_hard_drop(&game); break; + case 'r': + if (game.game_over) { + init_game(&game); + } + break; } update_game_tick(&game); + erase(); draw_board(&game); refresh(); } -- cgit v1.2.3-101-g0448