如何用编程代码来庆祝春节?

2023-12-23 19:49:50

如有错误,请在评论区指出!

Python 生成春联

from PIL import Image, ImageDraw, ImageFont

# 创建一个空白的红色图像
image = Image.new('RGB', (800, 400), color = 'red')

# 在图像上创建一个画布对象
draw = ImageDraw.Draw(image)

# 加载中文字体
font = ImageFont.truetype('simsun.ttc', 100)

# 写入春联的文字
draw.text((100, 50), '春', font=font, fill='white')
draw.text((200, 50), '福', font=font, fill='white')
draw.text((300, 50), '到', font=font, fill='white')
draw.text((400, 50), '家', font=font, fill='white')
draw.text((500, 50), '门', font=font, fill='white')
draw.text((600, 50), '来', font=font, fill='white')

draw.text((100, 250), '万', font=font, fill='white')
draw.text((200, 250), '象', font=font, fill='white')
draw.text((300, 250), '更新', font=font, fill='white')
draw.text((400, 250), '迎', font=font, fill='white')
draw.text((500, 250), '新', font=font, fill='white')
draw.text((600, 250), '年', font=font, fill='white')

# 显示图像
image.show()

# 保存图像
image.save('chunlian.png')

这段代码使用Python的PIL模块创建了一张春节联欢的图片,包括红色背景和春联文字。首先创建了一个800x400像素的红色背景图像,然后在这个背景上创建了一个画布对象。接着,使用PIL中的ImageFont.truetype()方法加载了一个中文字体,并在画布上使用draw.text()方法写入了春联文字。最后,通过image.show()方法显示了生成的图片,并通过image.save()方法保存了图片。

新年贺卡(HTML)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新年贺卡</title>
    <style>
        body {
            background-color: #f7e3af;
            font-family: Arial, sans-serif;
        }
        .card {
            width: 600px;
            height: 400px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }
        .card:before {
            content: "";
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('https://i.imgur.com/5e6uT3K.jpg');
            background-size: cover;
            filter: blur(5px);
            z-index: -1;
        }
        .card h1 {
            font-size: 60px;
            text-align: center;
            margin-top: 50px;
            color: #fff;
            text-shadow: 0 0 10px #000;
        }
        .card p {
            font-size: 30px;
            text-align: center;
            margin-top: 20px;
            color: #000;
        }
        .fireworks {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 1;
            pointer-events: none;
            overflow: hidden;
        }
        .fireworks canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>新年快乐</h1>
        <p>祝您新年快乐,万事如意!</p>
        <div class="fireworks"></div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/fireworks-js"></script>
    <script>
        // 创建烟花效果
        var fireworks = new Fireworks({
            target: document.querySelector('.fireworks'),
            hue: 120,
            particleCount: 100,
            delay: {
                min: 15,
                max: 30
            },
            speed: {
                min: 2,
                max: 5
            },
            acceleration: {
                x: 0,
                y: 0.05
            },
            friction: 0.95,
            gravity: 0.05,
            autoresize: true
        });
        fireworks.start();
        // 播放音效
        var audio = new Audio('https://freesound.org/data/previews/316/316847_5461839-lq.mp3');
        audio.play();
    </script>
</body>
</html>

烟花爆炸特效(Python)

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置窗口大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Fireworks Simulation")

# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

# 定义烟花类
class Firework:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.exploded = False
        self.particles = []

    def explode(self):
        self.exploded = True
        for i in range(100):
            speed = random.randint(1, 10)
            angle = random.uniform(0, 2 * 3.14159)
            particle = Particle(self.x, self.y, speed * math.cos(angle), speed * math.sin(angle), self.color)
            self.particles.append(particle)

    def draw(self, surface):
        if not self.exploded:
            pygame.draw.circle(surface, self.color, (self.x, self.y), 5)
        else:
            for particle in self.particles:
                particle.draw(surface)

# 定义粒子类
class Particle:
    def __init__(self, x, y, vx, vy, color):
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.color = color
        self.alpha = 255
        self.size = 5

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.alpha -= 5
        self.size -= 0.1

    def draw(self, surface):
        pygame.draw.circle(surface, (self.color[0], self.color[1], self.color[2], self.alpha), (int(self.x), int(self.y)), int(self.size))

# 创建烟花列表
fireworks = []

# 游戏循环
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
            firework = Firework(x, y, color)
            fireworks.append(firework)

    # 更新烟花和粒子
    for firework in fireworks:
        if not firework.exploded:
            firework.draw(screen)
            if random.random() < 0.01:
                firework.explode()
        else:
            for particle in firework.particles:
                particle.update()
                if particle.alpha <= 0:
                    firework.particles.remove(particle)

    # 绘制背景
    screen.fill(black)

    # 更新屏幕
    pygame.display.flip()

# 退出 Pygame
pygame.quit()

生成随机春节祝福语(Python,HTML)

import random

# 定义春节祝福语列表
greetings = [
    "祝你新年快乐,万事如意!",
    "愿你在新的一年里,身体健康,心情愉快!",
    "祝你和家人新年团圆,幸福安康!",
    "愿新的一年里,你的事业蒸蒸日上,财源滚滚!",
    "祝你在新的一年里,事业有成,爱情甜蜜!",
    "愿新的一年里,你的生活充满阳光和希望!",
    "祝你在新的一年里,收获满满,笑口常开!",
    "愿新的一年里,你的梦想成真,前程似锦!",
    "祝你和家人新年快乐,幸福美满!",
    "愿新的一年里,你的生活更加精彩,更加美好!"
]

# 随机选择一条祝福语并输出
print(random.choice(greetings))
<!DOCTYPE html>
<html>
<head>
    <title>随机春节祝福语生成器</title>
    <meta charset="utf-8">
    <script>
        // 定义春节祝福语列表
        var greetings = [
            "祝你新年快乐,万事如意!",
            "愿你在新的一年里,身体健康,心情愉快!",
            "祝你和家人新年团圆,幸福安康!",
            "愿新的一年里,你的事业蒸蒸日上,财源滚滚!",
            "祝你在新的一年里,事业有成,爱情甜蜜!",
            "愿新的一年里,你的生活充满阳光和希望!",
            "祝你在新的一年里,收获满满,笑口常开!",
            "愿新的一年里,你的梦想成真,前程似锦!",
            "祝你和家人新年快乐,幸福美满!",
            "愿新的一年里,你的生活更加精彩,更加美好!"
        ];

        function generateGreeting() {
            // 随机选择一条祝福语
            var greeting = greetings[Math.floor(Math.random() * greetings.length)];
            // 显示祝福语
            document.getElementById("greeting").innerHTML = greeting;
        }
    </script>
</head>
<body>
    <h1>随机春节祝福语生成器</h1>
    <button onclick="generateGreeting()">生成祝福语</button>
    <p id="greeting"></p>
</body>
</html>

烟花特效(C++)

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>

// 烟花粒子类
class Particle {
public:
    Particle(float x, float y, float speed, float angle, float size, sf::Color color) {
        m_shape.setPosition(x, y);
        m_velocity.x = speed * std::cos(angle * M_PI / 180);
        m_velocity.y = -speed * std::sin(angle * M_PI / 180);
        m_shape.setFillColor(color);
        m_shape.setRadius(size);
    }

    void update(float dt) {
        m_shape.move(m_velocity * dt);
        m_velocity.y += m_gravity * dt;
        m_lifetime -= dt;
        if (m_lifetime <= 0) {
            m_alive = false;
        }
        m_shape.setRadius(m_shape.getRadius() - m_sizeDecay * dt);
        m_shape.setFillColor(sf::Color(m_shape.getFillColor().r, m_shape.getFillColor().g, m_shape.getFillColor().b, m_lifetime / m_initialLifetime * 255));
    }

    bool isAlive() const {
        return m_alive;
    }

    sf::CircleShape getShape() const {
        return m_shape;
    }

private:
    sf::CircleShape m_shape;
    sf::Vector2f m_velocity;
    float m_gravity = 100.0f;
    float m_lifetime = 2.0f;
    float m_initialLifetime = 2.0f;
    float m_sizeDecay = 50.0f;
    bool m_alive = true;
};

int main() {
    // 创建窗口
    sf::RenderWindow window(sf::VideoMode(800, 600), "2024 新年快乐");
    window.setFramerateLimit(60);

    // 加载字体
    sf::Font font;
    if (!font.loadFromFile("arial.ttf")) {
        std::cerr << "Failed to load font" << std::endl;
        return 1;
    }

    // 创建文本
    sf::Text text("2024 新年快乐", font, 80);
    text.setFillColor(sf::Color::White);
    text.setPosition(window.getSize().x / 2 - text.getLocalBounds().width / 2, window.getSize().y / 2 - text.getLocalBounds().height / 2);

    // 创建烟花粒子容器
    std::vector<Particle> particles;

    // 随机数种子
    std::srand(std::time(nullptr));

    // 主循环
    while (window.isOpen()) {
        // 处理事件
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        // 更新烟花粒子
        float dt = 1.0f / 60.0f;
        for (auto it = particles.begin(); it != particles.end();) {
            it->update(dt);
            if (!it->isAlive()) {
                it = particles.erase(it);
            } else {
                ++it;
            }
        }

        // 制造烟花特效
        if (std::rand() % 100 < 5) {
            float x = std::rand() % window.getSize().x;
            float y = window.getSize().y;
            float speed = 200.0f + std::rand() % 100;
            float angle = -90.0f + std::rand() % 45;
            float size = 10.0f + std::rand() % 20;
            sf::Color color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
            particles.emplace_back(x, y, speed, angle, size, color);
        }

        // 清空窗口
        window.clear(sf::Color::Black);

        // 绘制文本
        window.draw(text);

        // 绘制烟花粒子
        for (auto& particle : particles) {
            window.draw(particle.getShape());
        }

        // 显示窗口
        window.display();
    }

    return 0;
}

文章来源:https://blog.csdn.net/m0_69824302/article/details/135167334
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。