贪吃蛇小游戏

运用链表的创建建立一个贪吃蛇游戏,对于c语言链表以及指针的掌握更加牢靠。

#include<stdio.h>
#include<curses.h>
#include<stdlib.h>
struct snake *addsnake(struct snake *tail);//用于贪吃蛇添加身体长度的函数
void initsnake(struct snake *head);//用于初始化贪吃蛇的身体
int snakebody(int han,int li,struct snake *node);//用于在showpicture函数中判断是否显示贪吃蛇的身体
void initncurses();//用于初始化界面
void showpicture(struct snake *node);//用于显示贪吃蛇游戏的函数
struct snake *delete(struct snake *head);//在贪吃蛇前进过程中,删掉头节点的函数,使贪吃蛇在不吃东西的情况下身体的长度一直保持一致
struct snake *pengzhuang(void);//在贪吃蛇碰撞到墙壁时游戏结束的函数
struct snake *movesnake(struct snake *head,struct snake *head1);//贪吃蛇移动的函数
void changedir();//改变按键贪吃蛇走位的函数
void freshdemo(struct snake *head);//不断刷新界面,使游戏运转的函数
void initfood();//食物的初始化函数
int hasfood(int,int);//在showpicture函数中判断是否显示食物的函数
int eatself(struct snake *a,struct snake *b);//贪吃蛇碰撞到自己的身体结束游戏
int key;//键盘获取上下左右变量
int dir;//方向变量
#define RIGHT 1
#define LEFT -1
#define UP    2
#define DOWN -2//此处是取绝对值,使得贪吃蛇在向右行驶的时候不可以再向左走,只能向上下走

struct snake
{
int hang;
int lie;
struct snake *next;
};

struct snake food;

void initfood()
{
int x=rand()%18;
int y=rand()%18;
if(x==0 || y==0)
{
x=5,y=8;
}
food.hang=x;
food.lie=y;
}

int hasfood(int a,int b)
{
if(food.hang==a && food.lie==b)
{
return 1;
}
return 0;
}

struct snake *addsnake(struct snake *tail)
{
struct snake *p=tail;
struct snake *new=(struct snake*)malloc(sizeof(struct snake));
switch(dir)
{
case RIGHT:
new->hang=p->hang;
new->lie=p->lie+1;
break;
case LEFT:
new->hang=p->hang;
new->lie=p->lie-1;
break;
case UP:
new->hang=p->hang-1;
new->lie=p->lie;
break;
case DOWN:
new->hang=p->hang+1;
new->lie=p->lie;
break;
}
p->next=new;
new->next=NULL;
p=new;
return p;
}

void initsnake(struct snake *head)
{
dir=RIGHT;
int i;
struct snake *p=head;
for(i=1;i<=2;i++)
{
p=addsnake(p);
}
}

int snakebody(int han,int li,struct snake *node)
{
struct snake *p=node;
while(p != NULL)
{
if(p->hang==han && p->lie==li)
{
return 1;
}
p=p->next;
}
return 0;
}

void initncurses()
{
initscr();
keypad(stdscr,1);
}

void showpicture(struct snake *node)
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++)
{
if(hang==0 || hang==19)
{
for(lie=0;lie<20;lie++)
{
printw("--");
}
printw("\n");
}
if(hang>0 && hang <19)
{
for(lie=0;lie<21;lie++)
{
if(lie==0 || lie==20)
{
printw("|");
}else if(snakebody(hang,lie,node))
{printw("[]");}
else if(hasfood(hang,lie))
{
printw("$$");
}
else{printw("  ");}
}
printw("\n");
}
}
}

struct snake *delete(struct snake *head)
{
struct snake *m;
m=head;
head=head->next;
free(m);
return head;
}

struct snake *pengzhuang(void)
{
struct snake *tou=(struct snake*)malloc(sizeof(struct snake));
tou->hang=5;
tou->lie=5;
tou->next=NULL;
initsnake(tou);
return tou;
}

int eatself(struct snake *head,struct snake *tail)
{
struct snake *p=head;
while(p->next != NULL)
{
if(p->hang==tail->hang && p->lie==tail->lie)
{
return 1;
}
p=p->next;
}
return 0;
}

struct snake *movesnake(struct snake *head,struct snake *head1)
{
struct snake *tail;
struct snake *tou;
struct snake *p=head;
while(p->next != NULL){p=p->next;}
p=addsnake(p);
if(p->hang==food.hang && p->lie==food.lie){initfood();}
else{head=delete(head);}
if((p->hang==0 || p->hang==19 || p->lie==0 || p->lie==20)||(eatself(head,p)))
{
while(head != NULL)
{
struct snake *sha=head;
head=head->next;
free(sha);
}
tou=pengzhuang();
return tou;
}
return head;
}

void turn(int direction)
{
if(abs(dir) != abs(direction))
{
dir=direction;
}
}

void changedir()
{
while(1)
{
key=getch();
switch(key)
{
case KEY_RIGHT:
turn(RIGHT);
break;
case KEY_DOWN:
turn(DOWN);
break;
}
}
}

void freshdemo(struct snake *head)
{
struct snake *headnew=head;
 while(1)
{
headnew=movesnake(headnew,headnew);
showpicture(headnew);
refresh();
usleep(150000);
}
}

int main()
{
int j;
initfood();
struct snake *head=(struct snake*)malloc(sizeof(struct snake));
head->hang=5;
head->lie=5;
head->next=NULL;
initncurses();
initsnake(head);
showpicture(head);
pthread_t  t1;
pthread_t  t2;
pthread_create(&t1,NULL,changedir,NULL);
pthread_create(&t2,NULL,freshdemo,head);
while(1);
getch();
endwin();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值