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

qiaoqiaohonghu 2024-10-09 10:05:02 阅读 64

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

<code>#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;

}



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。