我正在尝试使用astrics“*”绘制一棵圣诞树,最后我能够绘制一张。
问题在于,当我使用textattr(130)对其进行着色时,“此颜色为绿色,闪烁”& cprintf功能树遍布整个屏幕。
我绘制了一条垂直线来查看cprintf的效果。
我对cprintf以外的任何其他方法都没问题,我只希望树能够正确显示并着色。
我的代码是:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
textattr(130);
cprintf("*"
"\n*"
"\n*"
"\n*"
"\n*"
"\n*"
"\n*"
"\n*"
"\n*"
"\n*");
cprintf(
"\n *"
"\n * *"
"\n * *"
"\n * *"
"\n * *"
"\n ** **"
"\n * *"
"\n * *"
"\n * *"
"\n ** **"
"\n * *"
"\n * *"
"\n * *"
"\n * *"
"\n ****** ******"
"\n * *"
"\n * *"
"\n * *"
"\n * *"
"\n ***");
getch();
return 0;
}
我希望有人愿意帮助我。
答案 0 :(得分:2)
我参加了你的计划并提到this并提出了以下计划。
#include <stdio.h>
#define RESET 0
#define BRIGHT 1
#define DIM 2
#define UNDERLINE 3
#define BLINK 4
#define REVERSE 7
#define HIDDEN 8
#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7
void textcolor(int attr, int fg, int bg);
void reset_screen(void);
void print_tree(void);
int main()
{
textcolor(BRIGHT, RED, BLACK);
print_tree();
getchar();
reset_screen();
return 0;
}
void textcolor(int attr, int fg, int bg)
{
char command[13];
sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
printf("%s", command);
}
void reset_screen(void)
{
system("reset");
return;
}
void print_tree(void)
{
printf(" \n");
printf(" \n");
printf(" * \n");
printf(" * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" ** ** \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" ** ** \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" ****** ****** \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * * \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" M E R R Y C H R I S T M A S \n");
printf(" \n");
printf(" \n");
return;
}
圣诞快乐!!
答案 1 :(得分:0)
您可以使用setcolor()函数来更改文本的颜色。以下示例程序说明了它的用法。
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int color, midx, midy;
char colname[35];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* for centering text on the display */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* get the current drawing color */
color = getcolor();
/* convert color value into a string */
itoa(color, colname, 10);
strcat(colname, " is the current drawing color.");
/* display a message */
outtextxy(midx, midy, colname);
/* clean up */
getch();
closegraph();
return 0;
}