C语言贪吃蛇代码

# include<stdio.h>
# include<stdlib.h>
# include<time.h>
# include<ctype.h>
# include<conio.h>
# include<windows.h>
typedef struct NODE{
	struct NODE *next;
	int x;
	int y;
}Node;
char pch;
char cch;
char pause=0;
int foodx[2];
int foodflag=0;
int score=0;
Node *head;
int speed=400;
long now;
void gotoxy(int x,int y);
void eatfood(Node*head);
void displaymap();
void displaysnake(Node *head);
void insertnew(Node *head,int x,int y);
void movesnake1(Node*head);
void myclear(Node *head);
char button();
void food(int foodx[2]);
void diplayfood(int foodx[2]);
int test(Node *head);
void init();
void displaymessage();
void displayscoretime();
void gotoxy(int x,int y)
{
	COORD P;
	P.X=x;
	P.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),P);
}
void displaymap()
{
	unsigned i,j;
	gotoxy(10,0);
	for(j=0;j<50;j++)
		printf("*");
	for(i=1;i<25;i++)
	{
		gotoxy(10,i);
		for(j=0;j<50;j++)
		{
			if(j==0||j==49)
				printf("*");
			else
				printf(".");
		}
	}
	gotoxy(10,i);
	for(j=0;j<50;j++)
		printf("*");	
}
void diplaysnake(Node *head)
{
	Node*first=head;
	while(first!=NULL)
	{
		gotoxy(first->x,first->y);
		if(first==head)
			printf("#");
		else
			printf("@");
		first=first->next;
	}
}
void insertnew(Node *head,int x,int y)
{
	Node *new;
	Node *first=head;
	new=(Node*)malloc(sizeof(Node));
	new->x=x;
	new->y=y;
	while(first->next!=NULL)
		first=first->next;
	first->next=new;
	new->next=NULL;
}
void movesnake1(Node*head)
{
	int x1,y1,x2,y2,flag=0;
	x1=head->x;
	y1=head->y;
	switch(cch)
	{
		case 'w':if(pch=='a'||pch=='d'||pch==27||pch==26)
				 {
					 head->y-=1;
					 flag=1;
					 pch=cch;
				 }
				 break;
		case 'a':if(pch=='w'||pch=='s'||pch==24||pch==25)
				 {
					head->x-=1;
					flag=1;
					pch=cch;
				 }
				 break;
		case 's':if(pch=='a'||pch=='d'||pch==27||pch==26)
				 {
					head->y+=1;
					flag=1;
					pch=cch;
				 }
				 break;
		case 'd':if(pch=='w'||pch=='s'||pch==24||pch==25)
				 {
					head->x+=1;
					flag=1;
					pch=cch;
				 }
				 break;
		default:break;
	}
	if(flag!=1)
	{
		if(pch=='a'||pch==27)
			head->x-=1;
		else if(pch=='w'||pch==24)
			head->y-=1;
		else if(pch=='s'||pch==25)
			head->y+=1;
		else if(pch=='d'||pch==26)
			head->x+=1;
	}
	eatfood(head);
	head=head->next;
	while(head!=NULL)
	{
		x2=head->x;
		y2=head->y;
		head->x=x1;
		head->y=y1;
		x1=x2;
		y1=y2;
		head=head->next;
	}
}
void eatfood(Node*head)
{
	Node *new,*current;
	if((head->x==foodx[0])&&(head->y==foodx[1]))
	{
		new=(Node*)malloc(sizeof(Node));
		while(head->next!=NULL)
		{
			current=head;
			head=head->next;
		}
		head->next=new;
		new->x=current->x;
		new->y=current->y;
		new->next=NULL;
		score++;
		if((speed>=50)&&(score<=30)&&(score%6==5))
			speed-=50;
		else if((score<=50)&&(speed>=50))
			speed-=100;
		else if((speed<=50)&&(speed>=10))
			speed-=10;
		displayscoretime();
		food(foodx);
		diplayfood(foodx);
	}
}
void myclear(Node *head)
{
	while(head!=NULL)
	{
		gotoxy(head->x,head->y);
		printf(".");
		head=head->next;
	}
}
char button()
{
	char ch=0;
	if(_kbhit())
	{
		while(_kbhit())
			ch=getch();
	}
	return isalpha(ch)?tolower(ch):ch;
}
void food(int foodx[2])
{
	srand((unsigned)time(NULL));
	foodx[0]=rand()%48+11;
	foodx[1]=rand()%24+1;
}
void diplayfood(int foodx[2])
{
	gotoxy(foodx[0],foodx[1]);
	printf("&");
}
int test(Node *head)
{
	Node *first=head->next;
	while(first!=NULL)
	{
		if((first->x==head->x)&&(first->y==head->y))
		{
			return 0;
		}
		first=first->next;
	}
	return 1;
}
void init()
{
	CONSOLE_CURSOR_INFO ConsoleCursorInfo;//控制台屏幕不显示光标
	HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleCursorInfo(hConsoleOutput, &ConsoleCursorInfo);
	ConsoleCursorInfo.bVisible = FALSE;
    SetConsoleCursorInfo(hConsoleOutput, &ConsoleCursorInfo);
	
	head=(Node*)malloc(sizeof(Node));
	head->x=40;
	head->y=10;
	head->next=NULL;
	insertnew(head,41,10);
	insertnew(head,42,10);
	insertnew(head,43,10);
	insertnew(head,44,10);
	displayscoretime();
}
void displaymessage()
{
	time_t mytime;
	struct tm *mydate;
	time(&mytime);
	mydate=localtime(&mytime);
	gotoxy(70,4);
	printf("%d年%d月%d日",mydate->tm_year+1900,mydate->tm_mon+1,mydate->tm_mday);
	gotoxy(65,6);
	printf("#代表蛇头,@代表蛇身,&代表食物");
	gotoxy(65,8);
	printf("w代表上,s代表下,a代表左,d代表右");
	gotoxy(65,10);
	printf("按任意键开始游戏...");
}
void displayscoretime()
{
	gotoxy(65,12);
	printf("总得分为%d",score);
}
void main()
{
	init();
	displaymessage();
	food(foodx);
	foodflag=1;
	displaymap();
	diplayfood(foodx);
	diplaysnake(head);
	while(!_kbhit());
	getch();
	gotoxy(65,10);
	printf("空格键暂停游戏          ");
	pch=cch='a';
	now=clock();
	while((head->x>10)&&(head->x<60)&&(head->y>0)&&(head->y<25))
	{
		diplayfood(foodx);
		diplaysnake(head);
		_sleep(speed);
		myclear(head);
		cch=button();
		pause=(cch==' ')?!pause:pause;
		if(!pause)
		{
			gotoxy(65,10);
			printf("空格键暂停游戏          ");
			movesnake1(head);
		}
		else
		{
			gotoxy(65,10);
			printf("空格键继续游戏...          ");
		}
		if(!test(head))
			break;
		gotoxy(65,14);
		printf("游戏时间%ds",(clock()-now)/CLOCKS_PER_SEC);
	}
	gotoxy(30,12);
	printf("GAME OVER!!!");
	gotoxy(65,15);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值