Introduction :
Creating graphics application in C or C++ programming language is always a fun and exciting project for developers. Using graphics.h library of Turbo C++ IDE, you can build various design, scenery and basic games. In this article, I shall show you how to create a beautiful scenery with house, sun, grass and tree in C or C++ programming language. Here, I draw the scenery with the help of functions of graphics.h library.
About the program :
This is a C or C++ graphics program that displays a house with a beautiful scenery. When you run the program, you can see a drawing of house, sun, grass and tree one by one on the graphics screen, First, it draws a house with walls, windows, a door etc. Then, it draws other parts of the scenery like sun, grass and tree on the screen. After completing the drawing, you have to press any key to stop the program.
Explanation of the program :
At the beginning, include graphics.h conio.h and dos.h libraries in the program. In the main() function, declare integer variables such as “xmax”, “ymax”, “x”, “y”, “i”, “graphic_driver” and “graphic_mode”. Then, use initgraph() function that initializes the graphics mode. You can use setcolor() function to change the drawing color of different parts of the house and scenery.
You can get the maximum x and y coordinate of the screen using getmaxx() and getmaxy() functions. Using line() function, draw the roof, walls, doors and windows of the house. The sun is drawn using circle() function. Using arc() function, you can draw grasses and trees. You have to use delay() function to create an animation effect. Finally, you can use getch() function to get a key press and use closegraph() function to stop the graphics mode.
How run the program :
First, open Turbo C++ IDE on your PC and create a new C or C++ file with .c or .cpp extension. If you have not installed Turbo C++ IDE, first install it on your PC. Now, copy the below source code and paste it in your C or C++ file that you have just created. If you do not know how to copy paste in Turbo C++, click here. You have to also know how to use graphics.h in Turbo C++ IDE.
Source code of the program :
The following source code shows you how draws a house with scenery using graphics.h library in C or C++ programming language. To use the source code of the program, Just copy the code and paste it in your project file.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int xmax, ymax, x, y, i;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setcolor(14);
xmax = getmaxx();
ymax = getmaxy();
x = xmax / 2;
y = ymax / 2;
line(x - 100, y - 100, x + 300, y - 100);
delay(200);
line(x + 300, y - 100, x + 200, y);
delay(200);
line(x + 200, y, x - 200, y);
delay(200);
for (i = 0; i < 400; i += 10)
{
line(x - 200 + i, y, x - 100 + i, y - 100);
setcolor(8);
delay(200);
}
setcolor(14);
line(x + 200, y, x + 200, y + 100);
delay(200);
line(x + 200, y + 100, x - 150, y + 100);
delay(200);
for (i = 0; i < 350; i += 10)
{
line(x - 150 + i, y + 100, x - 150 + i, y);
setcolor(5);
delay(200);
}
setcolor(14);
for (i = 0; i <= 75; i += 5)
{
line(x + 280 - i, y - 80 + i, x + 280 - i, y + 20 + i);
setcolor(10);
delay(200);
}
setcolor(14);
line(x + 280, y + 20, x + 200, y + 100);
delay(200);
for (i = 0; i < 30; i += 5)
{
line(x - 150, y + 35 + i, x - 120, y + 35 + i);
setcolor(7);
delay(200);
}
setcolor(14);
for (i = 0; i <= 30; i += 5)
{
line(x - 120 - i, y + 35, x - 120 - i, y + 65);
setcolor(7);
delay(200);
}
setcolor(14);
line(x - 120, y + 65, x - 150, y + 65);
delay(200);
for (i = 0; i < 30; i += 5)
{
line(x + 200, y + 35 + i, x + 170, y + 35 + i);
setcolor(7);
delay(200);
}
setcolor(14);
for (i = 0; i < 30; i += 5)
{
line(x + 170 + i, y + 35, x + 170 + i, y + 65);
setcolor(7);
delay(200);
}
setcolor(14);
line(x + 170, y + 65, x + 200, y + 65);
delay(200);
for (i = 0; i < 75; i += 5)
{
line(x, y + 25 + i, x + 50, y + 25 + i);
setcolor(3);
delay(200);
}
setcolor(14);
line(x + 50, y + 25, x + 50, y + 100);
delay(200);
for (i = 0; i < 50; i += 5)
{
line(x + i, y + 100, x + i, y + 25);
setcolor(3);
delay(200);
}
setcolor(14);
line(x + 50, y + 100, x + 50, y + 120);
delay(200);
line(x + 50, y + 120, x, y + 120);
delay(200);
for (i = 0; i <= 45; i += 5)
{
line(x + i, y + 120, x + i, y + 100);
setcolor(2);
delay(200);
}
setcolor(14);
line(x + 50, y + 120, x + 65, y + 135);
delay(200);
line(x + 65, y + 135, x + 15, y + 135);
for (i = 0; i <= 45; i += 5)
{
line(x + 15 + i, y + 135, x + i, y + 120);
setcolor(2);
delay(200);
}
setcolor(14);
line(x + 65, y + 135, x + 65, y + 155);
delay(200);
line(x + 65, y + 155, x + 15, y + 155);
for (i = 0; i <= 45; i += 5)
{
line(x + 15 + i, y + 155, x + 15 + i, y + 135);
setcolor(2);
delay(200);
}
setcolor(6);
for (i = 30; i >= 1; i--)
{
circle(x + 250, y - 150, 30 - i);
delay(200);
}
line(0, y + 50, x - 150, y + 50);
delay(200);
line(x + 253, y + 50, xmax, y + 50);
delay(200);
setcolor(10);
for (i = 40; i >= 1; i--)
{
arc(0, y + 50, 0, 90, 40 - i);
arc(80, y + 50, 0, 180, 40 - i);
arc(160, y + 50, 80, 180, 40 - i);
arc(xmax - 10, y + 50, 80, 180, 40 - i);
delay(200);
}
setcolor(8);
for (i = 30; i >= 1; i--)
{
arc(x - 700, y + 50, 0, 70, 450 + i);
arc(x + 150, y + 50, 90, 180, 350 + i);
delay(200);
}
setcolor(10);
for (i = 100; i >= 1; i--)
{
arc(0, 0, 270, 360, 100 - i);
arc(200, 0, 180, 360, 100 - i);
delay(200);
}
getch();
closegraph();
return 0;
}
Output of the program :
When you run the program on your PC, you can see the output of the program like below video on my YouTube channel.
Conclusion :
In this above article, you see how to create a scenery with house, sun, grasses and trees step by step using C or C++ graphics programming language. I think you understood the source code of the program. You can use the code in your project. Thank you for visiting my site.