C语言获取键盘输入的函数:getchar;scanf;gets。需要回车才能响应
贪吃蛇项目:
1、ncurse:
可取代ncurse的:C图形库GTK;C++图形库QT。基本落伍
使用ncurse是因为按键响应及时,不需要回车
创建.c文件,后边需要添加-lcurses,
使用gcc编译时,需要添加 -lcurses
#include <curses.h>int main()
{initscr();//ncurse界面初始化函数printw("This is a curses window.\n");//ncurse模式下的printgetch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果,即看不到上面那句话。endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
2、贪吃蛇方向按键上下左右:
vi /usr/include/curses.h 查看相应功能性按键
头文件:include<curses.h>
KEY_DOWN == 0402;
KEY_UP == 0403;
KEY_LEFT == 0404;
KEY_RIGHT == 0405;
#include <curses.h>int main()
{int key;initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收while(1){key = getch();switch(key){case KEY_DOWN:printw("DOWN\n");break;case KEY_UP:printw("UP\n");break;case KEY_LEFT:printw("LEFT\n");break;case KEY_RIGHT:printw("RIGHT\n");break;}}endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
3、贪吃蛇地图规划:
大小20x20
竖直方向上的边界用 “|”
水平边界用 “–”
第0行和第19行既有横边界也有竖边界,1-18行只有竖边界
先做上边一横,再做两竖,中间用空格填充, 最后做一横
#include <curses.h>void initNcurse()
{initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收
}void gamePic()
{int hang,lie;//第0行的边界for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else{printw(" ");}}printw("\n");}//1-18行的边界if(hang > 0 && hang <= 18){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else{printw(" ");}}printw("\n");for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang");}}
}
int main()
{initNcurse();gamePic();getch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果.endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
地图的优化:取消重复的代码
for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");}//第0行与第19行的“|”和空格与下边一致if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang");}}
}
4、贪吃蛇的身体:“[]”
身体节点(结构体)包含:行坐标,列坐标,下一个节点的位置(地址/指针)
先做一个节点,定义身体行列结构体,与地图上的坐标对比,如果相同则print[]
定义多个身子节点,在第一个节点基础上,行不变,列+/-1.
用链表遍历的方式显示完整身子
struct Snake
{int hang,lie;struct Snake *next;
};
struct Snake node1 = {2,2,NULL};//节点为(2,2)
struct Snake node2 = {2,3,NULL};
struct Snake node3 = {2,4,NULL};
//封装函数获取节点
void hasSnakeNode(int i,j)
{struct Snake *p;p = &node1;while(p != NULL){if(p->hang == i;p->lie == j){return 1;}p = p->next;}
}
//在打印空格的地方加入遍历,显示蛇的身体
if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else if(hasSnakeNode(hang,lie)){//函数封装遍历printw("[]");}else{printw(" ");}}printw("\n");}
//主函数添加
node1.next = &node2;
node2.next = &node3;
链表动态添加蛇的节点:
修改简化上述代码
#include <curses.h>
#include <stdlib.h>struct Snake *head;
struct Snake *tail;struct Snake
{int hang,lie;struct Snake *next;
};void initNcurse()
{initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收
}/*struct Snake node1 = {2,2,NULL};//节点为(2,2)
struct Snake node2 = {2,3,NULL};
struct Snake node3 = {2,4,NULL};
*/
//封装函数获取节点
int hasSnakeNode(int i,int j)
{ struct Snake *p;p = head;while(p != NULL){if(p->hang == i && p->lie == j){return 1;}p = p->next;}return 0;
}
//在打印空格的地方加入遍历,显示蛇的身体
void gamePic()
{int hang,lie;//第0行的边界for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");}//第0行与第19行的“|”和空格与下边一致if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");}else if(hasSnakeNode(hang,lie)){//函数封装遍历printw("[]");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang");}}
}
//添加节点
void addNode()
{struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));new->hang = tail->hang;new->lie = tail->lie+1;new->next = NULL;tail->next = new;tail = new;
}
//初始化节点
void initSnake()
{head = (struct Snake *)malloc(sizeof(struct Snake));head->hang = 2;head->lie = 2; //头结点的位置head->next = NULL;tail = head;addNode();addNode();addNode();//4个节点
}int main()
{initNcurse();initSnake();gamePic();getch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果.endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
贪吃蛇移动向右移动:删除头节点,尾巴添加一个新节点
贪吃蛇撞墙死后重新初始化蛇节点。重新初始化后要释放前节点;
#include <curses.h>
#include <stdlib.h>struct Snake *head = NULL;
struct Snake *tail = NULL;struct Snake
{int hang,lie;struct Snake *next;
};void initNcurse()
{initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收
}//封装函数获取节点
int hasSnakeNode(int i,int j)
{ struct Snake *p;p = head;while(p != NULL){if(p->hang == i && p->lie == j){return 1;}p = p->next;}return 0;
}
//地图
void gamePic()
{int hang,lie;//ncurse光标的改变//改变光标,使蛇身移动之后覆盖原地图move(0,0);//第0行的边界for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");}//第0行与第19行的“|”和空格与下边一致if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");
//在打印空格的地方加入遍历,显示蛇的身体}else if(hasSnakeNode(hang,lie)){//函数封装遍历printw("[]");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang");}}
}
//添加节点
void addNode()
{struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));new->hang = tail->hang;new->lie = tail->lie+1;new->next = NULL;tail->next = new;tail = new;
}
//初始化节点
void initSnake()
{struct Snake *p;while(head != NULL){ //释放旧的链表p = head;head = head->next;free(p);}head = (struct Snake *)malloc(sizeof(struct Snake));head->hang = 1;head->lie = 1; //头结点的位置head->next = NULL;tail = head;addNode();addNode();addNode();//4个节点
}//删除节点
void deleNode()
{struct Snake *p;p = head; //p指向头,使删除的节点释放head = head->next;free(p);
}//蛇身体的向右移动
void moveSnake()
{addNode();deleNode();//撞墙(链尾撞)(hang=0撞上,hang=20撞下,lie=0撞左,lie=20撞右)if(tail->hang == 0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20){initSnake();//死后重新初始化蛇节点。重新初始化后要释放前节点}}int main()
{int con;initNcurse();initSnake();gamePic();while(1){
{ //自由运动moveSnake();gamePic();//改变之后需要重新扫描地图才能实现//刷新界面refresh();usleep(100000);//以微秒为单位;sleep(1);以秒为单位}getch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果.endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
贪吃蛇实现自行游走;(需要用到while(1)来检测用户键盘输入)
贪吃蛇的方向改变,
[刷新界面与刷新方向键都是用while(1),两个while(1)就要用到linux线程来解决]
**线程:**程序都是从上到下线性运行,要想2个同时运行,需要linux线程;一个程序有两个线程,互不影响。线程头文件#include<pthread.h>gcc编译的时候需添加-pthread,若-pthread显示错乱,用-lpthread例:
pthread_t th1;pthread_create(&th1,NULL,func1,NULL);//&th1为线程的描述符,func1为创建的函数,返回值为void*,NULL为函数中的参数//void *func1(),定义的该函数返回值是void*型
贪吃蛇的身体及其自由移动:
#include <curses.h>
#include <stdlib.h>#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2struct Snake
{int hang,lie;struct Snake *next;
};struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int dir;void initNcurse()
{initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收noecho();
}//封装函数获取节点
int hasSnakeNode(int i,int j)
{ struct Snake *p;p = head;while(p != NULL){if(p->hang == i && p->lie == j){return 1;}p = p->next;}return 0;
}
//地图
void gamePic()
{int hang,lie;//ncurse光标的改变//改变光标,使蛇身移动之后覆盖原地图move(0,0);//第0行的边界for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");}//第0行与第19行的“|”和空格与下边一致if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");
//在打印空格的地方加入遍历,显示蛇的身体}else if(hasSnakeNode(hang,lie)){//函数封装遍历printw("[]");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang,\n");}}
}
//添加节点
void addNode()
{struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));new->next = NULL;switch(dir){case UP:new->hang = tail->hang-1;new->lie = tail->lie;break;case DOWN:new->hang = tail->hang+1;new->lie = tail->lie;break;case LEFT:new->hang = tail->hang;new->lie = tail->lie-1;break;case RIGHT:new->hang = tail->hang;new->lie = tail->lie+1;break;}tail->next = new;tail = new;
}
//初始化节点
void initSnake()
{struct Snake *p;//初始方向dir = RIGHT;while(head != NULL){ //释放旧的链表p = head;head = head->next;free(p);}head = (struct Snake *)malloc(sizeof(struct Snake));head->hang = 1;head->lie = 1; //头结点的位置head->next = NULL;tail = head;addNode();addNode();addNode();//4个节点
}//删除节点
void deleNode()
{struct Snake *p;p = head; //p指向头,使删除的节点释放head = head->next;free(p);
}//蛇身体的向右移动
void moveSnake()
{addNode();deleNode();//撞墙(链尾撞)(hang=0撞上,hang=20撞下,lie=0撞左,lie=20撞右)if(tail->hang == 0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20){initSnake();//死后重新初始化蛇节点。重新初始化后要释放前节点}}
//界面刷新封装(线程)
void* refreshJiemian()
{while(1){//自由运动moveSnake();gamePic();//改变之后需要重新扫描地图才能实现//刷新界面refresh();usleep(100000);//以微秒为单位;sleep(1);以秒为单位}
}//限制蛇身体直上直下的转向
void turn(int direction)
{if(abs(dir) != abs(direction)){ //abs:取绝对值dir = direction;}
}//方向改变封装(线程)
void* changeDir()
{//int key;while(1){key = getch();switch(key){case KEY_DOWN://dir = DOWN;turn(DOWN);break;case KEY_UP://dir = UP;turn(UP);break;case KEY_LEFT://dir = LEFT;turn(LEFT);break;case KEY_RIGHT://dir = RIGHT;turn(RIGHT);break;}}
}int main()
{//两个while(1)的实现需要用到线程pthread_t t1;pthread_t t2;initNcurse();initSnake();gamePic();pthread_create(&t1,NULL,refreshJiemian,NULL);pthread_create(&t2,NULL,changeDir,NULL);while(1);getch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果.endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}
5、贪吃蛇的食物:“##”
食物的位置:设置一个初始位置,用static设置一个静态变量,函数被重新调用的时候值不会改变。
//初始化食物
void initFood()
{static int x = 4; //static是一个静态变量,函数被重新调用的时候值不会改变static int y = 5;food.hang = x;food.lie = y;x+=2;//食物的偏移y+=2;
}
改进:
食物的随机刷新用:rand()%20;
rand()生成一个随机数,生成较大的数采用取余,%20取余余数范围在0-19之间。
蛇吃食物,重新刷新,并且使食物的位置保持在边框内。
//初始化食物
void initFood()
{int x = rand()%20; //xrand()生成一个随机数,生成较大的数采用取余,%20取余int y = rand()%20;food.hang = x;food.lie = y;
}
蛇撞自杀:
//蛇自杀死亡
int ifSnakeDie()
{//定义变量,遍历贪吃蛇struct Snake *p;p = head;if(tail->hang < 0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20){return 1;}while(p->next != NULL){if(p->hang == tail->hang && p->lie == tail->lie){return 1;}p = p->next;}return 0;
}
贪吃蛇完整代码:
#include <curses.h>
#include <stdlib.h>#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2struct Snake
{int hang,lie;struct Snake *next;
};struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int dir;//定义蛇的食物
struct Snake food;//初始化食物
void initFood()
{int x = rand()%20; //xrand()生成一个随机数,生成较大的数采用取余,%20取余int y = rand()%20;food.hang = x;food.lie = y;}void initNcurse()
{initscr();//ncurse界面初始化函数keypad(stdscr,1);//从标准的stdscr中接收功能键,1表示接收noecho();
}//封装函数获取节点
int hasSnakeNode(int i,int j)
{ struct Snake *p;p = head;while(p != NULL){if(p->hang == i && p->lie == j){return 1;}p = p->next;}return 0;
}//封装函数获取食物
int hasFood(int i,int j)
{ if(food.hang == i && food.lie == j){return 1;}return 0;
}//地图
void gamePic()
{int hang,lie;//ncurse光标的改变//改变光标,使蛇身移动之后覆盖原地图move(0,0);//第0行的边界for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");}//第0行与第19行的“|”和空格与下边一致if(hang >= 0 || hang <= 19){for(lie = 0;lie <= 20;lie++){if(lie == 0 || lie == 20){printw("|");
//在打印空格的地方加入遍历,显示蛇的身体}else if(hasSnakeNode(hang,lie)){//函数封装遍历printw("[]");}else if(hasFood(hang,lie)){ //判断是否有食物,有用##显示printw("##");}else{printw(" ");}}printw("\n");}//第19行的边界if(hang == 19){for(lie = 0;lie < 20;lie++){printw("--");}printw("\n");printw("By laowang,food.hang=%d,food.lie=%d\n",food.hang,food.lie);}}
}
//添加节点
void addNode()
{struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));new->next = NULL;switch(dir){case UP:new->hang = tail->hang-1;new->lie = tail->lie;break;case DOWN:new->hang = tail->hang+1;new->lie = tail->lie;break;case LEFT:new->hang = tail->hang;new->lie = tail->lie-1;break;case RIGHT:new->hang = tail->hang;new->lie = tail->lie+1;break;}tail->next = new;tail = new;
}
//初始化节点
void initSnake()
{struct Snake *p;//初始方向dir = RIGHT;while(head != NULL){ //释放旧的链表p = head;head = head->next;free(p);}initFood();head = (struct Snake *)malloc(sizeof(struct Snake));head->hang = 1;head->lie = 1; //头结点的位置head->next = NULL;tail = head;addNode();addNode();addNode();//4个节点
}//删除节点
void deleNode()
{struct Snake *p;p = head; //p指向头,使删除的节点释放head = head->next;free(p);
}//蛇自杀死亡
int ifSnakeDie()
{//定义变量,遍历贪吃蛇struct Snake *p;p = head;if(tail->hang < 0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20){return 1;}while(p->next != NULL){if(p->hang == tail->hang && p->lie == tail->lie){return 1;}p = p->next;}return 0;
}//蛇身体的移动
void moveSnake()
{addNode();//判断是否吃到食物,吃到,不删除节点if(hasFood(tail->hang,tail->lie)){initFood();}else{deleNode();}//撞墙(链尾撞)(hang=0撞上,hang=20撞下,lie=0撞左,lie=20撞右)if(ifSnakeDie()){initSnake();//死后重新初始化蛇节点。重新初始化后要释放前节点}}
//界面刷新封装(线程)
void* refreshJiemian()
{while(1){//自由运动moveSnake();gamePic();//改变之后需要重新扫描地图才能实现//刷新界面refresh();usleep(100000);//以微秒为单位;sleep(1);以秒为单位}
}//限制蛇身体直上直下的转向
void turn(int direction)
{if(abs(dir) != abs(direction)){ //abs:取绝对值dir = direction;}
}//方向改变封装(线程)
void* changeDir()
{//int key;while(1){key = getch();switch(key){case KEY_DOWN://dir = DOWN;turn(DOWN);break;case KEY_UP://dir = UP;turn(UP);break;case KEY_LEFT://dir = LEFT;turn(LEFT);break;case KEY_RIGHT://dir = RIGHT;turn(RIGHT);break;}}
}int main()
{//两个while(1)的实现需要用到线程pthread_t t1;pthread_t t2;initNcurse();initSnake();gamePic();pthread_create(&t1,NULL,refreshJiemian,NULL);pthread_create(&t2,NULL,changeDir,NULL);while(1);getch();//等待用户输入,如果没这句话程序就退出了,看不到运行结果.endwin();//程序退出,调用改函数来恢复shell终端显示,如果没这句话,shell终端字乱码,坏掉return 0;}