简易贪吃蛇V1.2

其实老早就写好了,结果忘了传上来,这个版本在蛇可以变长,增加了计分...

//
//V1.2
//
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

#define ShowTime 500
#define CheckTime 10
#define ShowRangeX 30//边界范围大小(横向)30
#define ShowRangeY 25//边界范围大小(纵向)25
#define ShowPlaceX 6//显示位置(横向)
#define ShowPlaceY 2//显示位置(纵向)
#define InfoShowPlaceX 70
#define InfoShowPlaceY 2
#define InfoShowRangeX 12
#define InfoShowRangeY 25
#define RIGHT	0x4d
#define LEFT	0x4b
#define UP		0x48
#define DOWN	0x50
#define	Left	1
#define Down	2
#define Up	3
#define Right	4 

void gotoxy(int x, int y);
void DrawBoard();//画边界
void DrawInitalSnake();//画蛇初始位置
void DrawFood();//画食物位置
int move(int direct);//移动
void HideCursor();//隐藏光标
void InitMap();
int isfood (int x,int y);//
void DrawInfo();
void DrawInfoBox();

int NowX=ShowPlaceX+ShowRangeX;
int NowY=ShowPlaceY+ShowRangeY/2;
int FoodX,FoodY;
int Head,Tail;
int DirectX[]={0,-1,0,0,1};
int DirectY[]={0,0,1,-1,0};
int MaxLen=ShowRangeX * ShowRangeY;
int SnakeBody[ShowRangeX * ShowRangeY][2];
bool Map[2*ShowRangeX][ShowRangeY];
int flg;
int Score;

int main(void)
{
	int key,time,next,pre;
	system("mode con cols=100 lines=30");//设置窗口大小
	system("title 简易贪吃蛇V1.0");
	system("color 8");//设置颜色 0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色  9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色
	DrawInfo();
	DrawInfoBox();
	HideCursor();//隐藏光标
	DrawBoard();//画边界
	InitMap();//初始化地图
	DrawInitalSnake();//画初始位置
	DrawFood();//画食物位置
	next=Right;//初始运动方向
	pre=Right;
	Score=0;
	while(1)
	{
		time=ShowTime/CheckTime;
		flg=0;
		while(time--)//多次检测键盘按键状态
		{	
			if(_kbhit())
			{
				key=getch();
				switch(key)
				{
				case LEFT :if(pre!=Right) next=Left;flg=1;break;
				case DOWN :if(pre!=Up)next=Down;flg=1;break;
				case UP :if(pre!=Down)next=Up;flg=1;break;
				case RIGHT :if(pre!=Left) next=Right;flg=1;break;
				}
			}
			Sleep(CheckTime);
			if(flg)
			{
				break;
			}
		}
		pre=next;
		fflush(stdin);//清空输入缓冲区
		while(time>=0)
		{
			Sleep(CheckTime);
			time--;
		}
		//system("cls");
		//DrawBoard();//重画边界
		if (move(next)==0) //移动
		{
			printf("Game Over !!!\n");
			system("pause");
			return 0;
		}
		
	}
return 0;
}

void gotoxy(int x, int y)
{
    COORD coord = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void DrawBoard()//画边界
{
	int i;
	gotoxy(ShowPlaceX-2,ShowPlaceY-1);
	printf("□");
	for(i=1;i<=ShowRangeX;i++)
	{
		printf("□");
	}
	printf("□");
	for (i=0;i<ShowRangeY;i++)
	{
		gotoxy(ShowPlaceX-2,ShowPlaceY+i);
		printf("□");
		gotoxy(ShowPlaceX+2*ShowRangeX,ShowPlaceY+i);
		printf("□");
		
	}
	gotoxy(ShowPlaceX-2,ShowPlaceY+ShowRangeY);
	printf("□");
	for(i=1;i<=ShowRangeX;i++)
	{
		printf("□");
	}
	printf("□");
	
}
void DrawInitalSnake()//画蛇的初始位置
{
	Head=0;Tail=0;//接下来这部分是建立初始的数组并画蛇初始位置
	SnakeBody[Head][0]=ShowPlaceX+ShowRangeX;
	SnakeBody[Head][1]=ShowPlaceY+ShowRangeY/2;
	gotoxy(SnakeBody[Head][0],SnakeBody[Head][1]);
	printf("■");
	Map[SnakeBody[Head][0]-ShowPlaceX][SnakeBody[Head][1]-ShowPlaceY]=1;//在地图上做标记

	SnakeBody[Head+1][0]=SnakeBody[Head][0]+2;
	SnakeBody[Head+1][1]=SnakeBody[Head][1];
	gotoxy(SnakeBody[Head+1][0],SnakeBody[Head+1][1]);
	printf("■");
	Map[SnakeBody[Head][0]-ShowPlaceX][SnakeBody[Head][1]-ShowPlaceY]=1;
	Head++;

	SnakeBody[Head+1][0]=SnakeBody[Head][0]+2;
	SnakeBody[Head+1][1]=SnakeBody[Head][1];
	gotoxy(SnakeBody[Head+1][0],SnakeBody[Head+1][1]);
	printf("■");
	Map[SnakeBody[Head][0]-ShowPlaceX][SnakeBody[Head][1]-ShowPlaceY]=1;
	Head++;
}
void DrawFood()//画食物位置
{
	srand((time_t )time(NULL));
	do 
	{
	FoodX=(rand()%ShowRangeX)*2+ShowPlaceX;
	FoodY=rand()%(ShowRangeY)+ShowPlaceY;
	}while(Map[FoodX][FoodY]);
	gotoxy(FoodX,FoodY);
	printf("■");
}

int move(int next)//移动
{
	int NextX=SnakeBody[Head][0]+2*DirectX[next];
	int NextY=SnakeBody[Head][1]+DirectY[next];
	if(NextX==ShowPlaceX-2||NextX==ShowPlaceX+2*ShowRangeX||NextY==ShowPlaceY-1||NextY==ShowPlaceY+ShowRangeY||Map[NextX-ShowPlaceX][NextY-ShowPlaceY]==1)
	{
			return 0;
	}
	else
	{
		if(!isfood(NextX,NextY))
		{
			//清除尾巴
			gotoxy(SnakeBody[Tail][0],SnakeBody[Tail][1]);
			printf("  ");
			Map[SnakeBody[Tail][0]-ShowPlaceX][SnakeBody[Tail][1]-ShowPlaceY]=0;
			Tail=(Tail+1)%MaxLen;
		}
		//向前移动
		Head=(Head+1)%MaxLen;
		gotoxy(NextX,NextY);
		if(Head==Tail) 
		{
			printf("你已经超神了!!!\n");
			return 0;
		}
		printf("■");
		SnakeBody[Head][0]=NextX;
		SnakeBody[Head][1]=NextY;
		Map[NextX-ShowPlaceX][NextY-ShowPlaceY]=1;
		if(isfood(NextX,NextY))
		{
			Score++;
			DrawInfo();
			DrawFood();
		}
	}
	return 1;
}

int isfood (int x,int y)
{
	if(x==FoodX && y==FoodY) return 1;
	return 0;
}

void InitMap()
{
	int i,j;
	for (i=0;i<2*ShowRangeX;i++)
		for (j=0;j<ShowRangeY;j++)
			Map[i][j];
}	
void HideCursor()//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = {1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

void DrawInfoBox()
{
	int i;
	gotoxy(InfoShowPlaceX-2,InfoShowPlaceY-1);
	printf("■");
	for(i=1;i<=InfoShowRangeX;i++)
	{
		printf("■");
	}
	printf("■");
	for (i=0;i<InfoShowRangeY;i++)
	{
		gotoxy(InfoShowPlaceX-2,InfoShowPlaceY+i);
		printf("■");
		gotoxy(InfoShowPlaceX+2*InfoShowRangeX,InfoShowPlaceY+i);
		printf("■");
		
	}
	gotoxy(InfoShowPlaceX-2,InfoShowPlaceY+InfoShowRangeY);
	printf("■");
	for(i=1;i<=InfoShowRangeX;i++)
	{
		printf("■");
	}
	printf("■");
}

void DrawInfo()
{
	gotoxy(InfoShowPlaceX,InfoShowPlaceY+1);
	printf("    你目前的分数是:");
	gotoxy(InfoShowPlaceX,InfoShowPlaceY+3);
	printf("           %d",Score);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值