相信很多小伙伴入门编程时都是被电影中的黑客形象/代码雨的炫酷效果所吸引的,起码我是因为这样的缘故成为了一名程序猿!
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 = (600, 600)
# The last number is the alpha value. It's a value between 0 and 255 that determines the transparency of the color.
screen_color = (0, 0, 0, 10)
# 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, (0, 255, 0)) 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, (0, 0))
# 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.
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 = (600, 600)
# The last number is the alpha value. It's a value between 0 and 255 that determines the transparency of the color.
screen_color = (0, 0, 0, 10)
# 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, (0, 255, 0)) 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, (0, 0))
# 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()
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/11108/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取