c/c++ 浪漫烟花代码(精品)

//几天没更新了,今天来给大家做个浪漫烟花,代码直接拿走,记得多多关照哦~

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
 
#define WIDTH 80
#define HEIGHT 25
 
typedef struct {
    float x;
    float y;
    float vx;
    float vy;
    int color;
} Particle;
 
void setCursorPosition(int x, int y) {
    printf("\033[%d;%df", y + 1, x + 1);
    fflush(stdout);
}
 
void clearScreen() {
    printf("\033[2J");
    setCursorPosition(0, 0);
}
 
void drawParticle(Particle* particle) {
    setCursorPosition((int) particle->x, (int) particle->y);
    printf("\033[48;5;%dm \033[0m", particle->color);
}
 
void updateParticle(Particle* particle, float dt) {
    particle->x += particle->vx * dt;
    particle->y += particle->vy * dt;
    particle->vy += 9.8 * dt;
}
 
void explode(Particle* particle) {
    particle->vx = (rand() % 51 - 25) / 10.0;
    particle->vy = (rand() % 51 - 25) / 10.0;
    particle->color = rand() % 256;
}
 
int main() {
    srand(time(NULL));
 
    Particle fireworks[100];
 
    clearScreen();
 
    for (int i = 0; i < 100; i++) {
        fireworks[i].x = WIDTH / 2;
        fireworks[i].y = HEIGHT - 1;
        fireworks[i].vx = (rand() % 51 - 25) / 10.0;
        fireworks[i].vy = -1 - rand() % 5;
        fireworks[i].color = rand() % 256;
    }
 
    while (1) {
        for (int i = 0; i < 100; i++) {
            drawParticle(&fireworks[i]);
            updateParticle(&fireworks[i], 0.1);
 
            if (fireworks[i].y > HEIGHT || fireworks[i].x < 0 || fireworks[i].x > WIDTH) {
                explode(&fireworks[i]);
                fireworks[i].x = WIDTH / 2;
                fireworks[i].y = HEIGHT - 1;
            }
        }
 
        usleep(10000);
    }
 
    return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
 
#define WIDTH 80
#define HEIGHT 25
 
typedef struct {
    float x;
    float y;
    float vx;
    float vy;
    int color;
} Particle;
 
void setCursorPosition(int x, int y) {
    printf("\033[%d;%df", y + 1, x + 1);
    fflush(stdout);
}
 
void clearScreen() {
    printf("\033[2J");
    setCursorPosition(0, 0);
}
 
void drawParticle(Particle* particle) {
    setCursorPosition((int) particle->x, (int) particle->y);
    printf("\033[48;5;%dm \033[0m", particle->color);
}
 
void updateParticle(Particle* particle, float dt) {
    particle->x += particle->vx * dt;
    particle->y += particle->vy * dt;
    particle->vy += 9.8 * dt;
}
 
void explode(Particle* particle) {
    particle->vx = (rand() % 51 - 25) / 10.0;
    particle->vy = (rand() % 51 - 25) / 10.0;
    particle->color = rand() % 256;
}
 
int main() {
    srand(time(NULL));
 
    Particle fireworks[100];
 
    clearScreen();
 
    for (int i = 0; i < 100; i++) {
        fireworks[i].x = WIDTH / 2;
        fireworks[i].y = HEIGHT - 1;
        fireworks[i].vx = (rand() % 51 - 25) / 10.0;
        fireworks[i].vy = -1 - rand() % 5;
        fireworks[i].color = rand() % 256;
    }
 
    while (1) {
        for (int i = 0; i < 100; i++) {
            drawParticle(&fireworks[i]);
            updateParticle(&fireworks[i], 0.1);
 
            if (fireworks[i].y > HEIGHT || fireworks[i].x < 0 || fireworks[i].x > WIDTH) {
                explode(&fireworks[i]);
                fireworks[i].x = WIDTH / 2;
                fireworks[i].y = HEIGHT - 1;
            }
        }
 
        usleep(10000);
    }
 
    return 0;
}

当然可以!以下是一个用C语言编写的高级生日快乐代码示例。这个程序会输出一个带有ASCII艺术的生日祝福,并播放一段生日歌曲。 ```c #include <stdio.h> #include <unistd.h> // 用于sleep函数 // ASCII艺术 "Happy Birthday" const char *ascii_art[] = { " _____ _ _ _ ", " / ____| | | | | | | ", " | (___ | | ___ | |__ | |_ ___ _ __ ", " \\___ \\ | |/ _ \\| '_ \\ | __/ _ \\| '__|", " ____) | | | (_) | | | || || (_) | | ", " |_____/ |_|\\___/|_| |_| \\__\\___/|_| ", }; // 生日歌曲(简化版) const char *song[] = { "Happy birthday to you,", "Happy birthday to you,", "Happy birthday dear [Name],", "Happy birthday to you!", }; void print_ascii_art() { for (int i = 0; i < sizeof(ascii_art) / sizeof(ascii_art[0]); i++) { printf("%s\n", ascii_art[i]); } } void play_song(const char *name) { for (int i = 0; i < sizeof(song) / sizeof(song[0]); i++) { if (i == 2) { printf("Happy birthday dear %s,\n", name); } else { printf("%s\n", song[i]); } sleep(1); // 模拟歌曲播放时间间隔 } } int main() { char name[50]; printf("请输入你的名字: "); scanf("%49s", name); // 读取名字,最多49个字符 printf("\n"); print_ascii_art(); printf("\n"); play_song(name); return 0; } ``` ### 代码说明: 1. **ASCII艺术**:使用一个字符串数组来存储“Happy Birthday”的ASCII艺术图案。 2. **生日歌曲**:使用一个字符串数组来存储生日歌曲的歌词,并在适当位置插入名字。 3. **打印ASCII艺术**:`print_ascii_art`函数负责逐行打印ASCII艺术。 4. **播放歌曲**:`play_song`函数负责逐行打印歌曲歌词,并在适当的位置插入名字,同时使用`sleep`函数模拟歌曲播放的时间间隔。 5. **主函数**:从用户处获取名字,然后调用上述两个函数来显示ASCII艺术和播放生日歌曲。 运行这个程序时,它会提示用户输入他们的名字,然后显示ASCII艺术和生日歌曲。希望你喜欢这个高级的生日快乐代码
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值