c_tetris

第 8 课:UI 界面 🎨

8.1 ncurses 初始化

void render_init(void) {
    initscr();              // 启动 ncurses
    cbreak();               // 禁用行缓冲
    noecho();               // 不显示输入
    keypad(stdscr, TRUE);   // 启用方向键
    curs_set(0);            // 隐藏光标
    
    start_color();
    init_pair(1, COLOR_CYAN, -1);    // I 方块
    init_pair(2, COLOR_YELLOW, -1);  // O 方块
}

8.2 绘制方块

void render_block(int x, int y, int color) {
    attron(COLOR_PAIR(color));
    mvaddch(y, x * 2, ACS_CKBOARD);
    mvaddch(y, x * 2 + 1, ACS_CKBOARD);
    attroff(COLOR_PAIR(color));
}

8.3 界面布局

┌────────────────────┐  NEXT:    HOLD:
│                    │  ┌──┐     ┌──┐
│    ██████          │  │██│     │██│
│      ██            │  └──┘     └──┘
└────────────────────┘  SCORE:   LEVEL:

✅ 本课检查清单


下一课:调试技巧