Skip to content
Related Articles

Related Articles

Improve Article

Program to create a Ship using Graphics

  • Last Updated : 18 May, 2021

In Turbo C graphics the graphics.h functions are used to draw different shapes like circle, rectangle, etc, display text(any message) in a different format (different fonts and colors). By using graphics.h we can make programs, animations and also games. These can be useful for beginners.

Function Used:

  • line(a1, b1, a2, b2): A function from graphics.h header file which draws a line from (a1, b1) point to (a2, b2) point.
  • circle( a, b, r): A function from graphics.h header file which draws a circle with (a, b) as the center and r as radius.
  • setfillstyle(pattern, color): A function from graphics.h header file by which we can give a pattern of drawing and also a specific color.
  • floodfill(a, b, c): A function from graphics.h header file by which we can color in a specific bounded area with (a, b) as the center and c is the border color.

Approach: 

  • The first step is to color the background Cyan using the setfillstyle() and floodfill() functions.
  • Then, make a straight line using the line() function which will act as the Floor of the Ship.
  • The next step is to make other lines using the line() functions to make a complete ship.
  • Total four circles are implemented using the circle() function to implement Life Jackets. Here, a total of two Life Jackets is implemented. Color it white using setfillstyle() and floodfill() functions.
  • Then, make a Railing using same the line() function and color it Black using setfillstyle() and floodfill() functions.
  • Make a Cabin using the line() function and color it Light Gray using setfillstyle() and floodfill() functions.
  • Make three windows using the circle() function and color it white using setfillstyle() and floodfill() functions.
  • Then, make two chimneys using the line() functions and color it Black using setfillstyle() and floodfill() functions.
  • Final step is to make some design using the line() function and color it Red using setfillstyle() and floodfill() functions.

Below is the C program to implement the above approach:

C




// C program for the above approach
  
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
  
// Driver Code
void main()
{
    int gd = DETECT, gm;
  
    // Initialize of gdriver
    // with DETECT macros
    initgraph(&gd, &gm,
              "C:\\turboc3\\bgi");
  
    // Set Background Color Cyan
    setfillstyle(SOLID_FILL,
                 CYAN);
    floodfill(50, 50, 15);
  
    // Base Line
    line(300, 600, 1000, 600);
  
    // Lower Line
    line(290, 580, 1003, 580);
  
    // Upper Line
    line(285, 560, 1005, 560);
  
    // Left Tangent
    line(300, 600, 220, 400);
  
    // Right Tangent
    line(1000, 600, 1020, 450);
  
    // Connector Line
    line(220, 400, 1020, 450);
    setfillstyle(SOLID_FILL,
                 BLACK);
    floodfill(250, 420, 15);
    floodfill(350, 590, 15);
    setfillstyle(SOLID_FILL,
                 RED);
    floodfill(350, 570, 15);
  
    // First Life Jacket
    setfillstyle(SOLID_FILL,
                 WHITE);
    circle(400, 480, 30);
    circle(400, 480, 20);
    floodfill(375, 480, 15);
  
    // Second Life Jacket
    circle(500, 480, 30);
    circle(500, 480, 20);
    floodfill(475, 480, 15);
  
    // Ralling
    setfillstyle(SOLID_FILL,
                 BLACK);
    line(230, 400, 230, 370);
    line(225, 400, 225, 365);
    line(230, 370, 430, 385);
    line(225, 365, 435, 380);
    line(430, 385, 430, 415);
    line(435, 380, 435, 415);
    floodfill(227, 367, 15);
  
    // Cabin
    setfillstyle(SOLID_FILL,
                 DARKGRAY);
    line(410, 415, 410, 315);
    line(410, 315, 1000, 374);
    line(1000, 374, 1000, 450);
    floodfill(950, 390, 15);
    floodfill(425, 410, 15);
    setfillstyle(SOLID_FILL,
                 WHITE);
  
    // First Window
    circle(900, 400, 20);
    floodfill(890, 400, 15);
  
    // Second Window
    circle(820, 400, 20);
    floodfill(810, 400, 15);
  
    // Third Window
    circle(740, 400, 20);
    floodfill(730, 400, 15);
  
    // First Chimnney
    setfillstyle(SOLID_FILL,
                 BLACK);
    line(880, 363, 880, 280);
    line(880, 280, 910, 280);
    line(910, 280, 910, 365);
    floodfill(890, 290, 15);
  
    // Second Chimnney
    line(950, 369, 950, 255);
    line(950, 255, 980, 255);
    line(980, 255, 980, 374);
    floodfill(960, 265, 15);
  
    // Hold a screen for a
    // while
    getch();
  
    // Close the initialized
    /// gdriver
    closegraph();
}

Output:

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :