我寫了一個小遊戲,但我遇到了控制檯問題。在遊戲中,我們使用控制檯將一些塊打印到給定的座標,因此我們可以使一些形狀在屏幕上移動。但是現在我想在遊戲結束時打印記分牌,當我使用控制檯功能時,它會再次打印這些塊,而不是我想要的文本。我該怎麼辦?控制檯不打印我想要的字符,它爲我的C++面向對象的編程類打印塊
我們使用的是Visual Studio 2010.從配置屬性 - >常規,我們將字符集設置爲「使用多字節字符集」。
這裏是我的控制檯類:
#include "console.h"
#include <iostream>
using namespace std;
Console::Console()
{
hConsoleOutput = CreateConsoleScreenBuffer(GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsoleOutput);
numberofastreoids = 0;
numberofenemyships = 0;
}
void Console::SetColor(int x, int y, int color)
{
DWORD NumberOfCharsWritten;
COORD coordinate;
coordinate.X = x;
coordinate.Y = y;
WriteConsoleOutputAttribute(hConsoleOutput, (WORD*) &color, 1, coordinate, &NumberOfCharsWritten);
}
void Console::PrintChar(int x, int y,char c)
{
DWORD NumberOfCharsWritten;
COORD coordinate;
coordinate.X = x;
coordinate.Y = y;
WriteConsoleOutputCharacter(hConsoleOutput, &c, 1, coordinate, &NumberOfCharsWritten);
}
void Console::UpdateScore(int i)
{
if(i==0)
numberofastreoids++;
if(i==1)
numberofenemyships++;
}
void Console::PrintScoreBoard()
{
char str1[] = "Number of Enemy Ships Destroyed: ";
unsigned long cChars;
WORD color;
WORD colorb=0;
WORD colorf=0;
colorb |= BACKGROUND_RED;
colorb |= BACKGROUND_GREEN;
colorb |= BACKGROUND_BLUE;
colorf |= FOREGROUND_RED;
colorf |= FOREGROUND_GREEN;
colorf |= FOREGROUND_BLUE;
color = colorb | colorf;
SetConsoleOutputCP(CP_UTF8);
SetConsoleTextAttribute(hConsoleOutput,color);
WriteConsole(hConsoleOutput,str1,strlen(str1),&cChars,NULL);
//cout << "Number of Enemy Ships Destroyed: " << numberofenemyships << endl;
//cout << "Total Score: " << (50*numberofenemyships)+(30*numberofastreoids) << endl;
getch();
}
這裏是它的標題:
#ifndef CONSOLE_H
#define CONSOLE_H
#include <conio.h>
#include <windows.h>
#include <string>
class Console
{
HANDLE hConsoleOutput;
int numberofastreoids;
int numberofenemyships;
public:
Console();
void SetColor(int x, int y, int color);
void PrintChar(int x, int y, char c);
void UpdateScore(int i);
void PrintScoreBoard();
};
#endif
在你列出的代碼中,我找不到你試圖打印分數的地方,只是str1字符串。 numberofenemyships等打印在哪裏? –