c语言跨年烟花源代码
时间: 2023-12-19 22:31:52 浏览: 287
以下是C语言编写的跨年烟花源代码,你可以将其复制到你的C语言编译器中运行,体验跨年烟花的美妙!
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 80
#define HEIGHT 25
#define MAX_PARTICLES 2000
#define GRAVITY 0.05
int term_width, term_height;
typedef struct {
float x, y;
float vx, vy;
float age;
float alpha;
} particle_t;
particle_t particles[MAX_PARTICLES];
void init_particle(particle_t *p) {
p->x = term_width / 2;
p->y = term_height - 1;
p->vx = ((float)rand() / RAND_MAX - 0.5) * 4;
p->vy = ((float)rand() / RAND_MAX) * -3;
p->age = 0;
p->alpha = 1;
}
void update_particle(particle_t *p) {
p->x += p->vx;
p->y += p->vy;
p->vy += GRAVITY;
p->age += 0.02;
p->alpha = 1 - p->age;
}
void draw_particle(particle_t *p) {
int x = (int)p->x;
int y = (int)p->y;
if (x >= 0 && x < term_width && y >= 0 && y < term_height) {
float alpha = p->alpha * 10;
if (alpha > 1) alpha = 1;
printf("\033[%d;%dH\033[48;2;%d;%d;%dm\033[38;2;%d;%d;%dm%c\033[0m",
y + 1, x + 1,
(int)(255 * alpha), (int)(255 * alpha), (int)(255 * alpha),
(int)(255 * (1 - alpha)), (int)(255 * (1 - alpha)), (int)(255 * (1 - alpha)),
rand() % 2 ? '*' : '.');
}
}
int main() {
srand(time(NULL));
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
term_width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
term_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
system("cls");
while (1) {
if (kbhit()) break;
for (int i = 0; i < MAX_PARTICLES; i++) {
if (particles[i].age == 0) {
init_particle(&particles[i]);
} else {
update_particle(&particles[i]);
draw_particle(&particles[i]);
if (particles[i].y >= term_height - 1) {
particles[i].age = 0;
}
}
}
Sleep(20);
}
return 0;
}
```
阅读全文