Python实现屏幕代码雨效果的示例代码
作者:Vertira 时间:2021-08-29 12:48:09
直接上代码
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()
运行效果:
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
成功安装。
来源:https://blog.csdn.net/Vertira/article/details/123349178
标签:Python,屏幕,代码雨
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CSS执行顺序与优先权的问题
2010-08-23 16:21:00
django中F与Q查询的使用
2022-11-20 19:23:22
![](https://img.aspxhome.com/file/2023/3/93483_0s.png)
composer.lock文件的作用
2023-06-06 13:05:24
jQuery.sheet – 创建Excel界面风格的jQuery在线应用
2010-01-27 13:03:00
![](https://img.aspxhome.com/file/UploadPic/20101/27/20101271369489s.jpg)
python用opencv批量截取图像指定区域的方法
2021-10-14 09:07:33
python 基于dlib库的人脸检测的实现
2022-08-23 22:59:07
![](https://img.aspxhome.com/file/2023/7/68967_0s.jpg)
asp如何做一个检索结果带链接的检索?
2010-07-11 21:15:00
Python PyInstaller安装和使用教程详解
2021-05-12 01:28:40
js更好地截取字符串
2008-03-11 19:00:00
jupyter notebook 写代码自动补全的实现
2023-04-14 03:04:00
![](https://img.aspxhome.com/file/2023/6/101436_0s.png)
asp中通过addnew添加内容后取得当前文章的自递增ID的方法
2011-02-05 11:05:00
python实现从pdf文件中提取文本,并自动翻译的方法
2021-06-08 18:55:56
![](https://img.aspxhome.com/file/2023/5/84135_0s.jpg)
ASP程序直接连接MYSQL数据库
2008-10-03 12:13:00
.NET 6中为record类型自定义Equals方法
2023-07-15 21:29:29
JSON.stringify转换JSON时日期时间不准确的解决方法
2014-07-20 13:25:07
![](https://img.aspxhome.com/file/2023/7/55917_0s.jpg)
Python导入数值型Excel数据并生成矩阵操作
2023-05-15 16:59:24
![](https://img.aspxhome.com/file/2023/3/64263_0s.jpg)
PyQt5显示GIF图片的方法
2023-04-04 02:22:29
![](https://img.aspxhome.com/file/2023/6/95676_0s.gif)
django query模块
2021-12-01 09:16:22
ASP 隐藏下载地址及防盗链代码
2011-02-26 11:17:00
python替换字符串中的子串图文步骤
2021-06-23 22:02:47
![](https://img.aspxhome.com/file/2023/6/69006_0s.jpg)