📜  使用C语言中的图形编程绘制棋盘

📅  最后修改于: 2021-05-25 22:43:31             🧑  作者: Mango

先决条件: graphics.h,如何在CodeBlocks中包含graphics.h?

在计算机图形学中,我们使用graphics.h提供直接功能来绘制不同的坐标形状(例如圆形,矩形等)。通过使用这些功能,我们可以绘制不同的对象,例如汽车,小屋,树木等。在此程序中,任务是使用图形中的功能绘制棋盘

要运行该程序,我们包含以下头文件:

#include 

方法:我们将使用以下功能创建一个国际象棋棋盘:

  1. 矩形(左,上,右,下): graphics.h头文件中的函数,用于绘制矩形。绘制矩形需要左上角和右下角的坐标。左侧指定左上角的X坐标,顶部指定左上角的Y坐标,右侧指定右下角的X坐标,底部指定右下角的Y坐标。
  2. delay():此函数存在于库“ dos.h”中,由于处理速度非常快,因此可将其用于保留程序输出一小段时间,因此请使用它来查看结果。
  3. setcolor(): graphics.h头文件中的函数,用于设置指针(光标)的颜色。计算机图形中有一些预定义的颜色。这里n是色号。
  4. setfillstyle(): graphics.h头文件中的函数,用于设置当前填充图案和填充颜色。
  5. floodfill(): graphics.h头文件中的函数,用于填充封闭区域。

以下是使用C语言中的图形绘制国际象棋棋盘的实现:

// C program to create a chess board
#include 
#include 
#include 
#include 
  
// Driver Code
void main()
{
    // Auto detection
    int gr = DETECT, gm;
    int r, c, x = 30, y = 30, black = 0;
  
    // Initialize graphics mode by passing
    // three arguments to initgraph function
  
    // &gdriver is the address of gdriver
    // variable, &gmode is the address of
    // gmode and  "C:\\Turboc3\\BGI" is the
    // directory path where BGI files are stored
    initgraph(&gr, &gm, "C:\\TURBOC3\\BGI");
  
    // Iterate over 8 rows
    for (r = 0; r < 8; r++) {
  
        // iterate over 8 cols
        for (c = 1; c <= 8; c++) {
  
            // If current block is to
            // color as black
            if (black == 0) {
  
                // set next color as white
                setcolor(WHITE);
  
                // sets the current fill
                // pattern and fill color
                // for black boxes
                setfillstyle(SOLID_FILL, BLACK);
  
                // creating rectangle
                // with length and breadth
                // with size 30
                rectangle(x, y, x + 30, y + 30);
  
                // Fill an enclosed area
                floodfill(x + 1, y + 1, WHITE);
  
                // Set black to true
                black = 1;
            }
  
            // If current block is to
            // color as white
            else {
                setcolor(WHITE);
  
                // sets the current fill
                // pattern and fill color
                // for whitw boxes
                setfillstyle(SOLID_FILL, WHITE);
  
                // creating rectangle
                // with length and breadth
                // with size 30
                rectangle(x, y, x + 30, y + 30);
  
                // Fill an enclosed area
                floodfill(x + 1, y + 1, WHITE);
  
                // Set black to false
                black = 0;
            }
  
            // Increment for next row
            x = x + 30;
  
            // delay function under library
            // "dos.h" for holding the
            // function for some time
            delay(30);
        }
        if (black == 0)
            black = 1;
        else
            black = 0;
  
        delay(30);
        x = 30;
        y = 30 + y;
    }
  
    // getch is used to hold the output screen
    // and wait until user gives any type of
    // input in turbo c
    getch();
  
    // close graph function is used to exit
    // from the graphics screen
    closegraph();
}

输出:
以下是上述程序的输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。