C++实现贪吃蛇源码

C++实现贪吃蛇源码

大一刚入学,算是自学的小测验,虽然代码有点丑,但是自我感觉还是蛮好的哈哈哈,毕竟全是自己一个人从无到有写出来的。

#include<iostream>
#include<Windows.h>
#include<ctime>
#include<conio.h>
using namespace std;
int SCORE = 0;//初始化分数
int Head_X;//蛇头横坐标
int Head_Y;//蛇头纵坐标
int Food_X;//食物横坐标
int Food_Y;//食物纵坐标
int Map[50][50];//二维数组存储地图
int Body_X[50];//身体横坐标
int Body_Y[50];//身体纵坐标
char direction;//方向
char _direction;//方向镜像 防止误按 
void color(int a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);}//颜色
//隐藏光标
void hide_cursor()
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(handle, &CursorInfo);
}
//更新地图信息
void Update_Map()
{for (int i = 0; i < 50; i++)
	{for (int j = 0; j < 50; j++)
		{if (i == 0 || i == 49 || j == 0 || j == 49)
			{Map[i][j] = 1;}
			else
			{Map[i][j] = 0;}
		}
	}//初始化地图
	for (int i = 0; i < SCORE + 1; i++)
	{Map[Body_X[i]][Body_Y[i]] = 3;}//更新身体坐标
	Map[Head_X][Head_Y] = 2;//更新蛇头坐标
	Map[Food_X][Food_Y] = 4;//更新食物坐标
	Map[0][0] = 1;//修复bug
}
//打印地图
void print_Map()
{for (int i = 0; i < 50; i++)
	{for (int j = 0; j < 50; j++)
		{if (Map[i][j] == 1)
			{color(136);
				cout << "0 ";
				color(7);
			}//地图边缘
			else if (Map[i][j] == 2)
			{color(17);
				cout << "☆";
				color(7);
			}//蛇头
			else if (Map[i][j] == 3)
			{color(34);
				cout << "1 ";
				color(7);
			}//身体
			else if (Map[i][j] == 4)
			{color(13);
				cout << "o ";
				color(7);
			}//食物
			else if (Map[i][j] == 0)
			{cout << "  ";}//空地
		}
		cout << endl;
	}
}
//初始化坐标
void Head()
{Head_X = rand() % 48 + 1;
Head_Y = rand() % 48 + 1;}//初始化蛇头坐标
void Food()
{srand((unsigned int)time(NULL));
Food_X = rand() % 48 + 1;
Food_Y = rand() % 48 + 1;}//初始化以及刷新食物坐标
//挑选难度界面
int select_difficulty()
{
	cout << "请输入难度:(1-9)" << endl;
	int sel=0;
	while (sel < 1 || sel>9) cin >> sel;
	system("cls");
	return sel;
}
//判断是否死亡
bool is_Die()
{
	if (Head_X < 1 || Head_X>48 || Head_Y < 1 || Head_Y>48)
	{return true;}
	else
	{return false;}
}
//生长
void Group()
{
	if (SCORE == 0)
	{return;}
	Body_X[SCORE] = Food_X;
	Body_Y[SCORE] = Food_Y;
}
//更新身体信息
void update_Body()
{
	for (int i = SCORE; i > 0; i--)
	{
		if (i == 1)
		{
			Body_X[i] = Head_X;
			Body_Y[i] = Head_Y;
		}
		else if (i > 1)
		{
			Body_X[i] = Body_X[i - 1];
			Body_Y[i] = Body_Y[i - 1];
		}
	}
}
//游戏界面
void game_interface(int difficulty)
{
	while (!is_Die())
	{
		Update_Map();
		print_Map();
		_direction = direction;
		if (_kbhit())
		{direction = _getch();}//判断是否键入,并获取键入值
		if (direction == 'w')
		{
			update_Body();
			Head_X--;			
		}
		else if (direction == 'a')
		{
			update_Body();
			Head_Y--;
		}
		else if (direction == 's')
		{
			update_Body();
			Head_X++;			
		}
		else if (direction == 'd')
		{
			update_Body();
			Head_Y++;			
		}
		else if (direction != 'w' && direction != 'a' && direction != 's' && direction != 'd')
		{
			direction = _direction;
			system("cls");
			continue;
		}
		if (Head_X == Food_X && Head_Y == Food_Y)
		{
			Group();
			Food();
			SCORE++;
		}		
		Sleep((10-difficulty)*100);
		system("cls");
	}
	system("cls");
	cout << "游戏结束,你的分数为:" << SCORE << endl;
}
int main()
{
	system("mode con cols=100 lines=51");
	hide_cursor();
	int Difficulty = select_difficulty();
	Head();
	Food();
	game_interface(Difficulty);
	system("pause");
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值