运行环境:Linux
#include<curses.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake *head;
struct Snake *tall;
int key;
int dir;
struct Snake food;
void iniFood()
{
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
x+=3;
y+=4;
}
void inicurses()
{
initscr();
keypad(stdscr,1);
noecho();//防止不需要的东西打印出来
}
int dingWeiPanDuan(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 dingWeiFood(int i,int j)
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void map()
{
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");
}
for(lie = 0; lie<=20; lie++){
if(lie == 0 || lie == 20){
printw("|");
}else if(dingWeiPanDuan(hang,lie)){
printw(")(");
}else if(dingWeiFood(hang,lie)){
printw("**");
}
else{
printw(" ");
}
}
printw("\n");
if(hang == 20){
for(lie = 0;lie<20;lie++){
printw("--");
}
printw("\n");
printw("By smallSnake,foog.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 = tall->hang-1;
new->lie = tall->lie;
break;
case DOWN:
new->hang = tall->hang+1;
new->lie = tall->lie;
break;
case LEFT:
new->hang = tall->hang;
new->lie = tall->lie-1;
break;
case RIGHT:
new->hang = tall->hang;
new->lie = tall->lie+1;
break;
}
tall->next = new;
tall = new;
}
void inisnake()
{
struct Snake *p;
dir = RIGHT;
while(head != NULL){//释放死亡后的节点
p = head;
head = head->next;
free(p);
}
iniFood();
head =(struct Snake *)malloc(sizeof(struct Snake));
head->hang = 1;
head->lie = 1;
head ->next = NULL;
tall = head;
addNode();
addNode();
addNode();
addNode();
addNode();
}
void deleNode()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
int ifSnakeDie()
{
struct Snake *p;
p = head;
if(tall->hang < 0 || tall->lie == 0 ||tall->hang == 20 ||tall->lie == 20){
return 1;
}
while(p->next!=NULL){
if(p->hang == tall->hang && p->lie == tall->lie){
return 1;
}
p=p->next;
}
return 0;
}
void moveSnake()
{
addNode();
if(dingWeiFood(tall->hang,tall->lie)){
iniFood();
}
else{
deleNode();
}
if(ifSnakeDie()){
inisnake();
}
}
void* refreshMap()
{
while(1){
moveSnake();
map();
refresh();
usleep(100000);
}
}
void turn(int dirAbsolute)
{
if(abs(dir) != abs(dirAbsolute)){
dir = dirAbsolute;
}
}
void *direction()//空型void指针
{
while(1){
key = getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
//dir = DOWN;
break;
case KEY_UP:
turn(UP);
//dir = UP;
break;
case KEY_LEFT:
turn(LEFT);
//dir = LEFT;
break;
case KEY_RIGHT:
turn(RIGHT);
//dir = RIGHT;
break;
}
}
}
int main()
{
pthread_t t1;//创建线程
pthread_t t2;
inicurses();
inisnake();
map();
pthread_create(&t1,NULL,refreshMap,NULL);
pthread_create(&t2,NULL,direction,NULL);//类似原型
while(1);
endwin();
return 0;
}