以下是一个简单的C语言烟花代码示例,使用Dev-C++软件编写:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // 仅适用于Windows操作系统
#define WIDTH 80
#define HEIGHT 24
void draw_firework(int x, int y, int color) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
printf("*"); // 输出星号表示烟花效果
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 恢复默认颜色
}
int main() {
srand(time(NULL)); // 初始化随机数种子
while (1) { // 无限循环,直到手动终止程序
int x = rand() % WIDTH; // 随机生成烟花的x位置
int y = rand() % HEIGHT; // 随机生成烟花的y位置
int color = rand() % 7 + 9; // 随机生成烟花的颜色(红色、绿色、蓝色等)
draw_firework(x, y, color); // 绘制烟花效果
Sleep(500); // 等待500毫秒(0.5秒)
}
return 0;
}
请注意,此代码仅适用于Windows操作系统,因为它使用了Windows API函数SetConsoleCursorPosition和GetStdHandle。如果你使用其他操作系统,你需要使用相应的函数或库来实现控制台文本的定位和颜色更改。