1

我正在尝试使用 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;
 }

我希望有人愿意帮助我。

4

2 回答 2

2

我接受了您的程序并参考了这一点,并提出了以下程序。

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

圣诞节快乐!!

于 2011-12-25T03:48:56.347 回答
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;
}
于 2012-12-06T18:55:34.087 回答