控制台没有打印我想要的字符,它会打印一个块

时间:2013-06-12 15:18:13

标签: c++ visual-studio-2010

对于我的c ++面向对象编程类我写了一个小游戏,但我遇到了控制台问题。在游戏中我们使用控制台将一些块打印到给定的坐标,因此我们可以使一些形状在屏幕上移动。但现在我想在游戏结束时打印记分牌,当我使用控制台功能时,它再次打印这些块,而不是我想要的文本。我该怎么办?

我们正在使用Visual Studio 2010.从Configuration Properties-> General我们将字符集设置为“Use Multi-Byte Character Set”。

这是我的控制台类:

#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

1 个答案:

答案 0 :(得分: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);

这会将前景色设置为白色,将背景色设置为白色。换句话说,您将其设置为白色打印白色。这就是你获得“阻止”的原因。