c语言-烟花代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void delay(unsigned int milliseconds) {
    clock_t start_time = clock();
    while (clock() < start_time + milliseconds);
}

int main() {
    srand(time(NULL));

    int numParticles = 50;
    int explosionRadius = 10;
    
    for (int i = 0; i < numParticles; i++) {
        int xVelocity = (rand() % (2 * explosionRadius)) - explosionRadius;
        int yVelocity = (rand() % (2 * explosionRadius)) - explosionRadius;
        
        for (int j = 0; j < 50; j++) {
            printf("Particle %d: x=%d, y=%d\n", i+1, j * xVelocity, j * yVelocity);
            delay(50);
        }
    }
    
    return 0;
}

这个简单的烟花代码会模拟一个烟花爆炸的效果。它会生成一定数量的粒子,每个粒子具有随机的水平速度和垂直速度。它会在屏幕上打印出每个粒子在不同时间点的位置,并且每隔50毫秒更新一次位置。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 延时函数,用于控制粒子的更新速度
void delay(unsigned int milliseconds) {
    clock_t start_time = clock();
    while (clock() < start_time + milliseconds);
}

int main() {
    // 设置随机数种子,以便每次运行程序时生成的随机数不同
    srand(time(NULL));

    // 定义粒子数量和爆炸半径
    int numParticles = 50;
    int explosionRadius = 10;
    
    // 循环生成粒子并模拟烟花爆炸效果
    for (int i = 0; i < numParticles; i++) {
        // 为每个粒子生成随机的水平速度和垂直速度
        int xVelocity = (rand() % (2 * explosionRadius)) - explosionRadius;
        int yVelocity = (rand() % (2 * explosionRadius)) - explosionRadius;
        
        // 更新粒子的位置并打印出来
        for (int j = 0; j < 50; j++) {
            printf("Particle %d: x=%d, y=%d\n", i+1, j * xVelocity, j * yVelocity);
            // 调用延时函数使粒子位置更新以及打印的速度适中
            delay(50);
        }
    }
    
    return 0;
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高教百科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值