打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
就是玩儿:python轻松实现代码雨效果!

相信很多小伙伴入门编程时都是被电影中的黑客形象/代码雨的炫酷效果所吸引的,起码我是因为这样的缘故成为了一名程序猿!

python模块中能够满足趣味性的就是大家熟悉的pygame的模块,相信很多小伙伴都玩过的。

今天就使用pygame模块探讨一下如何能够生成一个炫酷的代码雨效果,pygame作为一个非标准模块需要我们使用pip的方式安装一下。

pip install pygame

下面用到了两个python模块,分别是pygame和random,random是python解释器的内置模块不需要额外安装。

# Importing the pygame module and renaming it to game.
import pygame as game

# Importing the random module and renaming it to rand.
import random as rand

在开发真正的代码雨的业务逻辑时,首先对屏幕的大小,背景颜色,字体类型等做一个初始化。

# Setting the size of the screen to 600 by 600 pixels.
screen_size = (600600)

# The last number is the alpha value. It's a value between 0 and 255 that determines the transparency of the color.
screen_color = (00010)

# It's setting the font type to be used.
screen_font_type = '宋体'

# It's setting the font size to be used.
screen_font_size = 18

接下来对pygame对象做一个初始化的展示,将我们定义好的参数设置进去。

# It's initializing the pygame module.
game.init()

# It's setting the font type to be used.
font = game.font.SysFont(screen_font_type, screen_font_size)

# It's setting the size of the screen to 600 by 600 pixels.
screen = game.display.set_mode(screen_size)

# It's creating a surface object with the same size as the screen.
surface = game.Surface(screen_size, flags=game.SRCALPHA)

# It's converting the surface object to the same format as the screen.
game.Surface.convert(surface)

# It's filling the surface object with the color defined in the screen_color variable.
surface.fill(screen_color)

# It's filling the screen with the color defined in the screen_color variable.
screen.fill(screen_color)

然后,需要设置好需要在代码雨中展示的字符集是什么,一般代码中使用到的字母和数字居多,因此我们选择字母和数字作为代码雨的内容。

# It's defining the characters to be used in the rain.
codes_ = ['0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f''g''h''i''j''k''l',
          'm''n''o''p',
          'q''r''s''t''u''v''w''x''y''z']

# It's rendering the characters in the codes_ list to be used in the rain.
codes_ = [font.render(code, True, (02550)) for code in codes_]

cols = list(range(40))

最后,创建一个死循环的方式可以让代码雨一直在跑除非关闭窗口。

# It's an infinite loop.
while 1 > 0:
    for event_ in game.event.get():  # 获取pygame模块的事件对象,若是有退出事件则关闭窗口应用
        # It's checking if the event type is equal to the QUIT event type.
        if event_.type == game.QUIT:
            exit()
    # It's delaying the execution of the code for 30 milliseconds.
    game.time.delay(30)
    # It's blitting the surface object to the screen.
    screen.blit(surface, (00))
    # It's looping through the cols list.
    for i in range(len(cols)):
        # It's choosing a random character from the codes_ list.
        text = rand.choice(codes_)
        # It's blitting the text to the screen.
        screen.blit(text, (i * 15, cols[i] * 15))
        # It's checking if the value of the cols[i] is greater than 80 or the random number generated is greater than
        # 0.95. If either of the conditions is true, it's setting the value of the cols[i] to 0. If both of the conditions
        # are false, it's incrementing the value of the cols[i] by 1.
        cols[i] = 0 if cols[i] > 80 or rand.random() > 0.95 else cols[i] + 1
    # It's updating the display.
    game.display.flip()

「Python 集中营」,只做知识分享 !

python情感分析:基于jieba的分词及snownlp的情感分析!

python数据保存:记录pandas数据分析完成后的轻量级数据保存!

记录一次关于python内存泄露的排查!

PyQt5干货:如何实现进度条与定时器及子线程的同步关联!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python|pygame基础之壁球游戏
用 Python 实现带音乐的雪花飘落雪景图
12岁的少年教你用Python做小游戏
零基础用Python开发的第一个小游戏——太空射击
调用摄像头摄像及保存图片 - 代码分享 - 开源中国社区
python pygame实现代码雨(黑客帝国既视感)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服