美文网首页Mac/Linux服务器
在shell中开发一个俄罗斯方块

在shell中开发一个俄罗斯方块

作者: 塘朗山小钻风 | 来源:发表于2021-01-08 16:21 被阅读0次

效果图

shell中的俄罗斯方块


实现的技术要点

每帧显示需要清空上传显示状态,使用printf("\033c\n");语句就可以做到

游戏中都是需要一个循环机制的,使用usleep设置好毫秒数就可以指定每秒多少个逻辑循环

while(key != 'q' && key != 'Q'){

if (epoll_wait(epfd,events,1,0) > 0){//等待事件发生

key = getchar();

if (27 == key && getchar() == 91)key = getchar();// 上下左右 65 66 68 67

fflush(stdin);

}

usleep(1000 * 30);

}

标准输入流在读取输入时是阻塞的,可以调整为非阻塞的。如下代码

static struct termios old, current;

void initTermios(int echo) {

tcgetattr(0, &old); /* grab old terminal i/o settings */

current = old; /* make new settings same as old settings */

current.c_lflag &= ~ICANON; /* disable buffered i/o */

if (echo)  current.c_lflag |= ECHO; /* set echo mode */

else  current.c_lflag &= ~ECHO; /* set no echo mode */

tcsetattr(0, TCSANOW, &current); /* use these new terminal i/o settings now */

}

使用上下左右键来控制方块。向上键是旋转,向下键是瞬移

使用特定字符来拼凑黑白块

char* BLACKBLOCK = "▐█";

char* WHITEBLOCK = " ";

代码提交在https://github.com/lizhipingmaster/ShellTetris中

TODO

1. 棋盘状态使用byte数组(表示黑白及其他状态)而不是直接用char数组表示

2. 计分

3. 黑块消失时变色变形效果

相关文章

网友评论

    本文标题:在shell中开发一个俄罗斯方块

    本文链接:https://www.haomeiwen.com/subject/lulfaktx.html