C Mini Project: Animated Twinkle Star Using C Graphics. A twinkling star is the simplest of programs and you can do it in a snap using basic C graphics. This tutorial shows how to draw a twinkle star in a simple program that takes a single integer argument to define the diameter of the star. The tutorial also shows how to display a twinkle star as a 3D rotating object with an animated background, using simple C graphics.
The animation process starts by creating a window of the size that you want the twinkling star to be on your screen. Then, the window is set to a random value between 0 and 100. Once you have created your window, you will need to create two arrays, one that stores all of the frames and another that holds the values of the array. Finally, you will use the while loop to update the arrays in order to animate your twinkling star. Once you are finished, simply run the program again and enjoy the animated twinkling star on your computer.
#include
#include
#include
#include
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i, midx, midy;
/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s\n",
grapherrormsg(err));
return 0;
}
/* center position of the star */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/*
* draw a star and fill with different
* colors to get twinkling effect
*/
for (i = 1; i <= 15; i++) {
if (i == DARKGRAY)
continue;
setcolor(i);
setfillstyle(SOLID_FILL, i);
/* top portion of star */
line(midx, midy - 100, midx + 30, midy);
line(midx, midy - 100, midx, midy + 30);
line(midx + 30, midy, midx, midy + 30);
floodfill(midx + 1, midy + 1, i);
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
line(midx + 30, midy, midx + 140, midy);
line(midx + 30, midy, midx, midy + 30);
line(midx + 140, midy, midx, midy + 30);
floodfill(midx + 31, midy + 1, DARKGRAY);
/* right portion of star */
setcolor(i);
setfillstyle(SOLID_FILL, i);
line(midx + 140, midy, midx, midy + 30);
line(midx + 140, midy, midx + 40, midy + 50);
line(midx + 40, midy + 50, midx, midy + 30);
floodfill(midx + 40, midy + 48, i);
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
line(midx + 40, midy + 50, midx, midy + 30);
line(midx + 40, midy + 50, midx + 110, midy + 160);
line(midx + 110, midy + 160, midx, midy + 30);
floodfill(midx + 40, midy + 51, DARKGRAY);
/* bottom right portion of star */
setcolor(i);
setfillstyle(SOLID_FILL, i);
line(midx + 110, midy + 160, midx, midy + 30);
line(midx + 110, midy + 160, midx, midy + 90);
line(midx, midy + 90, midx, midy + 30);
floodfill(midx + 1, midy + 90, i);
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
line(midx, midy + 90, midx, midy + 30);
line(midx, midy + 30, midx - 110, midy + 160);
line(midx - 110, midy + 160, midx, midy + 90);
floodfill(midx - 1, midy + 90, DARKGRAY);
/* bottom left portion of star */
setcolor(i);
setfillstyle(SOLID_FILL, i);
line(midx, midy + 30, midx - 110, midy + 160);
line(midx - 40, midy + 50, midx - 110, midy + 160);
line(midx - 40, midy + 50, midx, midy + 30);
floodfill(midx - 38, midy + 50, i);
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
line(midx - 40, midy + 50, midx, midy + 30);
line(midx - 140, midy, midx - 40, midy + 50);
line(midx - 140, midy, midx, midy + 30);
floodfill(midx - 40, midy + 48, DARKGRAY);
/* left portion of star */
setcolor(i);
setfillstyle(SOLID_FILL, i);
line(midx - 30, midy, midx - 140, midy);
line(midx - 140, midy, midx, midy + 30);
line(midx - 30, midy, midx, midy + 30);
floodfill(midx - 30, midy + 1, i);
setcolor(DARKGRAY);
setfillstyle(SOLID_FILL, DARKGRAY);
line(midx - 30, midy, midx, midy + 30);
line(midx, midy - 100, midx - 30, midy);
line(midx, midy - 100, midx, midy + 30);
floodfill(midx - 28, midy, DARKGRAY);
/* sleep for 2 seconds */
sleep(2);
}
getch();
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}