用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

黑客帝国代码雨特效(要安装pygame)

2022-08-31 作者: Python自学举报

[python]代码库

#!/usr/bin/env python
# encoding: utf-8

import random, pygame


class matrix:
    '''
     This is a main Class, the file contains all documents.
     One document contains paragraphs that have several sentences
     It loads the original file and converts the original file to new content
     Then the new content will be saved by this class
    '''

    def __init__(self):
        self.panel_width = 1800
        self.panel_height = 1000
        self.font_size = 15

    def hello(self):
        '''
        This is a welcome speech
        :return: self
        '''
        return self

    def run(self):
        '''
        The program entry
        '''
        print('\033[1;31;0m')
        print('↓' * 20 + ' 类型选择: ' + '↓' * 20)
        print('0:数字代码雨 1:字符代码雨')
        print('\033[0m')
        type = input('请选择代码雨类型:')
        self.rain(type)

    def rain(self, type):
        pygame.init()
        win = pygame.display.set_mode((self.panel_width, self.panel_height))

        font = pygame.font.SysFont('SimHei', 22)
        rain_type = type if type in [0, 1] else 0
        texts = self.digital_text(font) if rain_type == 0 else self.word_text(font)

        bg_surface = pygame.Surface((self.panel_width, self.panel_height), flags=pygame.SRCALPHA)
        pygame.Surface.convert(bg_surface)
        bg_surface.fill(pygame.Color(0, 0, 0, 28))
        win.fill((0, 0, 0))

        # 按屏幕的宽带计算可以在画板上放几列坐标并生成一个列表
        column = int(self.panel_width / self.font_size)
        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(30)

            # 重新编辑图像第二个参数是坐上角坐标
            win.blit(bg_surface, (0, 0))

            for i in range(len(drops)):
                text = random.choice(texts)
                # 重新编辑每个坐标点的图像
                win.blit(text, (i * self.font_size, drops[i] * self.font_size))
                drops[i] += 1
                if drops[i] * 10 > self.panel_height or random.random() > 0.95:
                    drops[i] = 0

            pygame.display.flip()

    def digital_text(self, font):
        return [font.render(str(i), True, (0, 255, 0)) for i in range(10)]

    def word_text(self, font):
        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']
        return [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)]


if __name__ == '__main__':
    matrix().hello().run()

[代码运行效果截图]


黑客帝国代码雨特效(要安装pygame)


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...