影刀RPA实现电子烟花元宵快乐
评论
收藏

影刀RPA实现电子烟花元宵快乐

经验分享
千
千里
2024-02-23 15:10·浏览量:818
千
千里
影刀见习开发者
发布于 2024-02-23 15:10818浏览
# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot
from xbot import print, sleep
from .import package
from .package import variables as glv
import pygame
import random
def main(args):
    # 初始化Pygame
    pygame.init()

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

    # 定义颜色
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)

    # 定义烟花粒子类
    class Particle(pygame.sprite.Sprite):
        def __init__(self, x, y, color):
            super().__init__()
            self.image = pygame.Surface([2, 2])
            self.image.fill(color)
            self.rect = self.image.get_rect()
            self.rect.x = x
            self.rect.y = y
            self.speed_x = random.randint(-5, 5)
            self.speed_y = random.randint(-15, -5)
            self.gravity = 0.1

        def update(self):
            self.speed_y += self.gravity
            self.rect.x += self.speed_x
            self.rect.y += self.speed_y

    # 创建烟花粒子群
    particles = pygame.sprite.Group()

    # 定义文字图案
    font = pygame.font.Font('C:\WINDOWS\FONTS\MSYHL.TTC', 80)
    text = "元宵快乐"
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect(center=(screen_width // 2, screen_height // 2))
    text_pos_x = screen_width

    # 游戏主循环
    running = True
    clock = pygame.time.Clock()
    firework_counter = 0
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # 自动放烟花
        if firework_counter >= 10:
            # 在随机位置创建烟花
            random_pos = (random.randint(0, screen_width), random.randint(0, screen_height))
            for _ in range(100):
                particle = Particle(random_pos[0], random_pos[1], random.choice([WHITE, (255, 0, 0), (0, 255, 0), (0, 0, 255)]))
                particles.add(particle)
            firework_counter = 0
        else:
            firework_counter += 1

        # 更新烟花粒子位置
        particles.update()

        # 清空屏幕
        screen.fill(BLACK)

        # 绘制文字
        screen.blit(text_surface, (text_pos_x, text_rect.y))
        text_pos_x -= 2  # 控制文字的滚动速度
        if text_pos_x < -text_rect.width:
            text_pos_x = screen_width

        # 绘制烟花粒子
        particles.draw(screen)

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

        # 控制帧率
        clock.tick(60)

    # 退出游戏
    pygame.quit()

使用影刀的编码版,安装pygame模块

把代码复制进去就可以。

B站演示视频地址

公众号原文地址

收藏2
全部评论(1)
最新
发布评论
评论