C++控制台贪吃蛇——双缓冲版本

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

#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

#define red_s coordTemp.Y = cLine;coordTemp.X = cCol;WriteConsoleOutputAttribute(hOutBuffer, &red, 1, coordTemp, &bytes);WriteConsoleOutputAttribute(hOutput, &red, 1, coordTemp, &bytes);
#define blue_s coordTemp.Y = cLine;coordTemp.X = cCol;WriteConsoleOutputAttribute(hOutBuffer, &blue, 1, coordTemp, &bytes);WriteConsoleOutputAttribute(hOutput, &blue, 1, coordTemp, &bytes);
#define yellow_s coordTemp.Y = cLine;coordTemp.X = cCol;WriteConsoleOutputAttribute(hOutBuffer, &yellow, 1, coordTemp, &bytes);WriteConsoleOutputAttribute(hOutput, &yellow, 1, coordTemp, &bytes);
#define green_s coordTemp.Y = cLine;coordTemp.X = cCol;WriteConsoleOutputAttribute(hOutBuffer, &green, 1, coordTemp, &bytes);WriteConsoleOutputAttribute(hOutput, &green, 1, coordTemp, &bytes);
#define white_s coordTemp.Y = cLine;coordTemp.X = cCol;WriteConsoleOutputAttribute(hOutBuffer, &white, 1, coordTemp, &bytes);WriteConsoleOutputAttribute(hOutput, &white, 1, coordTemp, &bytes);


const int width = 50;			//游戏窗口宽度
const int height = 20;			//游戏窗口高度

//====== 双缓冲变量 =====//
HANDLE hOutput, hOutBuffer;
COORD coord = { 0,0 }, coordTemp = {0,0};
DWORD bytes = 0;
bool flagBufferSwap = false;
char ScreenData[height + 5][width + 5];

//===== 游戏变量 =====//
bool gameOver;					//游戏结束否
int x, y, fruitX, fruitY;		//蛇头坐标;果子坐标
int  fruitX2, fruitY2;		//蛇头坐标;果子坐标
int score ;						//得分
int tailX[100], tailY[100], nTail;//
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };	//方向枚举
eDirection dir;					//方向变量

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);//改变字体颜色

//游戏初始化
void Initial()
{
	//缓冲区初始化
	 hOutBuffer = CreateConsoleScreenBuffer(
		GENERIC_WRITE,//定义进程可以往缓冲区写数据
		FILE_SHARE_WRITE,//定义缓冲区可以共享写入权限
		NULL,
		CONSOLE_TEXTMODE_BUFFER,
		NULL
	 );
	 hOutput = CreateConsoleScreenBuffer(
		 GENERIC_WRITE,
		 FILE_SHARE_WRITE,
		 NULL,
		 CONSOLE_TEXTMODE_BUFFER,
		 NULL
	 );
	 //隐藏缓冲区光标
	 CONSOLE_CURSOR_INFO cci;
	 cci.bVisible = 0;
	 cci.dwSize = 1;
	 SetConsoleCursorInfo(hOutBuffer, &cci);
	 SetConsoleCursorInfo(hOutput, &cci);



	gameOver = false;			//游戏未结束
	dir = STOP;					//初始方向为停止
	x = width / 2;
	y = height / 2;				//当前蛇头初始位置:区域中心
	fruitX = (rand() % width)+1;
	fruitY = (rand() % height)+1;	//花的位置:随机产生
	fruitX2 = (rand() % width)+1;
	fruitY2 = (rand() % height)+1;	//花的位置:随机产生
	score = 0;					//初始得分为0
}

void Draw()
{
	system("cls");
	int i;
	//画框1-顶
	for (i = 1; i <= width + 2; i++) 
	{
		cout << "#";
	}
	cout << endl;
	for (i = 1; i <= height; i++)
	{
		for (int j = 0; j <= width+1; j++)
		{
			//画框2-左
			if (j == 0)
			{
				cout << "#";
				continue;
			}
			//画框3-右
			if (j == width + 1)
			{
				cout << "#";
				cout << endl;
				continue;
			}


			if (i == fruitY && j == fruitX)//画果子
			{
				cout << "F";
				continue;
			}
			
			else if (i == y && j == x)	//画蛇头
			{
				cout << "O";
				continue;
			}
			else//不是墙的地方
			{
				bool f = false;
				for (int k = 0; k < nTail; k++) 
				{
					if (tailX[k] == j && tailY[k] == i)
					{
						f = true;
						cout << "o";
					}
				}
				if (!f)
				{
						cout << " ";		//背景调试
				}
			}
			
		}
		
	}
	//画框4-底
	for (i = 0; i < width + 2; i++) 
	{
		cout << "#";
	}
	cout << endl;
	cout << "游戏得分" <<score<< endl;
}

void Draw_Double_Buffer()
{
	//把内容先存放再ScreenDate数组中
	
	int i,j;//i行,j列
	int cLine = 0,cCol = 0;

	WORD red = 0x44;
	WORD blue = 0x01;
	WORD green = 0x02;
	WORD yellow = 0x06;
	WORD white = 0x0f;

	//画框1-顶
	for (cCol = 0; cCol <= width + 1; cCol++)
	{
		red_s
		ScreenData[cLine][cCol] = '#';
	}
	cLine++;

	for (; cLine <= height; )
	{
		for ( cCol = 0; cCol <= width + 1; cCol++)
		{
			//画框2-左
			if (cCol == 0)
			{
				red_s
				ScreenData[cLine][cCol] = '#';
				continue;
			}
			//画框3-右
			if (cCol == width + 1)
			{
				red_s	
				ScreenData[cLine][cCol] = '#';
				cLine++;
				continue;
			}


			if (cLine == fruitY && cCol == fruitX)//画果子
			{
				green_s
				ScreenData[cLine][cCol] = 'F';
				continue;
			}
			else if (cLine == fruitY2 && cCol == fruitX2)//画果子
			{
				green_s
				ScreenData[cLine][cCol] = 'F';
				continue;
			}
			else if (cLine == y && cCol == x)	//画蛇头
			{
				blue_s
				ScreenData[cLine][cCol] = 'O';
				continue;
			}
			else//不是墙的地方
			{
				bool f = false;
				for (int k = 0; k < nTail; k++)
				{
					if (tailX[k] == cCol && tailY[k] == cLine)
					{
						f = true;
						yellow_s
						ScreenData[cLine][cCol] = 'o';
					}
				}
				if (!f)
				{
					ScreenData[cLine][cCol] = ' ';
				}
			}

		}

	}
	//画框4-底
	for (cCol = 0; cCol < width + 2; cCol++)
	{
		red_s
		ScreenData[cLine][cCol] = '#';
	}
	cLine++;

	white_s
	sprintf_s(ScreenData[cLine], "游戏得分:%3d", score);
}

//双缓冲区轮播
void Show_Double_Buffer()
{
	int i;
	Draw_Double_Buffer();

	if (flagBufferSwap == false)
	{
		flagBufferSwap = true;
		for (i = 0; i < height + 5; i++)
		{
			coord.Y = i;
			WriteConsoleOutputCharacterA(hOutBuffer, ScreenData[i], width+2, coord, &bytes);
		}
		SetConsoleActiveScreenBuffer(hOutBuffer);
	}
	else
	{
		flagBufferSwap = false;
		for (i = 0; i < height + 5; i++)
		{
			coord.Y = i;
			WriteConsoleOutputCharacterA(hOutput, ScreenData[i], width+2, coord, &bytes);
		}
		SetConsoleActiveScreenBuffer(hOutput);
	}
}

//输入
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;
			break;
		default:
			break;
		}
	}
}

//控制方向键
void Logic()
{
	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 (x < 1)
		x = width;
	if (x > width)
		x = 1;
	if (y < 1)
		y = height;
	if (y > height)
		y = 1;


	//加分
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width+1;
		fruitY = rand() % height+1;
		nTail++;
	}
	if (x == fruitX2 && y == fruitY2)
	{
		score += 10;
		fruitX2 = rand() % width + 1;
		fruitY2 = rand() % height + 1;
		nTail++;
	}
	
	//死亡
	for (int i = 1; i < nTail; i++)
	{
		if (tailX[0] == tailX[i] && tailY[0] == tailY[i])
		{
			gameOver = true;
		}
	}
}

void ShowLose()
{
	system("cls");
	cout << "菜鸡,你输了" << endl;
}

//主控程序
int main()
{
	Initial();
	while (!gameOver)
	{
		//Draw();
		Show_Double_Buffer();
		Input();
		Logic();
		Sleep(100);
	}
	Sleep(500);
	ShowLose();
	return 0;
}
发布了14 篇原创文章 · 获赞 26 · 访问量 3295

猜你喜欢

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