Pygame之纯Python实现你好2024效果

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)     # 控制帧率

相关推荐

  1. 再见20232024

    2024-01-24 14:26:03       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 14:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 14:26:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 14:26:03       82 阅读
  4. Python语言-面向对象

    2024-01-24 14:26:03       91 阅读

热门阅读

  1. 设计模式-桥接模式

    2024-01-24 14:26:03       57 阅读
  2. SpringBoot 整合redis

    2024-01-24 14:26:03       59 阅读
  3. 回顾一下容易被忽视golang基础的面试考察点

    2024-01-24 14:26:03       61 阅读
  4. 【Effective C++】4. 设计与声明

    2024-01-24 14:26:03       55 阅读
  5. Day24_216组合总数_17电话号码的字母组合

    2024-01-24 14:26:03       68 阅读
  6. Linux软件包管理器yum

    2024-01-24 14:26:03       54 阅读
  7. OpenGL渲染中使用EGL创建上下文

    2024-01-24 14:26:03       55 阅读
  8. 李沐深度学习-多层感知机从零开始

    2024-01-24 14:26:03       57 阅读
  9. 人工智能(Artificial Intelligence,简称AI)

    2024-01-24 14:26:03       46 阅读
  10. SpringMVC-域对象共享数据

    2024-01-24 14:26:03       48 阅读