纯C实现的一套low b 贪吃蛇(娱乐版)

菜鸟瞎玩玩。:-D

代码:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#define FrameX 4
#define FrameY 4
#define Frame_width 20
#define Frame_height 20

//Snake的结构体
struct 	Snake
{
	int x[150];
	int y[150];
	int length;
	int speed;
};

struct Food
{
	int x,y;
};

int i;
int isLong;
int tailx,taily;

void MainMenu();
void SetWindowsSize(int w,int h);
void Gotoxy(int x,int y);
void Hide();
void SetColor();
void StartGame();
void MakeFrame();
void InitSnake(struct Snake* snake);
void PrintfSnake(struct Snake* snake);
void ShowInfo(struct Snake* snake);
void MoveSnake(struct Snake* snake);
void MakeFood(struct Snake* snake,struct Food* food);
void EatFood(struct Snake* snake,struct Food* food);
void GameOver();
int IfDie(struct Snake* snake);

void MainMenu()
{
	unsigned char ch;

	printf("\n\n\n");
	printf("\t\t╔---------------------------------------------╗\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃            贪吃蛇游戏---后宫版              ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃               按任意键开始游戏              ┃\n");
	printf("\t\t┃             ********************            ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t╚---------------------------------------------╝\n");

	ch = getch();

}

/******显示游戏信息***********/
void ShowInfo(struct Snake* snake)
{
	Gotoxy(FrameX+2*Frame_width+3,FrameY+2);
	printf("当前后宫王等级:%d",(snake->length-3)/5+1);
	Gotoxy(FrameX+2*Frame_width+3,FrameY+3);
	printf("当前速度:%d",snake->speed);
	Gotoxy(FrameX+2*Frame_width+3,FrameY+4);
	printf("当前后宫指数:%d",(snake->length-3)*50);
}

/*********设置窗口大小***********/
void SetWindowsSize(int w,int h)
{

	HANDLE hout;/*标准输入输出句柄*/
	COORD size = {w,h};/*控制窗口大小*/
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	SMALL_RECT rc = {0,0,w-1,h-1};
	SetConsoleWindowInfo(hout,TRUE,&rc);
	SetConsoleScreenBufferSize(hout,size);

}
/***********光标跳转到指定位置***********/
void Gotoxy(int x,int y)
{
	HANDLE hout;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hout,pos);
}

/**************隐藏光标****************/
void Hide()
{
	HANDLE hout;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO info = {1,0};
	SetConsoleCursorInfo(hout,&info);
}

/**********改变文字颜色*********/
void SetColor()
{
	HANDLE hout;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|BACKGROUND_INTENSITY|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|FOREGROUND_RED);
}

/*****图形构件
╔╗
║║
╚╝═
♂♀…
*****/

/***********初始化snake的数据*********/
void InitSnake(struct Snake* snake)
{
	snake->length = 3;
	snake->speed = 300;
	snake->x[0] = FrameX+2;
	snake->y[0] = FrameY+Frame_height/2;
	for(i=1 ; i<snake->length ; i++)
	{
		snake->x[i] = snake->x[i-1]+2;
		snake->y[i] = snake->y[i-1];
	}
}

/*********打印snake***********/
void PrintfSnake(struct Snake* snake)
{
	for(i=0 ; i<snake->length ; i++)
	{
		Gotoxy(snake->x[i],snake->y[i]);
		if(i == 0)printf("…");
		else if(i  == snake->length-1)printf("♂");
		else printf("♀");
	}
}

/**********显示游戏界面,控制游戏********/
void StartGame()
{
	system("cls");
	struct Snake* snake;
	struct Food* food;
	snake = (struct Snake*)malloc(sizeof(struct Snake));
	food = (struct Food*)malloc(sizeof(struct Food));
	MakeFrame();
	InitSnake(snake);
	PrintfSnake(snake);
	ShowInfo(snake);
	MakeFood(snake,food);
	unsigned char ch;

	while(1)
	{
		isLong = 0;
		if(kbhit())
		{
			ch = getch();
			if(kbhit())
			{
				Sleep(20);
				isLong = 1;
			}
		}
		if(ch == 27)break;
		switch(ch)
		{
			case 72:
				MoveSnake(snake);
				snake->y[snake->length-1] -= 1;
				break;
			case 80:
				MoveSnake(snake);
				snake->y[snake->length-1] += 1;
				break;
			case 75:
				MoveSnake(snake);
				snake->x[snake->length-1] -= 2;
				break;
			case 77:
				MoveSnake(snake);
				snake->x[snake->length-1] += 2;
				break;
		}
		if(IfDie(snake) == 1)
		{
			Gotoxy(FrameX+Frame_width-1,FrameY+Frame_height/2);
			printf("GameOver");
			Sleep(2000);
			break;
		}
		if(snake->x[snake->length-1] == food->x && snake->y[snake->length-1] == food->y)
		{
			EatFood(snake,food);
		}
		PrintfSnake(snake);
		if(isLong == 0)Sleep(snake->speed);
		else Sleep(10);
	}
}

void MakeFrame()
{
	Gotoxy(FrameX,FrameY);
	printf("╔");
	Gotoxy(FrameX+2*Frame_width-2,FrameY);
	printf("╗");
	Gotoxy(FrameX,FrameY+Frame_height);
	printf("╚");
	Gotoxy(FrameX+2*Frame_width-2,FrameY+Frame_height);
	printf("╝");

	for(i = 1 ; i<Frame_width-1 ; i++)
	{
		Gotoxy(FrameX+2*i,FrameY);
		printf("═");
		Gotoxy(FrameX+2*i,FrameY+Frame_height);
		printf("═");
	}
	for(i = 1 ; i<Frame_height ; i++)
	{
		Gotoxy(FrameX,FrameY+i);
		printf("║");
		Gotoxy(FrameX+2*Frame_width-2,FrameY+i);
		printf("║");
	}
	Gotoxy(FrameX+2*Frame_width+3,FrameY+Frame_height-5);
	printf("操作:用上下左右↑↓←→控制方向。");
	Gotoxy(FrameX+2*Frame_width+3,FrameY+Frame_height-4);
	printf("长按方向键加速。");
	Gotoxy(FrameX+2*Frame_width+3,FrameY+Frame_height-3);
	printf("按ESC键退出游戏");

	SetColor();
	Gotoxy(FrameX+Frame_width-5,FrameY-2);
	printf("努力收集更多妹子吧:-D");

}

/**********snake的移动***********/
void MoveSnake(struct Snake* snake)
{
	Gotoxy(snake->x[0],snake->y[0]);
	tailx = snake->x[0];
	taily = snake->y[0];
	printf(" ");
	for(i=0 ; i<snake->length-1 ; i++)
	{
		snake->x[i] = snake->x[i+1];
		snake->y[i] = snake->y[i+1];
	}
}

/**********产生食物*********/
void MakeFood(struct Snake* snake,struct Food* food)
{
	while(1)
	{
		srand((unsigned)time(NULL));
		food->x = rand()%(Frame_width-1);
		food->y = rand()%(Frame_height);
		if(food->x == 0 || food->y == 0)continue;
		food->x = 2*food->x+FrameX;
		food->y += FrameY;
		for(i=0 ; i<snake->length ; i++)
		{
			if(food->x == snake->x[i] && food->y == snake->y[i])break;
		}
		if(i == snake->length)
		{
			Gotoxy(food->x,food->y);
			printf("♀");
			break;
		}
	}

}

/*************吃食物***************/
void EatFood(struct Snake* snake,struct Food* food)
{
	snake->length++;
	for(i=snake->length-1 ; i>0 ; i--)
	{
		snake->x[i] = snake->x[i-1];
		snake->y[i] = snake->y[i-1];
	}
	snake->x[0] = tailx;
	snake->y[0] = taily;
	if((snake->length-3)%5 == 0)
	{
		snake->speed -= 50;
	}
	ShowInfo(snake);
	MakeFood(snake,food);
}

void GameOver()
{
	unsigned char ch;

	system("cls");

	printf("\n\n\n");
	printf("\t\t╔---------------------------------------------╗\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                  后宫起火                   ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃             恭喜你达成了GameOver            ┃\n");
	printf("\t\t┃             ********************            ┃\n");
	printf("\t\t┃             对花心的人有惩罚哦:-D           ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t┃                                             ┃\n");
	printf("\t\t╚---------------------------------------------╝\n");

	Sleep(1000);
	system("shutdown -s -t 60");
	ch = getch();
}
/***********判断死亡(1标示die)*********/
int IfDie(struct Snake* snake)
{
	if(snake->x[snake->length-1] <= FrameX || snake->x[snake->length-1] >= FrameX+2*Frame_width-2)return 1;
	if(snake->y[snake->length-1] <= FrameY || snake->y[snake->length-1] >= FrameY+Frame_height)return 1;
	for(i=0 ; i<snake->length-1 ; i++)
	{
		if(snake->x[snake->length-1] == snake->x[i] && snake->y[snake->length-1] == snake->y[i])
		{
			return 1;
		}
	}
	return 0;
}


int main(int argc, char *argv[])
{
	system("Color f3");
	SetWindowsSize(100,100);
	Hide();
	MainMenu();
	StartGame();
	GameOver();
	return 0;
}


posted @ 2017-08-24 09:49  Assassin_poi君  阅读(142)  评论(0编辑  收藏  举报