游戏说明:运行在Linux环境下 基于Ncurse图形库的C语言小程序
项目知识要求:C编程基础、Linux基本操作(代码编写、编译代码、运行代码、创建文件夹)
代码的编辑:vi test1.c 进入文档按下i进行编写代码 按下esc进入命令行输入:wq保存代码
编译代码:gcc test1.c 然后回生成可执行的程序
运行代码:./a.out
创建文件夹:mkdir tanchishe
项目的目的与意义:启到承上启下的作用
承上:C语言基础、数据结构链表基础,C变量,流程控制,函数,指针,结构体等
启下:Linux系统编程,文件编程,进程,线程,通信,第三方包等
项目解读:
1.为什么要用ncurse? 相应键盘的上下左右键
2.地图规划
1.ncurse的输入输出
#include <curses.h>//包含头文件
int main()
{initscr();//nurse界面的初始化函数printw(“This is a curses window.\n”);//在ncurse模式下的printfgetch();//等待用户输入,如果没有这句话,程序就退出了,看不到运行的结果,也就是看不到上面那句话endwin();//程序退出,调用改函数来恢复shell终端的显示,如果没有这句话,shell终端字乱码,坏掉
}
使用curses时包含头文件#include<curses.h>
curses函数库能够优化光标的移动并最小化需要对屏幕进行的刷新,从而也减少了必须向字符终端发送的字符数目。
在linux中可以查看头文件中包含的内容 vi /uesr/include/curses.h,使用这些函数要用keypad函数
编译ncurse文件时需要包含头文件<curses.h>编译时gcc 工程名 -lcurses
int main()
{int key;initscr();keypad(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(); return 0;
}
2.地图规划
技术点:普通C语言的for循环语句和printw的输出
大小20x20
地图竖直方向上的边界”|“
地图水平方向上的边界”--“
贪吃蛇的身子”[]“
贪吃蛇食物”##“
#include "curses.h"void initNcurse()
{ initscr();keypad(stdscr,1);
}void gamePic()
{int hang;int lie;for(hang = 0;hang < 20;hang++){if(hang == 0){for(lie=0;lie<20;lie++){printw("--"); }printw("\n");}if(hang>=0 && hang <=19){ for(lie=0;lie<=20;lie++){if(lie == 0 || lie == 20){printw("|");}else{printw(" ");}}printw("\n");}if(hang == 19){for(lie=0;lie<20;lie++){printw("--"); }printw("\n");printw("BY Walker F.M"); }}
}int main()
{initNcurse();gamePic();getch();endwin();return 0;
}
3.贪吃蛇的绘制
3.1贪吃蛇的身子节点
1.行坐标
2.列坐标
3.下一个节点的位置(地址/指针)
#include "curses.h"
#include "stdlib.h"#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2struct Snake
{int hang;int 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;int y = rand()%20;food.hang = x;food.lie = y;
}void initNcurse()
{ initscr();keypad(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;int lie;move(0,0);for(hang = 0;hang < 20;hang++){ if(hang == 0){for(lie=0;lie<20;lie++){printw("--"); }printw("\n");}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");}if(hang == 19){for(lie=0;lie<20;lie++){printw("--"); }printw("\n");printw("BY Walker F.M,key = %d\n",key); }}
}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;//璁╁ご灏捐妭鐐归兘鎸囧悜{2,2,NULL} addNode(); addNode(); addNode();
}void deleNode()
{struct Snake *p;p = head;head = head->next;free(p);
}int ifSnakeDie()
{ struct Snake *p;p = head;if(tail->hang < 0 || tail->lie == 0 || tail->hang == 20 || 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(); }if(ifSnakeDie()){initSnake();}
}void* refreshJieMian()
{while(1) {moveSnake();gamePic();refresh();usleep(100000);}
}void turn(int direction)
{if(abs(dir) != abs(direction)){dir = direction;}
}void* changeDir()
{while(1){key = getch();switch(key){case KEY_DOWN:turn(DOWN);break;case KEY_UP:turn(UP);break;case KEY_LEFT:turn(LEFT);break;case KEY_RIGHT:turn(RIGHT);break;}}
}int main()
{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缁堢涔辩爜return 0;
}