2013-06-12 13 views
0

我写了一个小游戏,但我遇到了控制台问题。在游戏中,我们使用控制台将一些块打印到给定的坐标,因此我们可以使一些形状在屏幕上移动。但是现在我想在游戏结束时打印记分牌,当我使用控制台功能时,它会再次打印这些块,而不是我想要的文本。我该怎么办?控制台不打印我想要的字符,它为我的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 
+0

在你列出的代码中,我找不到你试图打印分数的地方,只是str1字符串。 numberofenemyships等打印在哪里? –

回答

1

这个片断:

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; 
SetConsoleTextAttribute(hConsoleOutput,color); 

此设置前景色为白色,背景颜色为白色。换句话说,你将它设置为白色打印白色。这就是为什么你得到“块”。

+0

非常感谢。我疯了,那种背景的事情从来没有发生过。 – DenizKara

+0

如果解决了您的问题,请接受答案(点击勾号)。 –

相关问题