C语言-------贪吃蛇(源代码)

#include
#include
#include //光标设置
#include //食物随机
#include //按键监控
#include
#pragma comment(lib,“winmm.lib”)

/////////////////////////////
// 辅助宏定义
#define MAPHEIGHT 25 //窗口属性
#define MAPWIDTH 60 //窗口属性
#define SNAKESIZE 200 //蛇最大节数
//数据设计
/////////////////////////////
// 食物结构体
struct
{
//坐标
int x;
int y;
}food;

/////////////////////////////
// 蛇的结构体
struct
{
int x[SNAKESIZE];
int y[SNAKESIZE];
int len; //蛇的长度
int speed; //蛇的移动速度
}snake;

int key=‘w’; //初始化移动方向
int changeFlag=0;//蛇的变化的标记
//模块化设计—》功能的划分—》抽象到函数
void drawMap();
void createFood();
void keyDown();//按键操作
int snakeStatus();//判断是否结束游戏
void gotoxy(int x,int y); //TC是有的,要自己实现

int main()
{

gotoxy(3*MAPHEIGHT,MAPHEIGHT/2-1);
printf("游戏时请打开Caps(大写)键");
system("pause");
char ch;
drawMap();
gotoxy(3*MAPHEIGHT,MAPHEIGHT/2);
printf("鼠标左键暂停,右键继续");
snake.speed=300;
while(1)
{ 
    createFood();
	snakeStatus();
	Sleep(snake.speed);
    keyDown();
	if(!snakeStatus())
		break;
}system("cls");
gotoxy(MAPWIDTH/2-15,MAPHEIGHT/2);
	printf("\t你好菜\t");
	system("pause");
	gotoxy(MAPWIDTH/2,MAPHEIGHT/2+1);
    printf("是否重新开始?(Y/N)");
	ch=getch();
	if(ch=='y'||ch=='Y')
		{system("cls");
	return main();}
	else
		return 0;

}

void drawMap()
{ //○:食物 ■:蛇身
srand((unsigned int)time(NULL));
//1.圈地范围
for(int i=0;i<=MAPHEIGHT;i++)
{
gotoxy(0,i);
printf(“■”);
gotoxy(MAPWIDTH,i);
printf(“■”);
}
//因为■占用两个字符
for(int i=0;i<=MAPWIDTH;i+=2)
{
gotoxy(i,0);
printf(“■”);
gotoxy(i,MAPHEIGHT);
printf(“■”);
}
//2.画蛇
snake.len=3;
snake.x[0]=MAPWIDTH/2;
snake.y[0]=MAPHEIGHT/2;
//蛇头
gotoxy(snake.x[0],snake.y[0]);
printf(“■”); //一节是x=2
for(int k=1;k {
snake.x[k]=snake.x[k-1]+2;
snake.y[k]=snake.y[k-1]+2;
gotoxy(snake.x[k],snake.y[k]);
printf(“■”);
}
//3.画食物
food.x=rand()%(MAPWIDTH+4)+2;
food.y=rand()%(MAPHEIGHT+2)+2;
gotoxy(food.x,food.y);
printf(“○”);
}
void createFood()
{

if(snake.x[0]==food.x&&snake.y[0]==food.y)
{

	srand((unsigned int)time(NULL));
	//产生的食物不能在蛇的身上,并且坐标是偶数
	while (1)
	{
		int flag=1;			
		food.x=rand()%(MAPWIDTH-4)+2;
		food.y=rand()%(MAPHEIGHT-4)+1;
		//产生的食物不能在蛇身上
		for(int k=0;k

}
void keyDown()//按键操作
{
//无按键的处理
if(_kbhit())
{
fflush(stdin);
key=_getch();
}
//擦出
if(!changeFlag)
{
gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf(" ");
}
//后面的蛇身
for(int i=snake.len-1;i>0;i–)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
//移动方向的处理
switch(key)
{
case ‘w’:
case ‘W’:
snake.y[0]–;
break;
case ‘s’:
case ‘S’:
snake.y[0]++;
break;
case ‘a’:
case ‘A’:
snake.x[0]-=2; //一个符号占用两个字符
break;
case ‘d’:
case ‘D’:
snake.x[0]+=2;
break;

}
gotoxy(snake.x[0],snake.y[0]);
printf("■");
changeFlag=0;
gotoxy(MAPHEIGHT+2,0);

}
int snakeStatus()//判断是否结束游戏
{
if(snake.x[0]==0||snake.x[0]==MAPHEIGHT-2||snake.x[0]==MAPWIDTH-2||snake.y[0]==0||snake.y[0]==MAPWIDTH-2||snake.y[0]==MAPHEIGHT)
return 0;
//蛇头不能撞自己
for(int k=1;k {
if(snake.y[0]==snake.y[k]&&snake.x[0]==snake.x[k])
{
return 0;
}
}
return 1;
}
void gotoxy(int x,int y) //TC是有的,要自己实现
{
//调用win32 API 去设置控制台的光标位置
//1.找到控制台的这个创建口
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
//2.光标的结构体
COORD coord;
//3.设置坐标
coord.X =x;
coord.Y =y;
//4.同步到控制台Set console Position
SetConsoleCursorPosition(handle,coord);

}

你可能感兴趣的:(C语言)