C program to draw a moving boat using graphics
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, we will discuss how to draw a moving boat in C using graphics.
- getmaxx(): The graphics.h header file includes the getmaxx() function, which returns the maximum X coordinate for the current graphics mode and driver.
- setcolor(N): The setcolor() function in the header file graphics.h is used to change the current drawing color to the new color.
- setlinestyle(linestyle, upattern, thickness): The setlinestyle() function in the header file graphics.h sets the style for all lines drawn by using functions like line, lineto, rectangle, drawpoly, and so on.
- 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.
- floodfill(pattern, color): The function is used to fill a confined space. To fill the area, the current fill pattern and color are used.
Approach: Follow the steps below to generate the moving boat:
- Pass three arguments to the initgraph() function to initialize the graphics driver and graphics mode.
- Initialize the boat’s position by considering two variables X and Y.
- Create a river/sea on which the boat will move by drawing a rectangle and fill it with light blue paint to make it look like a river/sea.
- Choose the coordinates so that the boat is just above the river/sea.
- Change the boat’s position using a loop continuously so that it appears to be moving in the river.
Below is the implementation of the above approach: