21xrx.com
2025-04-26 02:35:59 Saturday
文章检索 我的文章 写文章
简单易学的C语言烟花程序代码
2023-06-12 21:59:02 深夜i     11     0
C语言 编程 烟花程序 基本数据类型 函数

C语言是一种十分重要的编程语言,可以用于编写各种程序。今天,我们将分享一个简单易学的C语言烟花程序代码。这个程序可以让你在屏幕上掌握控制一个美丽而华丽的烟花。

要编写这个程序,你需要有一些基本的C语言知识,如变量,函数和循环。以下是程序的代码:

#include 
#include 
#include 
#include 
void setCursorPosition(int x, int y);
void setColor(int colorCode);
void explosion(int x, int y, int color);
void trail(int x, int y, int color);
void main()
{
  srand(time(NULL));
  int x = 40, y = 12, t = 30;
  while (t--)
  {
    setColor(15);
    system("cls");
    explosion(x, y, rand() % 6 + 9);
    Sleep(100);
  }
}
void explosion(int x, int y, int color)
{
  int i, j, r;
  for (i = -8; i < 8; ++i)
  {
    r = abs(i) * 2;
    for (j = -r; j < r; ++j)
    {
      trail(x + i, y + j / 2, color);
    }
  }
}
void trail(int x, int y, int color)
{
  setCursorPosition(x, y);
  setColor(color);
  printf("o");
}
void setCursorPosition(int x, int y)
{
  COORD position = y ;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
void setColor(int colorCode)
{
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorCode);
}

运行这个程序你会得到一个美丽而华丽的烟花效果。同时,你也可以尝试对代码进行一些修改并观察它们的效果。这个程序也可以让你了解到C语言的基本数据类型和函数的使用方法,是一个很不错的练习和学习资源。

  
  

评论区