日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python實(shí)現(xiàn)字母閃爍效果的示例代碼_python

作者:樹獺叔叔 ? 更新時間: 2022-09-24 編程語言

效果圖

1. 介紹

屏幕上隨機(jī)閃爍的代碼塊,一定能滿足我們對于電影中黑客的一絲絲設(shè)想,這次,讓我們用簡簡單單的30行python代碼,實(shí)現(xiàn)這個效果。

前面我們借助 python 實(shí)現(xiàn)了代碼雨的效果,這一次,我們同樣借助pygamerandom兩個包,實(shí)現(xiàn)代碼閃爍的效果。

此次我們只是用pygamerandom兩個包,首先,將他們導(dǎo)入:

import pygame
import random

之后,我們進(jìn)行pygame界面的初始化工作:

# 參數(shù)
SCREENSIZE=(600,600)
BLACK=(0,0,0,13)
# 初始化
pygame.init()
font = pygame.font.SysFont('宋體', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)

之后設(shè)置一下我們字體的相關(guān)內(nèi)容:

# 內(nèi)容
lib=[chr(i) for i in range(48,48+10)] + [chr(i) for i in range(97,97+26)]   # [0-9 a-z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40))  # 字體15, 窗口600

最后在一個循環(huán)中,更新界面并實(shí)現(xiàn)閃爍的代碼打印效果:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.time.delay(33)
    screen.blit(surface, (0, 0))
    for i in range(n:=len(cols)):
        text = random.choice(texts)
    # 代碼閃爍
    x,y=random.randint(0,n-1),random.randint(0,n-1)
    screen.blit(text,(x*15,cols[y]*15))
    pygame.display.flip()

2. 完整代碼

完整代碼如下:

import pygame
import random
# 參數(shù)
SCREENSIZE=(600,600)
BLACK=(0,0,0,13)
# 初始化
pygame.init()
font = pygame.font.SysFont('宋體', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)
# 內(nèi)容
lib=[chr(i) for i in range(48,48+10)] + [chr(i) for i in range(97,97+26)]   # [0-9 a-z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40))  # 字體15, 窗口600
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.time.delay(33)
    screen.blit(surface, (0, 0))
    for i in range(n:=len(cols)):
        text = random.choice(texts)
        # 隨機(jī)閃爍
        x,y=random.randint(0,n-1),random.randint(0,n-1)
        screen.blit(text,(x*15,cols[y]*15))
    pygame.display.flip()

原文鏈接:https://juejin.cn/post/7126567549679960078

欄目分類
最近更新