Open In App

Draw a moving cycle using computer graphics programming in C/C++

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Functions used:

  • 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:

C++




// C++ program to draw the moving
// cycle using computer graphics
  
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>
  
// Driver code
int main()
{
    int gd = DETECT, gm, i, a;
  
    // Path of the program
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  
    // Move the cycle
    for (i = 0; i < 600; i++) {
        // Upper body of cycle
        line(50 + i, 405, 100 + i, 405);
        line(75 + i, 375, 125 + i, 375);
        line(50 + i, 405, 75 + i, 375);
        line(100 + i, 405, 100 + i, 345);
        line(150 + i, 405, 100 + i, 345);
        line(75 + i, 345, 75 + i, 370);
        line(70 + i, 370, 80 + i, 370);
        line(80 + i, 345, 100 + i, 345);
  
        // Wheel
        circle(150 + i, 405, 30);
        circle(50 + i, 405, 30);
  
        // Road
        line(0, 436, getmaxx(), 436);
  
        // Stone
        rectangle(getmaxx() - i, 436,
                  650 - i, 431);
  
        // Stop the screen for 10 secs
        delay(10);
  
        // Clear the screen
        cleardevice();
    }
  
    getch();
  
    // Close the graph
    closegraph();
}


Output:



Last Updated : 04 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads