程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

30 lines of python code to achieve code rain

編輯:Python

我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!

1. 介紹

上手 python,A lot of people first contact is pygame 包了,Can have fun at the same time,建立起對 python 的基本認識.

提到編程,I think many people's first impression is the hacker's image in the film,Under the background of black keyboard,With flashing green font,Dive into the crack system one by one......

當然,Reality and the imagination, of course, is irrelevant,But given the imagination,Why not to try on their own to achieve?所以,We can use this very simple30Lines of code to achieve the effect of a rain of code,Meet our imagination and curiosity.

We just use thispygamerandom兩個包,首先,Will they import:

import pygame
import random
復制代碼

之後,我們進行pygameInitialization of the interface:

# 參數
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)
復制代碼

After setting the content related to our font:

# 內容
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
復制代碼

The last in a loop,Update the interface and code to map the rain:

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)
# 繪制代碼雨
screen.blit(text, (i * 15, cols[i] * 15))
cols[i] = 0 if cols[i] >80 or random.random() > 0.95 else cols[i]+1
pygame.display.flip()
復制代碼

2. 完整代碼

完整代碼如下:

import pygame
import random
# 參數
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)
# 內容
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)
screen.blit(text, (i * 15, cols[i] * 15))
cols[i] = 0 if cols[i] >80 or random.random() > 0.95 else cols[i]+1
pygame.display.flip()
復制代碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved