多线程实现
操纵蛇是通过键盘的上下左右键
// gcc snake.c -lpthread #include <stdio.h> #include <stdlib.h> #include <time.h> #include <signal.h> #include <termios.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <pthread.h> #define KEYCODE_U 0x41 // 向上按键 #define KEYCODE_D 0x42 // 向下按键 #define KEYCODE_L 0x44 // 向左按键 #define KEYCODE_R 0x43 // 向右按键 int kfd = 0; struct termios cooked, raw; char dir = KEYCODE_U; // 当前蛇的移动方向 // 获取键盘响应:上、下、左、右键 void* get_dir(void *a) {while(1){char c;tcgetattr(kfd, &cooked); // 得到 termios 结构体保存,然后重新配置终端memcpy(&raw, &cooked, sizeof(struct termios));raw.c_lflag &=~ (ICANON | ECHO);raw.c_cc[VEOL] = 1;raw.c_cc[VEOF] = 2;tcsetattr(kfd, TCSANOW, &raw);if(read(kfd, &c, 1) < 0){perror("read():");exit(-1);}tcsetattr(kfd, TCSANOW
12345678910111213141516171819202122232425262728293031323334353637383940414243