c++ - 控制台不打印我想要的字符,它打印一个 block

标签 c++ visual-studio-2010

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

我们使用的是 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

最佳答案

这段代码:

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);

这会将前景色设置为白色,将背景色设置为白色。换句话说,您将其设置为在白色上打印白色。这就是为什么您会遇到“障碍”。

关于c++ - 控制台不打印我想要的字符,它打印一个 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069183/

相关文章:

c++ - Hippo Mocks 中具有不同返回值的多个预期调用是否可以重复使用模拟?

c# - Microsoft Solver Foundation 变量限制

c++ - 用于筛选的最佳数据结构是什么(即一些数字被划掉的列表)?

c++ - 整数的字典序比较

c++ - 点云库 : How visualize a set of 3D point stored in a C++ <vector>?

c# - 在 VS2010 中将实体模型从一个项目复制到另一个项目

visual-studio-2010 - JS文件在VS 2010中不断崩溃

c++ - 在 QT 中以全屏模式播放视频

c++ - 是否可以将 QtConcurrent::run() 与类的函数成员一起使用

c++ - 'sizeof' 对不完整类型 'SDL_Window' 的无效应用