C Program to Draw Stars in Night Sky Using C Graphics

Here is the C program to draw stars in night sky using graphics.h header file. In this program, we will randomly select 500 pixels on screen and color them in while. We will use putpixel functions of graphics.h header file to color a pixel at (x, y).

C program to draw stars in night sky using graphics

In this program, we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). Then we will randomly select any (x, y) coordinate using rand, getmaxx and getmaxy function and color it using putpixel function. After 500 milliseconds we will clear screen and again paint the screen with stars until presses any key.

#include <graphics.h>
#include <dos.h>
#include <stdlib.h>

int main() {
    int gd = DETECT, gm;
    int i, x, y;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
       

 while (!kbhit()) {
      /* color 500 random pixels on screen */
   for(i=0; i<=500; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,15);
      }
      delay(500);

      /* clears screen */
      cleardevice();
    }

    closegraph();
    return 0;
}
Output Here is the screen shot of twinkling night sky animation. C graphics program to draw stars in night sky

Conclusion

In concluding this journey through the enchanting realm of Turbo C graphics, we've delved into the mesmerizing task of drawing stars in the night sky. Beyond the lines of code, we've discovered the seamless marriage of programming precision with the canvas of artistic expression. Turbo C, with its vintage allure, has been our steadfast companion in unraveling the magic of creative coding.

As you reflect on the stars that now twinkle on your screen, consider the vast universe of possibilities that graphics programming unveils. Each pixel placed is a testament to the fusion of imagination and logical constructs, creating a symphony of lights reminiscent of a starry night. Turbo C, like a timeless artist's brush, has allowed us to paint the night sky with the strokes of code.

Moving forward, let your creativity soar. Experiment with different constellations, add interactivity to make the stars dance, or explore the dynamic nature of the celestial canvas. The beauty of graphics programming lies in its ability to transcend the boundaries of the tangible and weave stories in pixels.

In the realm of creative coding, Turbo C stands as a silent maestro, guiding us through the subtle nuances of visual storytelling. As we bid adieu to this tutorial, let the stars you've drawn serve as a reminder of the limitless horizons awaiting your exploration. May your future endeavors in graphics programming continue to be as boundless and captivating as the night sky you've crafted. Happy coding, and may your code always be as luminous as the stars you've brought to life.


Related Topics
C program for moving car animation
C Program for bouncing ball animation
C program to draw sine wave graph on screen
C program to draw pie chart using graphics
C graphics program to draw digital clock
C Program to draw a circle on screen
C Program to draw bar graph on screen
List of all C Graphics Programs