HDU-保姆级游戏程序设计-双缓冲1

先更正一下墙体:
这边else和if的语句是没错的。
于是添加bool变量`

else 
			{
				bool FlagPrint = false;
				for (int k = 1; k < nTail; k++)
				{
					//todo:画尾巴
					if (tailX[k] == i && tailY[k] == j) 
					{
						cout << "o";
						FlagPrint = true;
					}
				}
				textColor = 0x06;
				SetConsoleTextAttribute(h, textColor);
				if (!FlagPrint)
					cout << " ";
			}

bool变量,打印o,bool返回值为true(1),打印空格时,返回为false(0)
画面基本正常

现在解决卡顿的问题。
双缓冲的原理可以这样形象的理解:把电脑屏幕看作一块黑板。首先我们在内存环境中建立一个“虚拟“的黑板,然后在这块黑板上绘制复杂的图形,等图形全部绘制完毕的时候,再一次性的把内存中绘制好的图形“拷贝”到另一块黑板(屏幕)上。采取这种方法可以提高绘图速度,极大的改善绘图效果。

#include<iostream>;
#include<windows.h>;
#include<conio.h>;
using namespace std;
HANDLE hOutput, hOutBuf;
COORD coord = { 0,0 };
DWORD bytes = 0;
bool BufferSwapFlag = false;


bool gameOver;
const int width = 20;
const int height = 20;
char ScreenData[width + 5][height + 5];
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail=1;
int i, j;
enum eDirection { STOP = 0,LEFT,RIGHT,UP,DOWN };
eDirection dir;


void Initial()
{//创建控制台屏幕缓冲区。
	hOutBuf = 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。控制台鼠标的信息的变量为cci		
	cci.bVisible = 0;//光标不可见
	cci.dwSize = 1;//光标尺寸为1
	SetConsoleCursorInfo(hOutput, &cci);
	SetConsoleCursorInfo(hOutput, &cci);

	gameOver = false;
	dir = STOP;
	x = width / 2;
	y = height / 2;
	fruitX = rand() % width;
	fruitY = rand() % height;
}
void Draw()
{
	system("cls");
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	int textColor = 0x06;
	SetConsoleTextAttribute(h, textColor);
	for (int i = 0; i < width+2; i++)
		cout << "1";
	cout << endl;
	for (int j = 0; j < height; j++)
	{
		for (int i = 0; i < width; i++)
		{
			if (i == 0)
				cout << "2";
			if (i == x && j == y) {
				textColor = 0X0a;
				SetConsoleTextAttribute(h, textColor);
				cout << "O";
			}
			else if (i == fruitX && j == fruitY)
			{
				textColor = 0x084;
				SetConsoleTextAttribute(h, textColor);
				cout << "F";
			}
			else 
			{
				bool FlagPrint = false;
				for (int k = 1; k < nTail; k++)
				{
					//todo:画尾巴
					if (tailX[k] == i && tailY[k] == j) 
					{
						cout << "o";
						FlagPrint = true;					
					}
				}
				textColor = 0x06;
				SetConsoleTextAttribute(h, textColor);
				if (!FlagPrint)
					cout << " ";	
			}			
			if (i == width - 1)
				cout << "3";
		}
		cout << endl;
	}
	for (i = 0; i < width+2; i++)
		cout << "4";
	cout << endl;
}
void Draw2()
{
	int i, j;
	int currentLine = 0;
	for (int i = 0; i < width + 2; i++)
		ScreenData[currentLine][j]=	'1';
//先把内容存在screendata数组,在通过双缓冲显示
	currentLine++;
	for (int j = 0; j < height; j++)
	{
		for (int i = 0; i < width; i++)
		{
			if (i == 0)
				ScreenData[currentLine+i][j]='2';
				
		    if (i == x && j == y) 
			{
				ScreenData[currentLine + i][j] = '0';
			}	
			else if (i == fruitX && j == fruitY)
			{
				ScreenData[currentLine + i][j] = 'F';
			}
			else
			{
				bool FlagPrint = false;
				for (int k = 1; k < nTail; k++)
				{
					//todo:画尾巴
					if (tailX[k] == i && tailY[k] == j)
					{
						ScreenData[currentLine + i][j] = 'o';
						FlagPrint = true;
					}
				}
	
				if (!FlagPrint)
					ScreenData[currentLine + i][j] = ' ';
			}
			if (i == width - 1)
				ScreenData[currentLine + i][j] = '3';
		}
	}
	for (i = 0; i < width + 2; i++)
		ScreenData[currentLine + i][j] = '4';
	currentLine++;
	sprintf_s(ScreenData[currentLine + i], "游戏得分:%d", score);
}

void Show_doubleBuffer() 
{
	int i;
	Draw2();
	if (BufferSwapFlag == false)
	{
		BufferSwapFlag = true;
		for (i = 0; i < height + 5; i++)
		{	
			coord.Y = i;
			WriteConsoleOutputCharacterA(hOutBuf, ScreenData[i], width, coord, &bytes);
		}
		SetConsoleActiveScreenBuffer(hOutBuf);
	}
	else
	{
		BufferSwapFlag = false;
		for (i = 0; i < height + 5; i++)
		{
			coord.Y = i;
			WriteConsoleOutputCharacterA(hOutput, ScreenData[i], width, coord, &bytes);
		}
		SetConsoleActiveScreenBuffer(hOutput);
	}
}
void Input()
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case'a':
			dir = LEFT;
			x--;	
			break;
		case'd':
			dir = RIGHT;
			x++;
			break;
		case'w':
			dir = UP;
			y--;
			break;
		case's':
			dir = DOWN;
			y++;
		
			break;
		case'x':
			gameOver = true;
			break;
		default:
			break;
		}
	}
}
void Logic()
{	
	for (i = nTail; i >= 1; i--)
	{
		tailX[i] = tailX[i - 1];
		tailY[i] = tailY[i - 1];
	}
	tailX[0] = x;
	tailY[0] = y;	
	cout << tailY[1];
	cout << tailX[1];
	for (int i  = 0; i < nTail; i++)
		if (tailX[i] == x && tailY[i] == y)
			gameOver == true;
	if (x >= width) x = 0;
	else if (x < 0) 
		x = width - 1;   
	if (y >= height)y = 0; 
	else if (y < 0) 
		y = height - 1;
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width;	
		fruitY = rand() % height;
		nTail++;
	}
}

int main()
{	
	Initial();
	while (!gameOver)
	{
	//	Draw();
	//	Input();
		Show_doubleBuffer();
	/*	if (fruitX > x)
			x++;
		else if(fruitX<x)
			x--;
		if(x==fruitX)
		if (fruitY >= y)
			y++;
		else
			y--;
			*/
		Input();
    	Logic();
	}
	system("pause");
	return 0;
}





按理说没啥问题,但是报错了在这里插入图片描述
键盘的wasd方向控制完全相反,并且系统提示无法打开
在这里插入图片描述
在写DRAW()中,其实遇到过这样的问题。所以应该是DRAW2()中出现了什么问题。不过屏幕确实不再闪烁,说明双缓冲设置成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值