脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现屏幕代码雨效果的示例代码

Python实现屏幕代码雨效果的示例代码

2022-10-31 10:17Vertira Python

这篇文章主要介绍了如何利用Python中的Pygame模块实现代码雨效果,文中通过示例代码介绍的非常详细,感兴趣的朋友们下面随着小编来一起学习学习吧

直接上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x',
              'c',
              'v', 'b', 'n', 'm']
    texts = [
        font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
    ]
 
    # 也可以替换成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 的虚拟环境,输入下面的命令。快速安装

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

安装效果

?
1
2
3
4
5
6
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实现屏幕代码雨效果的示例代码的文章就介绍到这了,更多相关Python代码雨内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Vertira/article/details/123349178

延伸 · 阅读

精彩推荐
  • Pythonpython__new__内置静态方法使用解析

    python__new__内置静态方法使用解析

    这篇文章主要介绍了python__new__内置静态方法使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以...

    西西嘛呦4212020-05-06
  • Pythonpython实现根据窗口标题调用窗口的方法

    python实现根据窗口标题调用窗口的方法

    这篇文章主要介绍了python实现根据窗口标题调用窗口的方法,涉及Python操作窗口的技巧,具有一定参考借鉴价值,需要的朋友可以参考下 ...

    feige11702019-11-27
  • Pythontensorflow中next_batch的具体使用

    tensorflow中next_batch的具体使用

    本篇文章主要介绍了tensorflow中next_batch的具体使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    小妖精Fsky7422021-01-11
  • Pythonpython中异常捕获方法详解

    python中异常捕获方法详解

    异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置。下面介绍几种python中获取异常信息的方法,希望小伙伴们能够喜欢...

    mindg10272020-09-22
  • Pythonpython 自定义异常和主动抛出异常(raise)的操作

    python 自定义异常和主动抛出异常(raise)的操作

    这篇文章主要介绍了python 自定义异常和主动抛出异常(raise)的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    方naoke6602021-08-13
  • PythonPython利用matplotlib实现饼图绘制

    Python利用matplotlib实现饼图绘制

    Pyplot作为Matplotlib的子库,提供了和MATLAB差不多的绘图API。因此Pyplot作为常用的绘图模块,能很方便让用户绘制2D图表。本文将为大家介绍如何利用Matplotli...

    编程简单学11812022-03-11
  • Pythonpython计算对角线有理函数插值的方法

    python计算对角线有理函数插值的方法

    这篇文章主要介绍了python计算对角线有理函数插值的方法,涉及Python数学运算的相关技巧,需要的朋友可以参考下 ...

    songguo3802020-06-23
  • Python在keras中model.fit_generator()和model.fit()的区别说明

    在keras中model.fit_generator()和model.fit()的区别说明

    这篇文章主要介绍了在keras中model.fit_generator()和model.fit()的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Hodors26772020-06-18