Design Ludo Board Using Computer Graphics

03 Feb, 2022
In Turbo 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 graphics.h we can make programs, animations and also games. These can be useful for beginners.
Function Used:
rectangle(l, t, r, b): A function from graphics.h header file which draws a rectangle from left(l) to right(r) and from top(t) to bottom(b).
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 the radius.
outtextxy(int x, int y, char *string): A function from graphics.h header file by which we can print any statements where, x, y are the coordinates of the point and, the third argument contains the address of the string to be displayed.
settextstyle(int font, int direction, int font_size): A function from graphics.h header file by which we can make a style of the printable text where the font argument specifies the font of the text. The direction can be HORIZ_DIR (Left to right) or VERT_DIR (Bottom to top).
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 a specific bounded area with (a, b) as the center and c as the border color.
Approach:
In this program, nine functions are defined:red_corner()blue_corner()green_corner()yellow_corner()home()line_red_blue()line_green_yellow()line_red_green()line_yellow_blue()
red_corner()
blue_corner()
green_corner()
yellow_corner()
home()
line_red_blue()
line_green_yellow()
line_red_green()
line_yellow_blue()
The first step is to set the background color dark-gray by using the setfillstyle() and the floodfill() function.
Make two rectangles using the rectangle() function. Between them, one will act as the outer outline and another is the inner outline. Fill the color black in the space between the outer and inner outline.
The red_corner() function is called. Two rectangles are defined using the rectangle() function. Between them, one is the outer and another is an inner rectangle. The space between the outer rectangle is colored with red and the space between the inner rectangle is colored with white. Four circles are created using the circle() function and all the circles will be colored red. All the coloring will be implemented by using the setfillstyle() and the floodfill() functions.
Next step is to call blue_corner(), green_corner(), yellow_corner() functions sequentially.
The same thing has to be done in blue_corner(), green_corner(), yellow_corner() functions. But in the blue_corner() function the color blue is used, in the green_corner() function the color green is used, and in the yellow_corner() function the color yellow is used. The colors are filled by using setfillstyle() and floodfill() functions.
The next step is to call the home() function. In this function, two rectangles are implemented using the rectangle() function. Among them, one is the outer rectangle and another is the inner rectangle. The inner rectangle is colored black. There HOME is printed using the outtextxy(), settextstyle().
Call the line_red_blue() function. This function will draw the lines using the line() function. The space between the red and blue corners is divided into the same distanced line. Here a while loop will be implemented to draw the lines easily. Also, in this while loop, the code to color red will be implemented in the middle of that space. Also, a starting point needs to be created using a red color and a circle using the circle() function which is colored white. A Star Point was created which is also found on the real Ludo board. This is also implemented by the circle() function and color it White. In this function, decoration is done in the Home space by using red color. All the color is implemented by setfillstyle() and floodfill() functions.
Call line_green_yellow(), line_red_green(), line_yellow_blue() sequentially.
The same thing needs to be done in line_green_yellow(), line_red_green(), line_yellow_blue() functions. But in line_yellow_blue() the color blue is used, in line_red_green() the color green is used and in line_green_yellow() the color yellow is used. The colors are filled using setfillstyle() and floodfill() functions.
Below is the C program to implement the above approach:
C
// C program to implement// the above approach#include <conio.h>#include <graphics.h>#include <stdio.h> // Declaring Function used// in this programvoid red_corner();void blue_corner();void green_corner();void yellow_corner();void home();void line_red_blue();void line_green_yellow();void line_red_green();void line_yellow_blue(); // Driver Codevoid main(){    int gd = DETECT, gm;     // Initialize of gdriver with    // DETECT macros    initgraph(&gd, &gm, "C:\\turboc3\\bgi");     // Set Background Color    setfillstyle(SOLID_FILL, DARKGRAY);    floodfill(5, 5, 15);     // Main Outer Outline    rectangle(500, 200, 1250, 950);     // Main Inner Outline    rectangle(480, 180, 1270, 970);     // Coloring The Middle Space    setfillstyle(SOLID_FILL, BLACK);    floodfill(485, 185, 15);     // Calling red_corner() Function    red_corner();     // Calling blue_corner() Function    blue_corner();     // Calling green_corner() Function    green_corner();     // Calling yellow_corner() Function    yellow_corner();     // Calling home() Function    home();     // Calling line_red_blue() Function    line_red_blue();     // Calling line_green_yellow() Function    line_green_yellow();     // Calling line_red_green() Function    line_red_green();     // Calling line_yellow_blue() Function    line_yellow_blue();     // Holding The Screen For A While    getch();     // Close the initialized gdriver    closegraph();} void line_yellow_blue(){    int x = 950;     // Vertical Divisions    line(950, 600, 1250, 600);    line(950, 550, 1250, 550);     // Loop for creating horizontal    // line and coloring    while (x < 1250) {        line(x, 500, x, 650);        setfillstyle(SOLID_FILL, BLUE);        floodfill(x + 2, 555, 15);        x = x + 50;    }     setfillstyle(SOLID_FILL, DARKGRAY);    floodfill(1240, 555, 15);     // Start Point    setfillstyle(SOLID_FILL, BLUE);    floodfill(1195, 605, 15);    circle(1175, 625, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(1180, 630, 15);     // Star Point    circle(1125, 525, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(1130, 530, 15);     // Home Decoration    line(950, 650, 910, 600);    line(950, 500, 910, 550);    setfillstyle(SOLID_FILL, BLUE);    floodfill(925, 575, 15);} void line_red_green(){    int x = 500;     // Vertical Divisions    line(500, 600, 800, 600);    line(500, 550, 800, 550);     // Loop for creating horizontal    // line and coloring    while (x < 800) {        line(x, 500, x, 650);        setfillstyle(SOLID_FILL, GREEN);        floodfill(x + 2, 555, 15);        x = x + 50;    }     setfillstyle(SOLID_FILL, DARKGRAY);    floodfill(505, 555, 15);     // Start Point    setfillstyle(SOLID_FILL, GREEN);    floodfill(555, 505, 15);    circle(575, 525, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(580, 530, 15);     // Star Point    circle(625, 625, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(630, 630, 15);     // Home Decoration    line(800, 500, 840, 550);    line(800, 650, 840, 600);    setfillstyle(SOLID_FILL, GREEN);    floodfill(810, 575, 15);} void line_red_blue(){    int x = 650;     // Vertical Divisions    line(850, 650, 850, 950);    line(900, 650, 900, 950);     // Loop for creating horizontal    // line and coloring    while (x <= 900) {        line(800, x, 950, x);        setfillstyle(SOLID_FILL, RED);        floodfill(855, x + 2, 15);        x = x + 50;    }     setfillstyle(SOLID_FILL, DARKGRAY);    floodfill(855, 940, 15);     // Start point    setfillstyle(SOLID_FILL, RED);    floodfill(805, 895, 15);    circle(825, 875, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(830, 880, 15);     // Star Point    circle(925, 825, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(930, 830, 15);     // Home Decoration    line(800, 650, 840, 600);    line(950, 650, 910, 600);    setfillstyle(SOLID_FILL, RED);    floodfill(885, 615, 15);} void line_green_yellow(){    int x = 200;     // Vertical Divisions    line(850, 200, 850, 500);    line(900, 200, 900, 500);     // Loop for creating horizontal    // line and coloring    while (x < 500) {        line(800, x, 950, x);        setfillstyle(SOLID_FILL, YELLOW);        floodfill(855, x + 2, 15);        x = x + 50;    }     setfillstyle(SOLID_FILL, DARKGRAY);    floodfill(855, 205, 15);     // Start Point    setfillstyle(SOLID_FILL, YELLOW);    floodfill(905, 255, 15);    circle(925, 275, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(930, 280, 15);    // Star Point    circle(825, 325, 10);    setfillstyle(SOLID_FILL, WHITE);    floodfill(830, 330, 15);     // Home Decoration    line(800, 500, 840, 550);    line(950, 500, 910, 550);    setfillstyle(SOLID_FILL, YELLOW);    floodfill(885, 545, 15);} void red_corner(){    // Outer Line    rectangle(500, 650, 800, 950);     // Inner Line    rectangle(550, 700, 750, 900);     // Circles    circle(600, 750, 30);    circle(700, 750, 30);    circle(600, 850, 30);    circle(700, 850, 30);    setfillstyle(SOLID_FILL, RED);     // Rectangle Red    floodfill(505, 655, 15);     // Circle Red    floodfill(615, 765, 15);    floodfill(715, 765, 15);    floodfill(615, 865, 15);    floodfill(715, 865, 15);    setfillstyle(SOLID_FILL, WHITE);    floodfill(555, 705, 15);} void blue_corner(){    // Outer Line    rectangle(950, 650, 1250, 950);     // Inner Line    rectangle(1000, 700, 1200, 900);     // Circles    circle(1050, 750, 30);    circle(1150, 750, 30);    circle(1050, 850, 30);    circle(1150, 850, 30);    setfillstyle(SOLID_FILL, BLUE);     // Rectangle Blue    floodfill(955, 655, 15);     // Circle Blue    floodfill(1065, 765, 15);    floodfill(1165, 765, 15);    floodfill(1065, 865, 15);    floodfill(1165, 865, 15);    setfillstyle(SOLID_FILL, WHITE);    floodfill(1005, 705, 15);} void green_corner(){    // Outer Line    rectangle(500, 200, 800, 500);     // Inner Line    rectangle(550, 250, 750, 450);     // Circles    circle(600, 300, 30);    circle(700, 300, 30);    circle(600, 400, 30);    circle(700, 400, 30);    setfillstyle(SOLID_FILL, GREEN);     // Rectangle Green    floodfill(505, 215, 15);     // Circle Green    floodfill(615, 315, 15);    floodfill(715, 315, 15);    floodfill(615, 415, 15);    floodfill(715, 415, 15);    setfillstyle(SOLID_FILL, WHITE);    floodfill(555, 255, 15);} void yellow_corner(){    // Outer Line    rectangle(950, 200, 1250, 500);     // Inner Line    rectangle(1000, 250, 1200, 450);     // Circles    circle(1050, 300, 30);    circle(1150, 300, 30);    circle(1050, 400, 30);    circle(1150, 400, 30);    setfillstyle(SOLID_FILL, YELLOW);     // Rectangle Yellow    floodfill(955, 215, 15);     // Circle Yellow    floodfill(1065, 315, 15);    floodfill(1165, 315, 15);    floodfill(1065, 415, 15);    floodfill(1165, 415, 15);    setfillstyle(SOLID_FILL, WHITE);    floodfill(1005, 255, 15);} void home(){    // Outer Line    rectangle(800, 500, 950, 650);     // Inner Line    rectangle(840, 550, 910, 600);     // Coloring Middle Space Black    setfillstyle(SOLID_FILL, BLACK);    floodfill(860, 595, 15);     // Printing The Text 'HOME'    settextstyle(8, 0, 3);    outtextxy(848, 560, "HOME");}
Output:
LUDO BOARD
sumitgumber28
c-graphics
computer-graphics
C Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Producer Consumer Problem in C
Difference between break and continue statement in C
Exit codes in C/C++ with Examples
C Hello World Program
Handling multiple clients on server with multithreading using Socket Programming in C/C++
C program to find the length of a string
C / C++ Program for Dijkstra's shortest path algorithm | Greedy Algo-7
C Program for Tower of Hanoi
Conditional wait and signal in multi-threading