贪吃蛇源程序

急求一个贪吃蛇的TC的源程序,最好详细分块有注释的,尤其是键盘控制这一块的... 急求一个贪吃蛇的TC的源程序,最好详细分块有注释的,尤其是键盘控制这一块的 展开
 我来答
azhi61ming
推荐于2017-10-02 · TA获得超过250个赞
知道小有建树答主
回答量:290
采纳率:0%
帮助的人:197万
展开全部
下面是我以前写的,也不知道好不好,只是凭着自己坦慧感兴趣就悄信搭写下了。要是对你有作用就看看。
#include "graphics.h"
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
char way='r',waypo;
int Long=3,eat=0;
long Score=0;
void initshe();
void newfood();
void gameover();
void deng();
void shandian();
void ifdie();
void step();
void xianshi();
void bianli(int dd);
void jiachang(int jj);
typedef struct zuobiao
{
int x,y;
struct zuobiao *next;
}shedian;
shedian *head,*last,*p,*q;
void main()
{
long key=0;
randomize();
initshe();
shandian();
outtextxy(200,90,"Press any key to start!");getchar();
setfillstyle(1,YELLOW);bar(190,80,400,120);setfillstyle(1,BLUE);
while(1)
{
waypo=way;
if(bioskey(1))
{
key=bioskey(0);/*printf("key=%0x",key);*/
switch(key)
{
case ESC: gameover();break;
case UP: if(waypo!='d')way='u';break;
case DOWN: if(waypo!='u')way='d';break;
case LEFT: if(waypo!='r')way='l';break;
case RIGHT: if(waypo!='l')way='r';break;
case SPACE: deng();break;
default: break;
}
}
if(!eat)newfood();
step();
ifdie();
xianshi();
}
}
void initshe()
{
int gdriver=DETECT,gmode;
/* registerbgidriver(EGAVGA_driver);*/
initgraph(&gdriver,&gmode,"启拿g:\\CYUYAN\\TC\\");
setbkcolor(YELLOW);
setcolor(CYAN);
rectangle(0,0,639,479);
rectangle(10,10,630,470);
rectangle(59,39,579,439);
setfillstyle(1,GREEN);
floodfill(15,15,CYAN);
setfillstyle(8,RED);
floodfill(1,1,GREEN);
setcolor(BLUE);
line(59,59,579,59);
settextstyle(0,0,3);
outtextxy(220,13,"tan chi she");
setcolor(BROWN);
settextstyle(0,0,1);
outtextxy(65,50,"Score:");
outtextxy(200,50,"Long:");
outtextxy(330,50,"Creategamename:JiangZhiMing");
}
int r1,r2;
void newfood()
{
loop:r1=random(52)*10+65;
r2=random(38)*10+65;
q=head;
while(q->next)
{
if(q->x==r1&&q->y==r2)
goto loop;
q=q->next;
}
rectangle(r1-5,r2-5,r1+5,r2+5);
bar(r1-4,r2-4,r1+4,r2+4);
eat=1;
}
void gameover()
{
settextstyle(0,0,2);
outtextxy(200,80,"Want to win????");
outtextxy(200,100," NO!!!!");getchar();
setfillstyle(1,WHITE);
bar(0,0,639,479);
setcolor(GREEN);
settextstyle(0,0,2);
outtextxy(200,120,"!!GAMEOVER!!");
outtextxy(205,135," !baibai!");
while(head->next)
{
p=head;
head=head->next;
free(p);
}
getchar();
closegraph();
exit(0);
}
void deng()
{
int key1;
while(5)
{
key1=bioskey(0);
if(key1==SPACE)
break;
}
}
void shandian()
{
head=malloc(sizeof *head);
head->x=75,head->y=75;
p=malloc(sizeof *p);
p=head->next;
p->x=85,p->y=75;
last=malloc(sizeof *last);
last=p->next;
last->x=95,last->y=75;
last->next=NULL;
setcolor(RED);
setfillstyle(1,BLUE);
rectangle(head->x-5,head->y-5,head->x+5,head->y+5);
bar(head->x-4,head->y-4,head->x+4,head->y+4);
rectangle(p->x-5,p->y-5,p->x+5,p->y+5);
bar(p->x-4,p->y-4,p->x+4,p->y+4);
rectangle(last->x-5,last->y-5,last->x+5,last->y+5);
bar(last->x-4,last->y-4,last->x+4,last->y+4);
}

void step()
{
q=malloc(sizeof *q);
last->next=q;
q->x=last->x;q->y=last->y;q->next=NULL;
switch(way)
{
case 'l':q->x=last->x-10;if(q->x==r1&&q->y==r2)jiachang(1);break;
case 'r':q->x=last->x+10;if(q->x==r1&&q->y==r2)jiachang(2);break;
case 'd':q->y=last->y+10;if(q->x==r1&&q->y==r2)jiachang(3);break;
case 'u':q->y=last->y-10;if(q->x==r1&&q->y==r2)jiachang(4);break;
default:break;
}
last=q;
p=head;head=head->next;
setfillstyle(1,YELLOW);
bar(p->x-5,p->y-5,p->x+5,p->y+5);
free(p);
setfillstyle(1,BLUE);
rectangle(last->x-5,last->y-5,last->x+5,last->y+5);
bar(last->x-4,last->y-4,last->x+4,last->y+4);
}
void ifdie()
{
int waynum;
q=head;
if(last->x<60||last->x>580)gameover();
if(last->y>440||last->y<60)gameover();
switch(way)
{
case 'l':bianli(1);break;
case 'r':bianli(2);break;
case 'd':bianli(3);break;
case 'u':bianli(4);break;
default:break;
}
delay(50000);
}
void xianshi()
{
char st[20];
setcolor(GREEN);
rectangle(250,45,285,57);
rectangle(130,45,190,57);
setfillstyle(1,YELLOW);
bar(251,46,284,56);
bar(131,46,189,56);
sprintf(st,"%d",Long);
outtextxy(252,49,st);
sprintf(st,"%ld",Score);
outtextxy(132,49,st);
setfillstyle(1,BLUE);
setcolor(RED);
}
void bianli(int dd)
{
p=head;
while(p->next)
{
if(dd==1&&last->x-10==p->x&&last->y==p->y)
gameover();
if(dd==2&&last->x+10==p->x&&last->y==p->y)
gameover();
if(dd==3&&last->y+10==p->y&&last->x==p->x)
gameover();
if(dd==4&&last->y-10==p->y&&last->x==p->x)
gameover();
p=p->next;
}
}
void jiachang(int jj)
{
p=malloc(sizeof *p);
p->x=r1;p->y=r2;
if(jj==1)q->x=q->x-10;
if(jj==2)q->x=q->x+10;
if(jj==3)q->y=q->y+10;
if(jj==4)q->y=q->y-10;
last->next=p;
p->next=q;
eat=0;Long++;Score=Score+96+Long;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
深圳量子动力
2024-10-21 广告
表情捕捉软件可找深圳量子动力双目摄像系统可通过无线方式进行面部捕捉,可以满足在家、工作室、直播间、专业动捕棚等多种场景的不同使用人群。支持Metahuman Animator解算,支持升级animator高精度表情方案... 点击进入详情页
本回答由深圳量子动力提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式