0

for my c++ object oriented programming class i wrote a small game but i am having a problem with console. In the game we used console to print some blocks to given coordinates thus we can make some shapes move around the screen. But now i want to print a Scoreboard when the game ends, and when i use console function it prints these blocks again, not the text i want. What should i do?

We are using Visual Studio 2010. From Configuration Properties->General we set our character set to "Use Multi-Byte Character Set".

Here is my Console Class:

#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();
}

And here is the header for it:

#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
4

1 回答 1

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

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

于 2013-06-12T15:27:31.377 回答