#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
intfood_x,food_y;//é£ç©çåæ
structsneak
{
int x,y;
struct sneak *next;
};//èçç»æä½
voidSetpos(int i,int j)
{
COORD pos={i-1,j-1};
HANDLEhOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//å®ä½å¨ä»ä¹å°æ¹è¾åº
voidbeginsneak(struct sneak *head)
{
struct sneak *temp1,*temp2;
temp1=(structsneak*)malloc(sizeof(struct sneak));
temp2=(structsneak*)malloc(sizeof(struct sneak));
temp1->x=6;
temp1->y=5;
temp2->x=6;
temp2->y=4;
head->next=temp1;
temp1->next=temp2;
temp2->next=NULL;
}//åå§åèç身ä½
voidmap()
{
int i;
for(i=0;i<50;i++)
{
Setpos(i,1);
printf("-");
Setpos(i,25);
printf("-");
}
for(i=1;i<25;i++)
{
Setpos(1,i);
printf("|");
Setpos(50,i);
printf("|");
}
}//ç»å°å¾
voidfood(struct sneak *head)
{
struct sneak *p;
p=head;
int foodexist=0;//é£ç©æ è®°
srand((int)time(0));
while(foodexist==0)
{
food_x=rand()%48+2;
food_y=rand()%23+2;
while(p!=NULL)//å¤æé£ç©çææ¯å¦å¨èç身ä½é
{
if(p->x==food_x&&p->y==food_y)
{
foodexist=1;
break;
}
p=p->next;
}
if(foodexist==0)//å¦æ没æï¼åçæé£ç©
{
Setpos(food_x,food_y);
printf("ï¿¥");
break;
}
}
}
voidpaint(struct sneak *head)//ç»åºèç身ä½
{
struct sneak *p;
int ifhead=0; //å¤ææ¯å¦æ¯è头
p=head;
while(p!=NULL)
{
if(ifhead==0)//å¦ææ¯ï¼ç»åºè头
{
Setpos(p->x,p->y);
printf("#");
ifhead=1;
}
else//å
¶ä»é¨å
{
Setpos(p->x,p->y);
printf("*");
}
p=p->next;
}
}
structsneak *moveresult(struct sneak *head)//èç移å¨
{
struct sneak *new1;
new1=(structsneak*)malloc(sizeof(struct sneak));
static int direction=4;
char x;
if(_kbhit())
{
x=_getch();
if((x=='w'||x=='W')&&direction!=2)
direction=1;
if((x=='s'||x=='S')&&direction!=1)
direction=2;
if((x=='a'||x=='A')&&direction!=4)
direction=3;
if((x=='d'||x=='D')&&direction!=3)
direction=4;
while(_kbhit())
_getch();
}//æ§å¶æ¹å
switch(direction)//æ¯ç§»å¨ä¸æ¥é½è¦å¢å ä¸ä¸ªè身
{
case 1:
new1->x=head->x;
new1->y=head->y-1;
new1->next=head;
head=new1;
break;
case 2:
new1->x=head->x;
new1->y=head->y+1;
new1->next=head;
head=new1;
break;
case 3:
new1->x=head->x-1;
new1->y=head->y;
new1->next=head;
head=new1;
break;
case 4:
new1->x=head->x+1;
new1->y=head->y;
new1->next=head;
head=new1;
break;
}
return head;
}
intover(struct sneak *head)//å¤ææ¯å¦ç»æ
{
struct sneak *p;
p=head->next;
int gameover=0;
if((head->x)>=50||(head->x)<=1||(head->y)>=25||(head->y)<=1)//æå¢
{
gameover=-1;
}
while(p!=NULL)//æå°èªå·±
{
if((head->x==p->x)&&(head->y==p->y))
{
gameover=-1;
break;
}
p=p->next;
}
return gameover;
}
voiddecrease(struct sneak *head)//å åä¸ä¸ªè身
{
struct sneak *p,*pre;
p=head;
while(p->next!=NULL)
{
pre=p;
p=p->next;
}
Setpos(p->x,p->y);
printf(" ");
free(p);
pre->next=NULL;
}
voidgame()
{
system("cls");
Setpos(54,3);//å®ä½ã以åçä¹æ¯
printf("-è´ªåè游æ-");
int count=3;//è身çé¿åº¦
int speed=0;//å½åé度
struct sneak *head;//åå§åè头
head=(structsneak*)malloc(sizeof(struct sneak));
head->x=6;
head->y=6;
map();//å°å¾
beginsneak(head);//åå§åè
food(head);//åå§åé£ç©
paint(head);//ç»è身
while(1)
{
if(speed==9)//å¤ææ¯å¦éå
³
{
system("cls");
Setpos(30,7);
printf("666666.æåéå
³");
break;
}
Setpos(54,5);
printf("èçé¿åº¦ä¸ºï¼%d",count);
Setpos(54,7);
printf("å½åç级ï¼%d",speed);
head=moveresult(head);//移å¨èï¼åæ¶æ¯ä¸æ¬¡è身å ä¸
if(over(head)==-1)//æ¯å¦å¤±è´¥
{
system("cls");
Setpos(30,7);
printf("失败äº......");
Setpos(30,9);
printf("æç»é¿åº¦:%d",count);
break;
}
if(head->x==food_x&&head->y==food_y)//åå°é£ç©ï¼è身ä¸åä¸
{
count++;
food(head);
}
else//没åå°é£ç©ï¼è身åä¸
{
decrease(head);
}
paint(head);
speed=(int)(count/10);//å½åé度
Sleep(150-speed*15);
}
}
intmain()
{
printf("------------------ 欢è¿ç©å®¶ -----------------\n");
printf("------------------ w,a,s,dæ§å¶ --------------\n");
printf("------------------ è¾å
¥ä»»æé®å¼å§ -------------\n");
_getch();
game();
return 0;
}
温馨提示:答案为网友推荐,仅供参考