C语言用vs2017写一个简单的贪吃蛇游戏

  1. 源代码
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#define High 20
#define Wide 60
int speed = 0;  //定义一个全局变量控制小蛇蛇的速度
char name[100] = "游客选手"; //用户名称
void Snake_Menu()
{
	COORD pos;
	pos.X = 50;
	pos.Y = 10;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	printf("Dxs版贪吃蛇\n\n\n");
	printf("\t\t\t\t\t         请选择游戏难度:\n\n");
	printf("\t\t\t\t\t       简单模式请按键盘'1'\n\n");
	printf("\t\t\t\t\t       困难模式请按键盘'2'\n\n");
	printf("\t\t\t\t\t       噩梦模式请按键盘'3'\n\n");
	printf("\t\t\t\t\t       注册用户请按键盘'4'\n\n");
	printf("\t\t\t\t\t       退出游戏请按ESC键!\n");
}
void Register()
{
	system("cls");
	COORD pos;
	pos.X = 50;
	pos.Y = 10;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	printf("Dxs版贪吃蛇\n\n\n");
	printf("\t\t\t\t\t         请输入用户名称:");
	scanf("%s", name);
	printf("注册成功!请按任意键返回游戏!\n");
	char ch = _getch();
	system("cls");
}
void Draw_map()
{
	printf("\t\t\t贪吃蛇小游戏\n");
	for (int i = 0; i <= High; i++)
	{
		for (int j = 0; j <= Wide; j++)
		{
			if (i==0||j==0||i== High || j == Wide)
			{
				printf("&");
			}
			else {
				printf(" ");
			}		
		}
		if (i == 5)
		{
			printf("   作者:Dxs 日期:2019-8-3");
		}
		if (i == 6)
		{
			printf("   游戏按键提示如下:");
		}
		if (i == 7)
		{
			printf("   上:w W ↑ 8");
		}
		if (i == 8)
		{
			printf("   下:s S ↓ 2");
		}
		if (i == 9)
		{
			printf("   左:a A ← 4");
		}
		if (i == 10)
		{
			printf("   右:d D → 6");
		}
		if (i == 11)
		{
			printf("   暂停:space(空格键)");
		}
		printf("\n");
	}
}
typedef struct body
{
	int x;
	int y;
}BODY;
typedef struct Snake
{
	BODY list[Wide*High];
	int size;
	BODY food;
	int dx;
	int dy;
	int score;
	COORD coord;//光标信息
	BODY tail;
}Snake;
void init_food(Snake *snake)    //食物初始化
{
	srand((unsigned int)time(NULL));
	snake->food.x = rand() % (Wide-2)+1;
	snake->food.y = rand() % (High-2)+2;
}
void init_snake(Snake *snake)
{
	//蛇头初始化
	snake->list[0].x = Wide / 2;
	snake->list[0].y = High / 2;
	//蛇尾初始化
	snake->list[1].x = Wide / 2-1;
	snake->list[1].y = High / 2;

	//初始化蛇的大小
	snake->size = 2;
	snake->score = 0;
	snake->dx = 1;
	snake->dy = 0;
	init_food(snake);
}
//移动光标
void test()
{
	COORD pos;
	pos.X = 20;
	pos.Y = 30;
	//设置光标的位置
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

//隐藏光标
void hide_cur()
{
	//隐藏控制台的光标
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = sizeof(cci);
	cci.bVisible = FALSE;
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}

//4.打印蛇和食物
void Show_ui(Snake *snake)
{
	for (int i = 0; i < snake->size; i++)
	{
		//把蛇的位置坐标给光标的位置
		snake->coord.X = snake->list[i].x;
		snake->coord.Y = snake->list[i].y;
		//移动光标
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
		if (i == 0)
		{
			printf("@");
		}
		else
		{
			printf("*");
		}
	}
	//显示食物
	//把食物的坐标给光标
	snake->coord.X = snake->food.x;
	snake->coord.Y = snake->food.y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
	printf("$");
	//将原来的蛇尾打印为空。
	snake->coord.X = snake->tail.x;
	snake->coord.Y = snake->tail.y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
	printf(" ");
}

//移动蛇
void move_snake(Snake *snake)
{

	//记录蛇尾的坐标
	snake->tail.x = snake->list[snake->size - 1].x;
	snake->tail.y = snake->list[snake->size - 1].y;
	//更新蛇的身体坐标,除蛇头外
	for (int i = snake->size - 1; i > 0; i--)
	{
		//原来蛇头的位置赋值给蛇身,前面的蛇身位置赋值给后面的蛇身
		snake->list[i] = snake->list[i - 1];
	}
	//更新蛇头的位置
	snake->list[0].x += snake->dx;
	snake->list[0].y += snake->dy;
}

void Eat_food(Snake * snake)
{
	if (snake->list[0].x == snake->food.x&&snake->list[0].y == snake->food.y)
	{
		init_food(snake);
		snake->size += 1;
		//分数增加
		snake->score += 100;
	}
}
char key2 = 0;   //存储上一个按键值,通过两次按键比较防止小蛇可以反身撞击自己
void control_snake(Snake * snake)
{
	char key = 0;
	COORD pos;
	if (_kbhit())
	{
		key = _getch();
		
		switch (key)
		{
		case 'w':
		case 'W':
		case '8':
		case 72:
			if (key2 == 's' || key2 == 'S' || key2 == 80||key2=='2')
			{
				break;
			}
			else
			{
				snake->dx = 0;
				snake->dy = -1;
				key2 = key;
			}
			
			break;
		case 's':
		case 'S':
		case '2':
		case 80:
			if (key2 == 'w' || key2 == 'W' || key2 == 72||key2=='8')
			{
				break;
			}
			else
			{
				snake->dx = 0;
				snake->dy = 1;
				key2 = key;
			}
			
			break;
		case 'a':
		case 'A':
		case '4':
		case 75:
			if (key2 == 'd' || key2 == 'D' || key2 == 77||key2=='6')
			{
				break;
			}
			else
			{
				snake->dx = -1;
				snake->dy = 0;
				key2 = key;
			}
		
			break;
		case 'd':
		case 'D':
		case '6':
		case 77:
			if (key2 == 'a' || key2 == 'A' || key2 == 75||key2=='4')
			{
				break;
			}
			else
			{
				snake->dx = 1;
				snake->dy = 0;
				key2 = key;
			}
			
			break;
		case 32:
			pos.X = Wide/2-10;
			pos.Y = High/2;
			//设置光标的位置
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
			printf("游戏暂停!任意键继续!\n");
			_getch();
			system("cls");
			Draw_map();
			break;
		default:
			break;
		}
	}
	
}
//游戏结束
void game_over(Snake * snake)
{
	snake->coord.X = 0;
	snake->coord.Y = 10;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
	printf("            ####         #       ##     ##   ######         #####    #       #   ######   #######     ##\n"
		   "          ##            # #      ##     ##   #             #     #    #     #    #        #     ##    ##\n"
           "          ##  ####     #####     # #   # #   ######        #     #     #   #     ######   # ##        ##\n"
		   "          ##    ##    #     #    #  # #  #   #             #     #      # #      #        #   ##        \n"
           "           ######    #       #   #   #   #   ######         #####        #       ######   #     ##    ##\n");
	
	
	printf("\n\n\t\t您的分数为:%d分", snake->score);

	Sleep(10000);
	
	exit(0);
}
//蛇是否碰到自己
void snake_eat_body(Snake *snake)
{
	//如果蛇头碰到蛇身的任何一个,都算碰到自己,游戏结束
	for (int i = 1; i < snake->size; i++)
	{
		if (snake->list[0].x == snake->list[i].x&&snake->list[0].y == snake->list[i].y)
		{
			//游戏结束
			system("cls");
			game_over(snake);
		}
	}
}
void CurrentScoreAndUser(Snake* snake)
{
	COORD pos;
	pos.X = 64;
	pos.Y = 2;
	//设置光标的位置
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	printf("当前得分:%d\n", snake->score);
	pos.X = 64;
	pos.Y = 3;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	printf("当前用户:%s\n", name);
}

//5.游戏业务逻辑
void start_game(Snake *snake)
{
	init_snake(snake);
	move_snake(snake);
	Show_ui(snake);
	while (snake->list[0].x < Wide&&snake->list[0].y < High&&snake->list[0].x>=1&&snake->list[0].y>=2)
	{
		//1.控制蛇的方向
		control_snake(snake);
		//2。蛇移动,是不断的改变蛇的坐标和打印蛇
		move_snake(snake);
		Show_ui(snake);
		//更新蛇的坐标
		//打印蛇
		//3.蛇是否碰到自己
		snake_eat_body(snake);
		//4.蛇是否碰到食物(食物消失,产生新的事物)
		Eat_food(snake);
		CurrentScoreAndUser(snake);
		Sleep(speed);//控制蛇的速度
	}
	system("cls");
	game_over(snake);
}
int main(void)
{
    Loop:
	Loop2:
	hide_cur();
	Snake_Menu();
	char ss = _getch();
	switch (ss)
	{
	case '1':
		speed = 800;
		system("cls");
		break;
	case '2':
		speed = 400;
		system("cls");
		break;
	case '3':
		speed = 80;
		system("cls");
		break;
	case '4':
		Register();
		goto Loop2;
		system("cls");
		break;
	case 27:
		exit(0);
		break;
	default:
		printf("输入错误,请重新输入!");
		Sleep(2000);
		goto Loop;
		break;
	}
	Draw_map();
	//2.申请蛇的结构体空间
	Snake *snake = (Snake *)malloc(sizeof(Snake));
	start_game(snake);
	printf("\n");
	system("pause");
	return 0;
}

  1. 结果展示
    C语言用vs2017写一个简单的贪吃蛇游戏_第1张图片
    C语言用vs2017写一个简单的贪吃蛇游戏_第2张图片

你可能感兴趣的:(学习时光)