C语言——贪吃蛇

c语言实现贪吃蛇。

利用链表存储贪吃蛇坐标,利用光标跳转打印覆盖贪吃蛇。

#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"windows.h"
#include"time.h"
void enter();//进入界面
void over();//结束界面
void bawn();//围墙打印
void gotoprintf(int x,int y);//跳转打印 
void gotoxy(int x,int y);//光标跳转 
void gotodelete(int x,int y);//跳转删除
void producefood();//产生食物
int gitdirection();//获取方向
int judge();//游戏结束判断
void movesnake();//蛇移动
void eat();//蛇吃到食物后的变化
void changexy(int a,int b);//蛇的坐标的变化 
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor);//控制前景、背景色 
typedef struct Snake//蛇身 
{
	int x;
	int y;
	struct Snake * next;
}snake;
struct Food
{
	int x;
	int y;
};
snake *head=NULL,*tail=NULL;//蛇的头尾 
Food food;//食物位置 
int score = 0;//分数
int click=1;//方向 
int us;//停顿时间 

int main()
{
	enter();
	SetColor(11,0);
	bawn();
	producefood();
	if(gitdirection()==0) return 0;
	return 0;	
}
void enter()//进入 
{
	SetColor(0,10);
	int i = 0;
	for(;i<=100;i++)
	{
		printf(" ");
		printf("%d%%",i);
		Sleep(150-i);
		if(i<10)	printf("\b\b");
		else	printf("\b\b\b");
	}
	SetColor(4,0);
	printf("\n加载完成,即将进入...");
	Sleep(2000);
	system("cls");
	gotoxy(20,0);
	for(i=0;i<40;++i) printf("*");
	gotoxy(20,1);
	for(i=0;i<40;++i) printf("*");
	gotoxy(30,3);
	printf("任意键进入游戏"); 
	gotoxy(30,4);
	printf("方向键控制"); 
	gotoxy(20,6);
	for(i=0;i<40;++i) printf("*");
	gotoxy(20,7);
	for(i=0;i<40;++i) printf("*");
	gotoxy(20,8);
	system("pause");
}
void over()//结束界面
{
	system("cls");
	int i=0;
	gotoxy(20,0);
	for(i=0;i<40;++i)	printf("*");
	gotoxy(20,1);
	for(i=0;i<40;++i)	printf("*");
	gotoxy(30,3);
	printf("游戏 结束");
	gotoxy(30,4);
	printf("最终得分:%d 分",score);
	gotoxy(30,5);
	printf("还不错哟");
	gotoxy(20,7);
	for(i=0;i<40;++i)	printf("*");
	gotoxy(20,8);
	for(i=0;i<40;++i)	printf("*");
	printf("\n");
	system("pause");
}
void bawn()//围墙打印
{
	system("cls");
	int i;
	for(i=0;i<58;i+=2)
	{
		gotoprintf(i,0);
		gotoprintf(i,26);
	}
	for(i=1;i<26;++i)
	{
		gotoprintf(0,i);
		gotoprintf(56,i);
	}
	gotoxy(63,15);
	printf("分数:");
	gotoxy(77,15);
	printf("%d",score);
	head = (snake *)malloc(sizeof(snake));
	head->x=16;
	head->y=15;
	tail = (snake *)malloc(sizeof(snake));
	snake *p=(snake *)malloc(sizeof(snake));
	snake *q=(snake *)malloc(sizeof(snake));
	p->x=16;
	q->x=16;
	p->y=16;
	q->y=17;
	head->next=p;
	p->next=q;
	q->next=tail;
	tail->x=16;
	tail->y=18; 
	tail->next=NULL;
	gotoxy(16,14);
	printf("头");
 }
void gotoprintf(int x,int y)//跳转打印 
{
	gotoxy(x,y);
	printf("■");
}
void gotodelete(int x,int y)//跳转删除
{
	gotoxy(x,y);
	printf("  ");
}
void producefood()//产生食物
{
	srand((int)time(NULL));
	part:
	food.x=rand() % (54-2+1)+2;
	food.y=rand() % (25-1+1)+1;
	if(food.x%2!=0) food.x+=1;
	snake *judge=head;
	while(1)
	{
		if(judge->next==NULL) break;
		if(food.x == judge->x && food.y == judge->y)
		{
			goto part;
		}
		judge=judge->next;
	}
	gotoxy(food.x,food.y);
	printf("⊙"); 
}
int gitdirection()//获取方向
{
	while(1)
	{
		if(judge()==0) return 0;
		if(kbhit())
		{	
			getch();
			click = getch();
		}
		movesnake();
		eat();
	}
	return 1;
}
int judge()//游戏结束判断
{
	if(head->x==0||head->x==56||head->y==0||head->y==26)
	{
		over();
		return 0;
	}
	snake *p=head->next;
	while(1)
	{
		if(p->next==NULL) break;
		if(head->x==p->x&&head->y==p->y)
		{
			over();
			return 0;
		}
		p=p->next;
	}
	return 1;
}
void movesnake()//蛇移动
{
	int count = 0;
	int a = head->x;
	int b = head->y;
	snake *p=head;
	while(1)
	{
		if(p->next==NULL) break;
		gotodelete(p->x,p->y);
		count++;
		p=p->next;
	}
	switch(click)
	{
		case 72://上
			head->y-=1;
			changexy(a,b);
			break;
		case 80://下
			head->y+=1;
			changexy(a,b);
			break;
		case 75://左
			head->x-=2;
			changexy(a,b);
			break;
		case 77://右
			head->x+=2;
			changexy(a,b);
			break;
		default: break; 
	}
	p=head;
	while(1)
	{
		if(p->next==NULL) break;
		gotoprintf(p->x,p->y);
		p=p->next;
	}
	p=head;
	gotoxy(0,28);
	if(count<10) us=150;
	else if(count>=10&&count<20) us = 100;
	else us = 50;
	Sleep(us);
}
void eat()//蛇吃到食物后的变化
{
	if(head->x==food.x&&head->y==food.y)
	{
		producefood();
		snake *_new=(snake *)malloc(sizeof(snake));
		snake *p;
		p=head;
		while(1)
		{
			if(p->next->next==NULL) break;
			p=p->next;
		}
		p->next=_new;
		_new->next=tail;
		score+=10;
		gotoxy(77,15);
		printf("%d",score);
	}
}
void changexy(int a,int b)//蛇的坐标的变化 
{
	snake *p=head->next;
	int mid1,mid2,_mid1,_mid2;
	mid1=p->x;
	mid2=p->y;
	while(1)
	{
		if(p->next->next==NULL) break;
		_mid1=p->next->x;
		_mid2=p->next->y;
		p->next->x=mid1;
		p->next->y=mid2; 
		mid1=_mid1;
		mid2=_mid2;
		p=p->next;
	}
	p=head->next;
	p->x=a;
	p->y=b;
}
void gotoxy(int x, int y)//光标跳转 
{
    COORD pos;
    HANDLE hOutput;
    pos.X = x;
    pos.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, pos);
}
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)//控制前景、背景色 
{  
	HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);   
 	SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值