C++控制台贪吃蛇

大二下的游戏程序设计第一个小作业,以此篇记录一下,感谢李仕老师的循循善诱和同学们的热情讨论。

#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

#define LeftBorder 3;
#define TopBorder 3;

bool gameOver,Died;					//游戏结束否
bool gamePause,fruitFlash;					//游戏暂停否
const int width = 50;			//游戏窗口宽度
const int height = 20;			//游戏窗口高度
int x, y, fruitX, fruitY;		//蛇头坐标;果子坐标
int score;						//得分
int level;
int tailX[100], tailY[100], nTail;//


enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };	//方向枚举
eDirection dir;					//方向变量


HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

//游戏初始化
void Initial()
{
	gameOver = false;			//游戏未结束
	dir = STOP;					//初始方向为停止
	x = width / 2;
	y = height / 2;				//当前蛇头初始位置:区域中心
	tailX[0] = x;
	tailY[0] = y;
	fruitX = (rand() % width) + 1;
	fruitY = (rand() % height) + 1;	//花的位置:随机产生
	score = 0;					//初始得分为0
	nTail = 1;					//
	level = 1;
	gamePause = false;
	fruitFlash = false;
	Died = false;
	CONSOLE_CURSOR_INFO cci;
	cci.bVisible = 0;
	cci.dwSize = 1;
	SetConsoleCursorInfo(h, &cci);
}

void setPos(int cursorX, int cursorY)
{
	COORD pos;
	pos.X = cursorX + LeftBorder;
	pos.Y = cursorY + TopBorder;
	SetConsoleCursorPosition(h, pos);
}

void DrawMap()
{
	system("CLS");

	int textColor = 0x06;
	SetConsoleTextAttribute(h, textColor);

	setPos(0, 0);
	for (int i = 0; i < width + 2; i++)
		cout << "#";

	for (int i = 1; i <= height; i++)
	{
		setPos(0, i);
		for (int j = 0; j < width + 2; j++)
		{
			if (j == 0)
				cout << "#";
			else if (j == width + 1)
				cout << "#";
			else
				cout << " ";
		}
		cout << endl;
	}
	setPos(0, height+1);
	for (int i = 0; i < width + 2; i++)
		cout << "#";

	setPos(tailX[0], tailY[0]);
	SetConsoleTextAttribute(h, 0x09);
	cout << "O";

	setPos(width + 10, 0);
	cout << "====欢迎来到我做的辣鸡贪吃蛇====";
	setPos(width + 14, 2);
	cout << "■规则:  1、自撞就算死亡";
	setPos(width + 14, 4);
	cout << "          2、可以自由穿墙";
	setPos(width + 14, 6);
	cout << "■控制:  1、WASD控制方向";
	setPos(width + 14, 7);
	cout << "          2、WASD任意键开始";
	setPos(width + 14, 8);
	cout << "          3、X键退出,P键暂停,R键重新开始";
	setPos(width + 14, 10);
	cout << "■作者:  杭电18级数媒夏健潇";

	setPos(width + 14, 15);
	cout << "■当前难度:  "<< level;
	setPos(width + 14, 17);
	cout << "■当前得分:  " << score;

}

//方向键输入
void Input()
{
	if (_kbhit())			//若有敲键
	{
		switch (tolower(_getch()))	//读取字符
		{
			//判断方向键
		case 'a'://if (dir == RIGHT) {
				break;
			}
			else {
				dir = LEFT;
				break;
			}
		case 'd'://if (dir == LEFT) {
				break;
			}
			else {
				dir = RIGHT;
				break;
			}
		case 'w'://if (dir == DOWN) {
				break;
			}
			else {
				dir = UP;
				break;
			}
		case 's'://if (dir == UP) {
				break;
			}
			else {
				dir = DOWN;
				break;
			}
		case 'x'://退出
			gameOver = true;
			setPos(width / 2, height + 3);
			cout << "按任意键关闭窗口";
			system("pause");
			break;
		case'p':
			gamePause = !gamePause;
			break;
		case'r':
			system("cls");
			Initial();
			DrawMap();
		default:
			break;
		}
	}
}

void Logic()
{
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width + 1;
		fruitY = rand() % height + 1;
		nTail++;
	}

	int preX = tailX[0];
	int preY = tailY[0];
	int pre2X, pre2Y;
	tailX[0] = x;
	tailY[0] = y;

	for (int i = 1; i < nTail; i++)
	{
		pre2X = tailX[i];
		tailX[i] = preX;
		preX = pre2X;
		pre2Y = tailY[i];
		tailY[i] = preY;
		preY = pre2Y;
	}


	//移动蛇头位置
	switch (dir)
	{
	case LEFT:
		x--;
		break;
	case RIGHT:
		x++;
		break;
	case UP:
		y--;
		break;
	case DOWN:
		y++;
		break;
	default:
		break;
	}

	if(level<10)
		level = score / 30;

	for (int i = 1; i < nTail; i++)
	{
		if (tailX[0] == tailX[i] && tailY[0] == tailY[i])
		{
			Died = true;
		}
	}
}

void EraseSnake()
{
	for (int i = 0; i < nTail; i++)
	{
		setPos(tailX[i], tailY[i]);
		cout << " ";
	}
}

void DrawLocally()
{
	if (!fruitFlash)
	{
		setPos(fruitX, fruitY);
		SetConsoleTextAttribute(h, 0x04);
		cout << "F";
		fruitFlash = true;
	}
	else
	{
		setPos(fruitX, fruitY);
		SetConsoleTextAttribute(h, 0x04);
		cout << " ";
		fruitFlash = false;
	}
	for (int i = 0; i < nTail; i++)
	{
		setPos(tailX[i], tailY[i]);
		if (i == 0)
		{
			SetConsoleTextAttribute(h, 0x09);
			cout << "O";
		}
		else
		{
			SetConsoleTextAttribute(h, 0x0a);
			cout << "o";
		}
	}
	
	//穿墙
	if (x < 1)
		x = width;
	if (x > width)
		x = 1;
	if (y < 1)
		y = height;
	if (y > height)
		y = 1;

	setPos(width + 28, 15);
	cout << level;
	setPos(width + 28, 17);
	cout << score;
}

//主控程序
int main()
{
	Initial();
	DrawMap();
	while (!gameOver)
	{
		Input();	
		if (!gamePause) 
		{
			EraseSnake();
			Logic();
			DrawLocally();
			Sleep(100 - level * 10);
		}
		while (Died)
		{
			setPos(width / 2, height + 3);
			cout << "按R键重新开始";
			Input();
		}
	}

	
	return 0;
}
发布了14 篇原创文章 · 获赞 26 · 访问量 3296

猜你喜欢

转载自blog.csdn.net/weixin_44338553/article/details/104708647