Talk C chestnuts together (82nd back: C language example-simple Christmas tree)
Hello, everyone. Today is Christmas Eve,First of all, I wish you a merry Christmas.Thank you for your attention to this chapter's novels. It's still the old saying, so it's time to stop talking. Let's talk C chestnuts together!
The example we are talking about today has no connection with the previous content,Let's prepare a simple Christmas tree today.. We used the curses library when creating the Christmas tree, so that simple graphics can be easily depicted in the window. The curses library is a simple graph library on Linux. I have briefly introduced the curses library in other blogs. If any of the readers do not know about the curses library, click here to view it, in order to have a simple understanding of the curses library.
The following describes how to create a Christmas tree.:
1. initialize the screen; 2. depicts the top layer of a Christmas tree. 3. next, draw the next layer. The coordinates of the layer are larger than those of the previous layer. 4. repeat the previous step to describe layer 6. The more layers, the larger the Christmas tree, you can customize the layers as needed; 5. depict the trunk; 6. Refresh the screen; 7. Release screen-related resources.
The code we wrote is as follows. For details, refer:
void show(){ initscr(); move(1,10); // show the top layer printw("%c",'/'); move(1,11); printw("%c",'\\'); move(2,9); // show the next layer printw("%c",'/'); move(2,12); printw("%c",'\\'); move(3,8); // show the next layer printw("%c",'/'); move(3,13); printw("%c",'\\'); move(3,7); // put the gift on the tree printw("%c",'@'); move(3,14); printw("%c",'*'); move(4,7); // show the next layer printw("%c",'/'); move(4,14); printw("%c",'\\'); move(4,6); // put the gift on the tree printw("%c",'*'); move(4,15); printw("%c",'@'); move(5,6); // show the next layer printw("%c",'/'); move(5,15); printw("%c",'\\'); move(6,5); // show the bottom layer printw("%c",'/'); move(6,16); printw("%c",'\\'); mvvline(5,10,'|',5); move(20,20); refresh(); sleep(1); endwin();}
Spectators,We will describe some details in the above Code.
When describing the content of each layer of the Christmas tree, we first use move to move the cursor to the corresponding position, and then use the printw function to output a symbol to represent the branches of the Christmas tree. We also used this method to add some "gifts" to the christmas tree. The symbols "@" and "*" are used in the code.
The mvvline function is used to plot the trunk of a Christmas tree.
The entire Christmas tree is in the pyramid shape. Therefore, we can calculate the coordinates of the Christmas tree, which I will not elaborate on.
The following is the running result of the program.(This is a static figure. You can run the program and the Christmas tree will flash)
ToCreate a flashing effect. We use the offset method to depict the Christmas tree.. The specific code is as follows:
for(i=0; i<9; ++i) { if(i%2 == 0) show(); else print(); }
In the code, show first depicts the Christmas tree, then shifts the coordinates of the Christmas tree to the right, and uses print to depict the Christmas tree again. So repeatedly, the flash effect is achieved.
The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it. When compiling a program, you need to use the l parameter to link the curses library. The specific command is:
gcc ChristmasTree.c -o s -lcurses
Let's talk about the example of a simple Christmas tree. Let's talk about the example and let's look at it again. Finally,Happy Christmas again!