gpt4 book ai didi

c++ - 如何更改之前打印到屏幕上的 C++ 控制台中的字符

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:01 38 4
gpt4 key购买 nike

我正在上第二节 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] << "of "<< SUITS[rand() % 4] << endl;

我的实际代码将只显示一个框,其中一个字符用于显示面部,一个字符用于显示西装,打印时看起来像这样:

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

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


@regmagik- 它不会让我把代码放在注释中并且仍然格式化:这就是我现在拥有的:

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

最佳答案

这是在 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);

关于c++ - 如何更改之前打印到屏幕上的 C++ 控制台中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337481/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com