温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用Python制作中文汉字雨效果

发布时间:2022-03-11 11:43:50 来源:亿速云 阅读:138 作者:iii 栏目:开发技术

这篇文章主要介绍“如何用Python制作中文汉字雨效果”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何用Python制作中文汉字雨效果”文章能帮助大家解决问题。

直接上代码

import pygame
import random
 
 
def main():
    # 初始化pygame
    pygame.init()
 
    # 默认不全屏
    fullscreen = False
    # 窗口未全屏宽和高
    WIDTH, HEIGHT = 1100, 600
 
    init_width, init_height = WIDTH, HEIGHT
 
    # 字块大小,宽,高
    suface_height = 18
    # 字体大小
    font_size = 20
 
    # 创建一个窗口
    screen = pygame.display.set_mode((init_width, init_height))
 
    # 字体
    font = pygame.font.Font('msyh.ttf', font_size)
 
    # 创建一个图像对象
    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
    pygame.Surface.convert(bg_suface)
    bg_suface.fill(pygame.Color(0, 0, 0, 28))
 
    # 用纯色填充背景
    screen.fill((0, 0, 0))
 
    # 显示的字符
    letter = ['东南大学', '   ', '双一流', '   ', '  ', '大学', '  ', '机械工程学院', '   ', '   ', '  ', '东南', '   ', '双一流']
    texts = [
        font.render(str(letter[i]), True, (0, 255, 0)) for i in range(len(letter))
    ]
 
    # 也可以替换成0 1 显示
    # texts = [
    #     font.render('0',True,(0,255,0)),font.render('1',True,(0,255,0))
    # ]
 
    # 生成的列数
    column = int(init_width / suface_height)
    drops = [0 for i in range(column)]
 
    while True:
        # 按键检测
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 接受到退出事件后退出
                exit()
            elif event.type == pygame.KEYDOWN:
                # 按F11切换全屏,或窗口
                if event.key == pygame.K_F11:
                    print("检测到按键F11")
                    fullscreen = not fullscreen
                    if fullscreen:
                        # 全屏效果,参数重设
                        size = init_width, init_height = pygame.display.list_modes()[0]
                        screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
 
                    else:
                        init_width, init_height = WIDTH, HEIGHT
                        screen = pygame.display.set_mode((WIDTH, HEIGHT))
 
                    # 图像对象重新创建
                    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
                    pygame.Surface.convert(bg_suface)
                    bg_suface.fill(pygame.Color(0, 0, 0, 28))
                    column = int(init_width / suface_height)
                    drops = [0 for i in range(column)]
                elif event.key == pygame.K_ESCAPE:
                    # 按ESC退出
                    exit()
        # 延时
        pygame.time.delay(30)
 
        # 图像对象放到窗口的原点坐标上
        screen.blit(bg_suface, (0, 0))
 
        for i in range(len(drops)):
            # 随机字符
            text = random.choice(texts)
 
            # 把字符画到该列的下雨的位置
            screen.blit(text, (i * suface_height, drops[i] * suface_height))
 
            # 更新下雨的坐标
            drops[i] += 1
 
            # 超过界面高度或随机数,下雨位置置0
            if drops[i] * suface_height > init_height or random.random() > 0.95:
                drops[i] = 0
 
        # 更新画面
        pygame.display.flip()
 
 
if __name__ == '__main__':
    main()

运行效果:

如何用Python制作中文汉字雨效果

import pygame的安装方法

pygame 这个包没有安装。python安装pygame包的方法

很简单:

使用国内源安装,清华源 中科,阿里都可以。

进入Anaconda3 的虚拟环境,输入下面的命令。快速安装

pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple

安装效果

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pygame
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4c/0d/23f786eb611652b0125fcf334a0c21324922a756e6d954c50ecddfc8d4bb/pygame-2.1.2-cp36-cp36m-win_amd64.whl (8.4 MB)
     |████████████████████████████████| 8.4 MB 119 kB/s
Installing collected packages: pygame
Successfully installed pygame-2.1.2

成功安装。

关于“如何用Python制作中文汉字雨效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI