用python8行代码做超简单代码雨

朋友们好,python是一门计算机语言,可以编写好玩的程序。不过,在编程的过程中,装酷也是必不可少的,最好的例子就是:代码雨

在本文章,我将会用python通过两种方式来实现代码雨(一个低配一个高配)

那先来一个低配版的代码雨。其实,最简单的代码雨就是让一程序不断打印乱码,这就要用到random库,看效果:

 再来看这个效果如何实现:

import random
import time
time.sleep(1)
a=[('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'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('0'),('-'),('='),('+'),('/'),('?'),('.'),(','),('['),(']'),('~'),('%'),('#'),('*'),('^'),'{','}','(',')','<','>','@','|',':','$','&']
while True:
    random.shuffle(a)
    print(a[0:47])
    time.sleep(0.1)

低配版就是这个样子,那么接下来就是高配版了

看代码:

import random
import pygame
PANEL_width = 2000
PANEL_highly = 2000
FONT_PX = 15
pygame.init()

winSur = pygame.display.set_mode((PANEL_width, PANEL_highly))
font = pygame.font.SysFont("123.ttf", 25)
bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 28))
winSur.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)
]
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(2)]
column = int(PANEL_width / FONT_PX)
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:
      chang = pygame.key.get_pressed()
      if (chang[32]):
        exit()
  
  pygame.time.delay(40)
  winSur.blit(bg_suface, (0, 0))
  for i in range(len(drops)):
    text = random.choice(texts)
    winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
    drops[i] += 1
    if drops[i] * 10 > PANEL_highly or random.random() > 0.95:
      drops[i] = 0
  pygame.display.flip()
 

本文章的内容就是这个,感谢观看,谢谢。

猜你喜欢

转载自blog.csdn.net/hu20100913/article/details/126392143