📜  用图形绘制移动的船的C程序

📅  最后修改于: 2022-05-13 01:55:47.220000             🧑  作者: Mango

用图形绘制移动的船的C程序

在 C 图形中, graphics.h函数用于绘制不同的形状,如圆形、矩形等,以不同的格式(不同的字体和颜色)显示文本(任何消息)。通过使用头部 graphics.h 中的函数,还可以制作程序、动画和不同的游戏。在本文中,我们将讨论如何使用图形在 C 中绘制移动的船。

使用的功能:

  • getmaxx() graphics.h 头文件包含 getmaxx()函数,该函数返回当前图形模式和驱动程序的最大X坐标。
  • setcolor(N) 头文件 graphics.h 中的setcolor()函数用于将当前绘图颜色更改为新颜色。
  • setlinestyle(linestyle, uppattern, Thickness):头文件 graphics.h 中的 setlinestyle()函数设置所有使用 line、lineto、rectangle、drawpoly 等函数绘制的线条的样式。
  • rectangle(X1, Y1, X2, Y2) 它用于创建矩形。必须使用左上角和右下角的坐标绘制矩形。左上角的X坐标和Y坐标分别是X1Y1 ,右下角的X坐标和Y坐标分别是X2Y2
  • floodfill(pattern, color) 该函数用于填充密闭空间。要填充该区域,将使用当前的填充图案和颜色。

方法:按照以下步骤生成移动的船:

  • 将三个参数传递给initgraph()函数以初始化图形驱动程序和图形模式。
  • 通过考虑两个变量XY 来初始化船的位置。
  • 通过绘制一个矩形来创建一条河流/海洋,船将在其上移动,并用浅蓝色油漆填充它,使其看起来像一条河流/海洋。
  • 选择坐标,使船正好位于河流/海洋之上。
  • 使用循环不断改变船的位置,使其看起来在河中移动。

下面是上述方法的实现:

C
// C program to draw the moving boat
// using c graphics
  
#include 
#include 
#include 
#include 
  
// Driver Code
int main()
{
    // Initialize graphic driver
    int gdriver = DETECT, gmode, err;
    int i = 0, j, x, y, x1, y1, x2, y2;
  
    // Start graphics mode by passing
    // three arguments to initgraph()
  
    // &gdriver is the address of the
    // gdriver variable.
  
    // &gmode is the address of gmode
  
    // "C:Turboc3BGI" is the directory
    // path where BGI files are stored
    initgraph(&gdriver, &gmode,
              "C:\\Turboc3\\BGI");
    err = graphresult();
  
    if (err != grOk) {
        printf("Graphics Error: %s\n",
               grapherrormsg(err));
  
        return 0;
    }
  
    j = 0;
  
    // Initialize position for boat
    x = 50, y = getmaxy() / 2 + 140;
  
    while (x + 60 < getmaxx()
           && (!kbhit())) {
  
        // Set the positions for rain
        x1 = 10, i = y1 = 0;
        x2 = 0, y2 = 50;
  
        // Clears graphic screen
        cleardevice();
  
        // Set the color of river/sea
        setcolor(LIGHTBLUE);
        setlinestyle(SOLID_LINE, 1, 1);
        setfillstyle(SOLID_FILL,
                     LIGHTBLUE);
  
        // Draw the river/sea
        rectangle(0, getmaxy() / 2 + 150,
                  getmaxx(), getmaxy());
        floodfill(getmaxx() - 10,
                  getmaxy() - 10,
                  LIGHTBLUE);
  
        // Rain drops
        setlinestyle(DASHED_LINE, 1, 2);
  
        while (i < 700) {
            line(x1, y1, x2, y2);
            x1 = x1 + 20;
            y2 = y2 + 50;
            i++;
        }
  
        // Drawing the boat
        setlinestyle(SOLID_LINE, 1, 2);
        setcolor(BROWN);
  
        setfillstyle(SOLID_FILL, BROWN);
        sector(x, y, 180, 360, 50, 10);
  
        setcolor(DARKGRAY);
        setlinestyle(SOLID_LINE, 1, 3);
  
        // Leg and body of stick man
        line(x + 40, y - 15, x + 40, y - 40);
        line(x + 40, y - 15, x + 45, y - 10);
        line(x + 45, y - 10, x + 45, y);
        line(x + 40, y - 15, x + 37, y);
  
        // Head and hand of stick man
        circle(x + 40, y - 45, 5);
        line(x + 40, y - 35, x + 50, y - 30);
        line(x + 40, y - 35, x + 35, y - 32);
        line(x + 35, y - 32, x + 45, y - 25);
        line(x + 60, y - 45, x + 27, y + 10);
  
        // Moving the position of
        // boat and stick man
        x++;
  
        setcolor(LIGHTBLUE);
        delay(250);
  
        // Clears the graphic device
        cleardevice();
  
        // Drawing sea/river
        setlinestyle(SOLID_LINE, 1, 1);
        setfillstyle(SOLID_FILL,
                     LIGHTBLUE);
  
        rectangle(0, getmaxy() / 2 + 150,
                  getmaxx(), getmaxy());
        floodfill(getmaxx() - 10,
                  getmaxy() - 10,
                  LIGHTBLUE);
  
        // Rain drops
        setlinestyle(DASHED_LINE, 1, 2);
        x1 = 10, i = y1 = 0;
        x2 = 0, y2 = 70;
  
        while (i < 700) {
            line(x1, y1, x2, y2);
            x1 = x1 + 30;
            y2 = y2 + 60;
            i++;
        }
  
        // Drawing the boat
        setlinestyle(SOLID_LINE, 1, 1);
        setcolor(BROWN);
        setfillstyle(SOLID_FILL, BROWN);
        sector(x, y, 180, 360, 50, 10);
  
        // Body and leg of stic man
        setcolor(DARKGRAY);
        setlinestyle(SOLID_LINE, 1, 3);
        line(x + 40, y - 15, x + 40, y - 40);
        line(x + 40, y - 15, x + 45, y - 10);
        line(x + 45, y - 10, x + 45, y);
        line(x + 40, y - 15, x + 37, y);
  
        // Head hands of stick man
        circle(x + 40, y - 45, 5);
        line(x + 40, y - 35, x + 52, y - 30);
        line(x + 40, y - 35, x + 37, y - 32);
        line(x + 37, y - 32, x + 49, y - 25);
        line(x + 60, y - 45, x + 27, y + 10);
  
        // Forwarding the position of
        // the boat
        x++;
  
        // Sleep for 250 milliseconds
        delay(250);
  
        // Clears the graphic device
        cleardevice();
        j++;
    }
  
    getch();
  
    // Deallocate memory allocated
    // for graphic screen
    closegraph();
  
    return 0;
}


输出:

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程