Pygame之纯Python实现你好2024效果
前言:
对于某些指JavaScript与前端实现为Python实现你好2024效果的营销号实在看不下去了。无底线营销,还要私信拿源码,hhh
于是就有了以下代码:
运行前安装pygame
pip install pygame
运行效果如图,并且彩色方块会随机下落,其他过于复杂效果不想浪费时间图一乐
 
import pygame
import sys
import random
class Confetti(pygame.sprite.Sprite):
    def __init__(self) -> None:
        super().__init__()
        # 随机选择颜色
        self.color: tuple = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        # 创建纸屑的矩形,并设置其位置和速度
        self.image = pygame.Surface((10, 10))
        self.image.fill(self.color)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, width)
        self.rect.y = random.randint(-10, height)  # 将初始位置设置为屏幕中的随机位置
        self.speed_y = random.randint(5, 10)
    def update(self) -> None:
        
        self.rect.y += self.speed_y     # 移动纸屑
        # 如果纸屑超出屏幕底部,重新设置其位置和速度
        if self.rect.y > height:
            self.rect.y = random.randint(-10, 0)
            self.rect.x = random.randint(0, width)
            self.speed_y = random.randint(5, 6)
if __name__ == "__main__":
    
    pygame.init()   # 初始化Pygame
    # 设置窗口尺寸和标题
    width, height = 1200, 800
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Hello 2024 Demo")
    # 设置白色背景
    background_color: tuple = (255, 255, 255)  
    screen.fill(background_color)
   
    pygame.display.flip()    # 刷新屏幕
    # 创建字体对象和文字
    font = pygame.font.Font(None, 100)  # 使用默认字体,大小36
    text = font.render("Hello 2024 !", True, (3, 3, 6))  # 文字内容,抗锯齿,颜色为黑色
    # 获取文字的矩形和设置其位置
    text_rect = text.get_rect()
    text_rect.center = (width // 2, height // 2)
    # 创建纸屑组
    confetti_group = pygame.sprite.Group()
    # 创建纸屑对象并添加到组中
    for _ in range(100):
        confetti = Confetti()
        confetti_group.add(confetti)
    # 主循环
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        confetti_group.update()    # 更新纸屑位置
        screen.fill(background_color)   # 清空屏幕
        confetti_group.draw(screen) # 绘制纸屑
        screen.blit(text, text_rect)
        pygame.display.flip()   # 刷新屏幕
        clock.tick(90)     # 控制帧率











![[Android] Android文件系统中存储的内容有哪些?](https://img-blog.csdnimg.cn/direct/4bf9a1619d9b48dfbd46563a0eb3b2d8.png)







