Draw a moving cycle using computer graphics programming in C/C++
In C graphics, the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using the functions in the header graphics.h, programs, animations, and different games can also be made. In this article, let’s discuss how to draw a moving cycle in C using graphics.
- line(x1, y1, x2, y2): It is a function provided by graphics.h header file to draw a line. Here x1, y1 is the first coordinates of the line, and x2, y2 are the second coordinates of the line respectively.
- circle(x, y, r): It is a function provided by graphics.h header file to draw a circle. The x, y are the center points of the circle and r is the radius of the circle.
- rectangle(X1, Y1, X2, Y2): It is employed in the creation of a rectangle. The rectangle must be drawn using the coordinates of the left top and right bottom corners. The X-coordinate and Y-coordinate of the top left corner are X1 and Y1 and the X-coordinate and Y-coordinate of the bottom right corner are X2 and Y2 respectively.
- delay(n): It is used to hold the program for a specific time period. Here n is the number of seconds you want to hold the program.
- cleardevice(): It is used to clear the screen in graphic mode. It sets the position of the cursor to its initial position, that is, (0, 0) coordinates.
- closegraph(): It is used to close the graph.
Approach: Following are the steps below to generate a moving cycle:
- Pass the three arguments to the initgraph() function to initialize the graphics driver and graphics mode.
- Create the upper body of the cycle by drawing lines.
- Create the wheels of the cycle by drawing circles and choose the coordinates so that the wheels aligned just below the upper body of the cycle.
- The next step is to create the road by drawing lines and stone by drawing rectangles.
- Choose the coordinates so that the cycle is just above the road.
- Change the cycle’s position using a loop continuously so that it appears to be moving on the road.
Below is the implementation of the above approach: