【问题标题】:How to change a character in C++ console that was printed to the screen earlier如何在 C++ 控制台中更改之前打印到屏幕上的字符
【发布时间】:2014-12-06 22:49:55
【问题描述】:

我正在上第二节 C++ 课程,我必须为我的期末考试制作一个视频扑克游戏。 我想让我的游戏板打印到屏幕上并留在屏幕上,而只有改变的角色是唯一要改变的东西,同时保持游戏板的其余部分留在屏幕上的同一位置而不清除屏幕和重新打印一切。我在最后一堂课上制作的游戏更多,我使用了清晰的屏幕,它似乎闪烁了很多,因为每次进行更改时它都会重新打印整个屏幕。我没有把我所有的代码都放在这里,这样其他人就不会为我做我的项目,而是为我的问题编写了以下代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const char * SUITS[] = { "HEARTS", "CLUBS", "DIAMONDS", "SPADES" };
const char * FACES[] = { "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", 
            "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING" };

int main() {
    char q = 'a';
    do {
        srand(time(0));

        cout << "This is the card: " << endl;
        cout << FACES[rand() % 13] << " of " << SUITS[rand() % 4] << endl;
        cout << "Press any key to get another card, or q to quit: ";
        cin >> q;

    } while (q != 'q');
    return 0;
}

我怎样才能使上面唯一改变的是打印时的这一行: FACES[rand() % 13]

我的实际代码将只显示一个带有 char 的框来显示面部和一个 char 来显示西装,打印时它看起来像这样:

  ---------
 |         |
 |    A    |   (A is for Ace)
 |         |
 |    S    |   (S is for Spades)
 |         |
  ---------

如何只更改 A 或 S 而无需更改其他任何内容? 或者使用上面的代码,如何只更改 FACES 字符串和 SUITS 字符串而不更改其上方或下方的文本?


@regmagik- 它不会让我将代码放入 cmets 并使其仍然格式化: 这就是我现在拥有的:

const char SUITS[] = { 'H', 'C', 'D', 'S'};
const char FACES[] = { 'A','2','3','4','5','6','7','8','9','T','J','Q','K'};

int main() {
    DWORD dw;
    COORD here;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE)
    {
        printf("Invalid handle");
    }
    here.X = 10;
    here.Y = 10;

    //suit random num
    char suit_char = SUITS[rand() % 4];
    char face_char = FACES[rand() % 13];

    WriteConsoleOutputCharacter(hStdOut, &suit_char, 7, here, &dw);

    return 0;
}

我仍然收到一条错误消息,指出“char *”类型的参数与“LPCWSTR”类型的参数不兼容。如果我将 L 留在其中,则表示标识符“L”未定义。


@regmagik- 这是我更新的代码,它运行良好,但我希望用户的字符形式留在一个位置。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
#include <string>

using namespace std;

const TCHAR SUITS[] = { 'H', 'C', 'D', 'S' };
const TCHAR FACES[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
const TCHAR SPACE = ' ';
const int number_of_cards = 5;

int main() {
    char * line_of_stars = "*****************************************************\n";
    char * line_of_spaces = "*                                                   *\n";
    char * top_of_cards = "*  *******   *******   *******   *******   *******  *\n";
    char * card_sides = "* *       * *       * *       * *       * *       * *\n";
    char quit = 'q';
    //Display Screen
    cout << line_of_spaces << top_of_cards << card_sides << card_sides << card_sides
         << card_sides << card_sides << top_of_cards << line_of_spaces << line_of_stars;
    cout << "\nPress 'q' to quit or any other key to get new cards: ";

    DWORD dw;
    COORD suitCoord;
    COORD faceCoord;
    COORD nextMove;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE){ 
        printf("Invalid handle"); 
    }
    nextMove.X = 53;
    nextMove.Y = 11;
    do {
        for (int i = 0; i < number_of_cards; i++){

            int startX = 6 + (i * 10);
            int startY = 3;

            //Set coords for the i-nth card
            suitCoord.X = (startX);
            suitCoord.Y = (startY);
            faceCoord.X = (startX);
            faceCoord.Y = (startY + 2);
            //suit random num
            int rand1 = rand() % 4;
            TCHAR suit_char = SUITS[rand1];
            int rand2 = rand() % 13;
            TCHAR face_char = FACES[rand2];
            //Print to screen
            WriteConsoleOutputCharacter(hStdOut, &suit_char, 1, suitCoord, &dw);
            WriteConsoleOutputCharacter(hStdOut, &face_char, 1, faceCoord, &dw);
        }
        // Cover Last input with a space
        WriteConsoleOutputCharacter(hStdOut, &SPACE, 1, nextMove, &dw);
        cin.clear();
        cin >> quit;
        cout << "\b\b";
    } while (!(quit == 'q' || quit == 'Q'));

    return 0;
}

【问题讨论】:

标签: c++ string console character


【解决方案1】:

这是在 Visual Studio 2013 中编译的示例代码:

DWORD dw;
COORD here;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE)
{
    printf("Invalid handle");
}
here.X = 10;
here.Y = 10;
WriteConsoleOutputCharacter(hStdOut, L"My Text", 7, here, &dw);

【讨论】:

  • 我猜我需要#include 。我对这段代码不是很熟悉,你能给我解释一下吗?
  • 例如,我将如何显示 char 变量而不是“我的文本”。我应该用正常的 cout 做卡片(带有'-'和'|'的部分)然后有两个字符具有不同坐标的位置吗?
  • 要输出单个char ch='@';,您可以传递&amp;ch 而不是“我的文本”
  • 格式不够好,无法将我的代码放在 cmets 中,因此我将代码和问题添加到原始问题中。
  • 错误信息saying that argument of type "char *" is incompatible with the parameter of type "LPCWSTR"表示你的项目设置有UNICODE;将 char 的所有声明替换为 TCHAR 并用 TEXT 宏包装您的字符串文字:const TCHAR* SUITS[] = { TEXT("H"), TEXT("C"), TEXT("D"), TEXT("S")};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 2011-07-07
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多