gpt4 book ai didi

c - 清屏后添加额外空间

转载 作者:行者123 更新时间:2023-11-30 15:13:36 25 4
gpt4 key购买 nike

我有以下代码:

#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>

#define RESET "\x1b[1J"

#define D "\x1b[0m"
#define Y "\x1b[33;1m"
#define W "\x1b[37;1m"
#define B "\x1b[30;1m"

void printLeft(bool color)
{
if (color) {
printf(Y " __\n"
" / |\n"
" / \\\n"
" / |\n"
W " _\\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \\ " Y "| | " W "/\n"
" --" Y "|" W "\\____/" Y "|" W "--\n"
Y " \\ |\n"
" \\ /\n"
" \\ |\n"
W " |" Y "\\__|" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" / |\n"
" / \\\n"
" / |\n"
" _\\| |[*][*]| |/_\n"
" \\ | | /\n"
" --|\\____/|--\n"
" \\ |\n"
" \\ /\n"
" \\ |\n"
" |\\__||\n"
" | |\n"
" ==| |==");
}
}

void printRight(bool color) {
if (color) {
printf(Y " __\n"
" | \\\n"
" / \\\n"
" | \\\n"
W " _\\| " Y "|" W "[" B "*" W "][" B "*" W "]" Y "| " W "|/_\n"
" \\ " Y "| | " W "/\n"
" --" Y "|" W "\\____/" Y "|" W "--\n"
Y " | /\n"
" \\ /\n"
" | /\n"
W " |" Y "|__/" W "|\n"
" | |\n"
" ==| |==");
} else {
printf(" __\n"
" | \\\n"
" / \\\n"
" | \\\n"
" _\\| |[*][*]| |/_\n"
" \\ | | /\n"
" --|\\____/|--\n"
" | /\n"
" \\ /\n"
" | /\n"
" ||__/|\n"
" | |\n"
" ==| |==");
}
}

void printBottom(bool color) {
if (color) {
printf(Y " __\n"
" | |\n"
" / \\\n"
" | |\n"
" |" W "[" B "*" W "][" B "*" W "]" Y"|\n"
" | |\n"
W " _\\| " Y "|" W "\\____/" Y "| " W "|/_\n"
" \\/" Y "| |" W "\\/\n"
Y " | |\n"
W " /\\" Y "\\ /" W "/\\\n"
" ==/ " Y "|__| " W "\\==");
} else {
printf(" __\n"
" | |\n"
" / \\\n"
" | |\n"
" |[*][*]|\n"
" | |\n"
" _\\| |\\____/| |/_\n"
" \\/| |\\/\n"
" | |\n"
" /\\\\ //\\\n"
" ==/ |__| \\==");
}
}

int main(int argc, char *argv[])
{
short counter = 0;
while (1) {
printf(RESET);
if (counter == 0) printLeft(true);
else if (counter == 2) printRight(true);
else printBottom(true);
if (true) {
printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"" D);
} else {
printf("\n\n\"IT'S PEANUT BUTTER JELLY TIME\"");
}
fflush(stdout);
usleep(200000);
counter = (counter + 1) % 4;
}
return 0;
}

除了一件事之外,一切都运转良好。第一行似乎添加了额外的空格:

extra space

如何去掉这个多余的空间?

最佳答案

虽然我无法使用系统上的控制代码进行测试,但您很可能在此文本后面需要一个 \n:

printf(Y "\n\n\"IT'S PEANUT BUTTER JELLY TIME\"\n" D);

至少,这解决了 Windows 系统上的相同问题(并且 YD 似乎是颜色控制代码,因此这不会影响位置无论如何)。

更新:

看起来像 position control code也存在(但无法在这里测试):

#define     GOTOYX   "\x1B[%.2d;%.2dH"
Set cursor to (y, x). More...

因此,您可以使用此代码来定位第一行,而不是额外的换行符。

关于c - 清屏后添加额外空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482743/

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