当前位置: 首页> 文旅> 美景 > C项目实战:俄罗斯方块

C项目实战:俄罗斯方块

时间:2025/7/9 8:29:59来源:https://blog.csdn.net/m0_58233509/article/details/139013446 浏览次数:0次

图形

终端控制代码 (ANSI/VT100) 简介

终端 (控制) 代码是用来控制终端的特殊命令,它可以改变颜色和光标的位置,实现那些无法被程序本身完成的操作。

参考文章:终端控制代码 (ANSI/VT100) Terminal Codes 简介 (转载翻译)_终端代码-CSDN博客

#include <stdlib.h>
#include <stdio.h>int main()
{printf("\033[2J\033[1;1H\033[31;4mhello\n\033[0m");return 0;
}

输入设备

#include <stdlib.h>
#include <stdio.h>
#include <termios.h> int main()
{int ch;struct termios new; //用于去掉回显tcgetattr(0,&new);new.c_lflag =  new.c_lflag & ~(ICANON |ECHO);new.c_cc[VMIN] = 1; //接受最小长度new.c_cc[VTIME] = 0;//时间接受tcsetattr(0,TCSANOW,&new);while(1){ch = getchar();if(ch == 'Q')break;printf("%x ",ch);// 按照16进制进行输出,输入a,,十进制是97,十六进制输出65, A输出41 回车(LF)输出afflush(NULL);}return 0;
}

执行编译后的代码退出后,发现终端命令不显示,需要输入(reset命令)重启终端设备

#include <stdlib.h>
#include <stdio.h>
#include <termios.h> int main()
{int ch;struct termios new,old; //用于去掉回显tcgetattr(0,&new);tcgetattr(0,&old);new.c_lflag =  new.c_lflag & ~(ICANON |ECHO);new.c_cc[VMIN] = 1; //接受最小长度new.c_cc[VTIME] = 0;//时间接受tcsetattr(0,TCSANOW,&new);while(1){ch = getchar();if(ch == 'q')break;printf("%x ",ch);// 按照16进制进行输出,输入a,,十进制是97,十六进制输出65, A输出41 回车(LF)输出afflush(NULL);}printf("\n");tcsetattr(0,TCSANOW,&old);//恢复模式return 0;
}

并发

利用signal信号中的SIGALRM来实现

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>#include <signal.h>void int_handler(int code)
{printf("Get SIGINT\n");//code = 2printf("code = %d \n",code);//code = 2
}void alrm_handler(int code) //code显示对应的值,用kill -l命令查看
{alarm(1);// 每次收到alarm在调用信号printf("Get SIGALRM \n");//code = 14
}
int main()
{signal(SIGINT,int_handler);signal(SIGALRM,alrm_handler);alarm(1);//隔1秒发出SIGALRM信号while(1){int ch;ch = getchar();printf("ch = %x \n",ch);fflush(NULL);
//        printf("while(1)\n");
//        sleep(1);}return 0;
}

关键字:C项目实战:俄罗斯方块

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: