//贪食蛇
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<malloc.h>
#include <time.h>
int food[2];//食物
struct snake //蛇
{
int x;//一节蛇坐标
int y;
int life;//这个有什么用?
struct snake *next;//指向下一节
};
struct snake *createlist(/*struct snake* head */)//创建二节蛇
{
struct snake* q,*p;
//第一节,头
q=(struct snake*)malloc(sizeof(struct snake));
//q=head;
q->x=16;q->y=16;q->life=1;
//第二节
p=(struct snake*)malloc(sizeof(struct snake));
q->next=p;
p->x=16;p->y=15;p->life=1;
p->next=NULL;//蛇尾
return q;//返回蛇头
}
void drawdot(int x,int y)//绘制蛇的一节
{
setfillcolor(BLUE);//颜色
x=(x-1)*20;
y=(y-1)*20;
bar(x,y,x+20,y+20);
}
void drawsnake(struct snake* head)//绘制蛇
{
struct snake* p;
p=head;//从蛇头开始
while(p!=NULL)//如果到蛇尾结束绘制
{
drawdot(p->x,p->y);//绘制一节
p=p->next;//下一节
}
}
int main()
{
initgraph(960,640);
struct snake* head;//定义贪食蛇
head=createlist();//初始化两节蛇
drawsnake(head);//显示
//游戏循环
getch();
closegraph();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<malloc.h>
#include <time.h>
int food[2];//食物
struct snake //蛇
{
int x;//一节蛇坐标
int y;
int life;//这个有什么用?
struct snake *next;//指向下一节
};
struct snake *createlist(/*struct snake* head */)//创建二节蛇
{
struct snake* q,*p;
//第一节,头
q=(struct snake*)malloc(sizeof(struct snake));
//q=head;
q->x=16;q->y=16;q->life=1;
//第二节
p=(struct snake*)malloc(sizeof(struct snake));
q->next=p;
p->x=16;p->y=15;p->life=1;
p->next=NULL;//蛇尾
return q;//返回蛇头
}
void drawdot(int x,int y)//绘制蛇的一节
{
setfillcolor(BLUE);//颜色
x=(x-1)*20;
y=(y-1)*20;
bar(x,y,x+20,y+20);
}
void drawsnake(struct snake* head)//绘制蛇
{
struct snake* p;
p=head;//从蛇头开始
while(p!=NULL)//如果到蛇尾结束绘制
{
drawdot(p->x,p->y);//绘制一节
p=p->next;//下一节
}
}
int main()
{
initgraph(960,640);
struct snake* head;//定义贪食蛇
head=createlist();//初始化两节蛇
drawsnake(head);//显示
//游戏循环
getch();
closegraph();
return 0;
}