📜  C程序使用图形显示带有消息的动画鞭炮

📅  最后修改于: 2021-05-28 03:57:44             🧑  作者: Mango

在本文中,我们将讨论如何使用Graphics绘制Rocket

方法:

  • 在main()函数,调用launch()函数。 launch()函数是一个基础,它将重复调用fire()和mess()函数,直到满足给定条件为止。
  • mess()函数将包含生日消息。
    • 要描绘生日消息,请使用settextstyle()和outtextxy()函数。这些条件,通过用户的输入,确定了烟花可以达到的最大高度。
    • 请注意,可以在函数调用之前编写任何文本。
  • fire()函数最初会调用cracker()函数。
    • 该裂解装置()函数实现的五个鞭炮用图形的线()函数三角形的援助。
    • 为了给颜色设置,使用setfillstyle()和Floodfill()函数。
    • fire()函数将使用line()函数实现相同长度的多行。
  • 头文件“ time.h”用于其中存在的延迟函数。延迟函数将使我们能够为动画添加变化。

下面是上述方法的实现:

C
// C program for the above approach
  
#include 
#include 
#include 
#include 
  
// Function to display the crackers
void crackers()
{
    // 1st Cracker
    setfillstyle(SOLID_FILL, RED);
    line(80, 680, 130, 680);
    line(80, 680, 105, 630);
    line(105, 630, 130, 680);
    floodfill(105, 650, 15);
  
    // 2nd Cracker
    setfillstyle(SOLID_FILL, BLUE);
    line(300, 680, 350, 680);
    line(300, 680, 325, 630);
    line(325, 630, 350, 680);
    floodfill(325, 650, 15);
  
    // 3rd Cracker
    setfillstyle(SOLID_FILL, BROWN);
    line(500, 680, 550, 680);
    line(500, 680, 525, 630);
    line(525, 630, 550, 680);
    floodfill(525, 650, 15);
  
    // 4th Cracker
    setfillstyle(SOLID_FILL, GREEN);
    line(700, 680, 750, 680);
    line(700, 680, 725, 630);
    line(725, 630, 750, 680);
    floodfill(725, 650, 15);
  
    // 5th Cracker
    setfillstyle(SOLID_FILL, CYAN);
    line(920, 680, 970, 680);
    line(920, 680, 945, 630);
    line(945, 630, 970, 680);
    floodfill(945, 650, 15);
}
  
// Function to display the crackers
// using the crackers() function
void fire()
{
    int v = 625, u = 630, g = 630, h = 625,
        a = 325, n = 325, e = 325, x = 320, j = 330;
    int b = 525, m = 525, f = 525, y = 520,
        k = 530, c = 725, i = 725, o = 725,
        z = 720, l = 730;
    int t = 630, s = 625;
  
    // Call the crackers funtions
    crackers();
  
    // Iterate until h is 435
    while (h != 435) {
        crackers();
  
        // Crackers function
        // called
        line(n, g, n, h);
        line(m, u, m, v);
        line(o, u, o, v);
        line(a, u, x, v);
        line(b, u, y, v);
        line(c, u, z, v);
        line(e, t, j, s);
        line(f, t, k, s);
        line(i, t, l, s);
  
        // Update the value of
        // variables
        a = a - 10;
        x = x - 10;
        u = u - 10;
        v = v - 10;
        g = g - 10;
        h = h - 10;
        b = b - 10;
        y = y - 10;
        c = c - 10;
        z = z - 10;
        t = t - 10;
        s = s - 10;
        e = e + 10;
        f = f + 10;
        i = i + 10;
        j = j + 10;
        k = k + 10;
        l = l + 10;
  
        // Delay of 30 ms
        delay(30);
    }
}
  
// Function to display the birthday
// message
void mess()
{
    // Set the text color
    settextstyle(8, 0, 5);
  
    // Print the birthday message
    outtextxy(200, 350, "HAPPY"
                        " BIRTHDAY TO "
                        "ANYONE....");
}
  
// Function to call the fire and the
// mess function repeatedly to make
// the fire crackers
void launch()
{
    int q1 = 630, q2 = 590;
  
    // While Q2 is not the same
    // as 330
    while (q2 != 330) {
        // Clear the screen
        cleardevice();
  
        // Call the fire and mess
        // function
        fire();
        mess();
  
        // Print the lines
        line(105, q1, 105, q2);
        line(945, q1, 945, q2);
  
        // Update the value of q1
        // and q2
        q1 = q1 - 20;
        q2 = q2 - 20;
  
        // Call the fire and mess
        // function again
        fire();
        mess();
  
        // Delay of 30 ms
        delay(30);
    }
}
  
// Driver Code
void main()
{
    int gd = DETECT, gm;
  
    // Initialize of gdriver
    initgraph(&gd, &gm, "C:\\"
                        "turboc3\\bgi");
  
    // Lauch the fire
    launch();
  
    // Fill the left circles and text
    // to white color
    setfillstyle(SOLID_FILL, WHITE);
    circle(105, 370, 20);
    floodfill(104, 371, 15);
    floodfill(106, 371, 15);
  
    // Fill the right circles and text
    // to white color
    setfillstyle(SOLID_FILL, WHITE);
    circle(945, 370, 20);
    floodfill(944, 371, 15);
    floodfill(946, 371, 15);
  
    // Hold the screen for a while
    getch();
  
    // Close the initialized gdriver
    closegraph();
  
    return 0;
}


输出:

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