2011-12-24 76 views
1

我想用astrics「*」畫一棵聖誕樹,最後我能畫出一棵。是否有任何替代cprintf?

問題是當我使用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; 
} 

我希望有人願意幫助我。

+1

替代方案是Curses,我想。 –

+0

或直接使用VT100轉義碼。 –

回答

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

聖誕快樂!

+0

非常感謝你 聖誕快樂你 – ALAA

+0

@ALAA如果你認爲上述帖子回答你的問題,請點擊勾號確認。謝謝!! –

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; 
} 
相關問題