由于单缓冲版(非局部绘制)闪屏严重且cls效率过差,双缓冲版应运而生
HDU/数字媒体技术/游戏程序设计作业
#include<iostream>
#include <math.h>
#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 = 40;
const int height = 20;
char ScreenData[width + 5][height + 5];
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
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.bVisible = 0;
cci.dwSize = 1;
SetConsoleCursorInfo(hOutput, &cci);
SetConsoleCursorInfo(hOutBuf, &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);
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
int textColor = 0X06;
SetConsoleTextAttribute(h, textColor);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
{
textColor = 0X0b;
SetConsoleTextAttribute(h, textColor);
cout << "#";
}
if (i == fruitY && j == fruitX)
{
textColor = 0X44;
SetConsoleTextAttribute(h, textColor);
cout << "F";
}
else if (i == y && j == x)
{
textColor = 0Xaa;
SetConsoleTextAttribute(h, textColor);
cout << "O";
}
else
{
bool flagPrint = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
textColor = 0Xaa;
SetConsoleTextAttribute(h, textColor);
cout << "o";
flagPrint = true;
}
}
textColor = 0X06;
SetConsoleTextAttribute(h, textColor);
if (!flagPrint)
cout << " ";
}
if (j == width - 1)
{
textColor = 0X0b;
SetConsoleTextAttribute(h, textColor);
cout << "#";
}
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "游戏得分:" << score << endl;
}
void Draw2()
{
WORD textColor = 0Xbb;
WORD textColor1 = 0X44;
WORD textColor2 = 0Xaa;
WORD textColor3 = 0X00;
WORD textColor4 = 0X11;
short i, j;
int currentLine = 0;
COORD coord1;
for (int j = 0; j < width; j++)
{
coord1.X = j;
coord1.Y = currentLine;
WriteConsoleOutputAttribute(hOutBuf, &textColor, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor, 1, coord1, &bytes);
ScreenData[currentLine][j] = '#';
}
currentLine++;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
if (j == 0)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = '#';
}
else if (i == fruitY && j == fruitX)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor1, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor1, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = 'F';
}
else if (i == y && j == x)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor4, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor4, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = '0';
}
else
{
bool flagPrint = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor2, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor2, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = 'o';
flagPrint = true;
}
}
if (!flagPrint)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor3, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor3, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = ' ';
}
}
if (j == width - 1)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = '#';
}
}
}
for (j = 0; j < width; j++)
{
coord1.X = j;
coord1.Y = currentLine + i;
WriteConsoleOutputAttribute(hOutBuf, &textColor, 1, coord1, &bytes);
WriteConsoleOutputAttribute(hOutput, &textColor, 1, coord1, &bytes);
ScreenData[currentLine + i][j] = '#';
}
currentLine++;
//sprintf_s(ScreenData[currentLine + i], "游戏得分:%d", score)
sprintf_s(ScreenData[currentLine + i], "游戏得分:%4d", 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':
{
if (dir == RIGHT)
dir = RIGHT;
else
dir = LEFT;
break;
}
case 'd':
{
if (dir == LEFT)
dir == LEFT;
else
dir = RIGHT;
break;
}
case 'w':
{
if (dir == DOWN)
dir == DOWN;
else
dir = UP;
break;
}
case 's':
{
if (dir == UP)
dir = UP;
else
dir = DOWN;
break;
}
case 'x':
gameOver = true;
break;
default:
break;
}
}
}
void Logic()
{
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
//if(x>width || x<0 || y>height||y<0)
// gameOver = true;
if (x >= width - 1) x = 1;
else if (x < 1)
x = width - 2;
if (y >= height) y = 0;
else if (y < 0)
y = height - 1;
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % (width -2);
fruitY = rand() % height;
nTail++;
}
for (int i = 0; i < nTail; i++)
{
if (tailX[i] == fruitX && tailY[i] == fruitY)
{
fruitX = rand() % (width - 2);
fruitX = rand() % height;
}
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
}
}
int Over()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
int textColor = 0X04;
SetConsoleTextAttribute(h, textColor);
cout << " _____ __ __ ______ ______ ________ _____" << endl;
cout << " / ____| /\\ | \\/ | ____| / __ \\ \\ / / ____| __ \\" << endl;
cout << " | | __ / \\ | \\ / | |__ | | | \\ \\ / /| |__ | |__) |" << endl;
cout << " | | |_ | / /\\ \\ | |\\/| | __| | | | |\\ \\/ / | __| | _ /" << endl;
cout << " | |__| |/ ____ \\| | | | |____ | |__| | \\ / | |____| | \\ \\" << endl;
cout << " \\_____/_/ \\_\\_| |_|______| \\____ / \\/ |______|_| \\_\\" << endl;
cout << endl;
textColor = 0X0b;
SetConsoleTextAttribute(h, textColor);
cout << "您的得分为:" << score << endl;
return 0;
}
int main()
{
Initial();
while (!gameOver)
{
//Draw();
Show_doublebuffer();
Input();
Logic();
Sleep(30);
}
Over();
return 0;
}