用python实现代码雨,仿黑客帝国矩阵(附源码)。

俗话说得好:

python学得好,装酷没烦恼。

装酷装得当, 圈粉一大把。

今天我们就来探讨python实现代码雨!

 

import pygame   
import random

def main():
    初始化pygame
    pygame.init()

   默认不全屏
    fullscreen = False
    窗口未全屏宽和高
    WIDTH, HEIGHT = 1100, 600

    init_width, init_height = WIDTH, HEIGHT

    字块大小,宽,高
    suface_height = 18
    字体大小
    font_size = 20

    创建一个窗口
    screen = pygame.display.set_mode((init_width, init_height))

    字体
    font = pygame.font.Font('MSYHMONO.ttf', font_size)

(如需MSYHMONO自提,去这里

    创建一个图像对象
    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
    pygame.Surface.convert(bg_suface)
    bg_suface.fill(pygame.Color(0, 0, 0, 28))

    用纯色填充背景(黑色)
    screen.fill((0, 0, 0))

    显示的字符
    letter = ['qe','q', '6w', 'e','3', 'r', '2','7t', '101y', 'u', '687i', 'o', 'p6', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x',
              'c',
              'v', 'b', 'n', 'm','1','0']
    texts = [
        font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
    ]

    也可以替换成0 1 显示
     texts = [
         font.render('0',True,(0,255,0)),font.render('1',True,(0,255,0))
     ]

    生成的列数
    column = int(init_width / suface_height)
    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:
                按F11切换全屏,或窗口
                if event.key == pygame.K_F11:
                    print("检测到按键F11")
                    fullscreen = not fullscreen
                    if fullscreen:
                        全屏效果,参数重设
                        size = init_width, init_height = pygame.display.list_modes()[0]
                        screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)

                    else:
                        init_width, init_height = WIDTH, HEIGHT
                        screen = pygame.display.set_mode((WIDTH, HEIGHT))

                    图像对象重新创建
                    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
                    pygame.Surface.convert(bg_suface)
                    bg_suface.fill(pygame.Color(0, 0, 0, 28))
                    column = int(init_width / suface_height)
                    drops = [0 for i in range(column)]
                elif event.key == pygame.K_ESCAPE:
                    按ESC退出
                    exit()
        延时
        pygame.time.delay(17)

        图像对象放到窗口的原点坐标上
        screen.blit(bg_suface, (0, 0))

        for i in range(len(drops)):
            随机字符
            text = random.choice(texts)

            把字符画到该列的下雨的位置
            screen.blit(text, (i * suface_height, drops[i] * suface_height))

            # 更新下雨的坐标
            drops[i] += 1

            超过界面高度或随机数,下雨位置置0
            if drops[i] * suface_height > init_height or random.random() > 0.95:
                drops[i] = 0

        更新画面
        pygame.display.flip()

if __name__ == '__main__':
    main()

源码:

import pygame
import random


def main():
    # 初始化pygame
    pygame.init()

    # 默认不全屏
    fullscreen = False
    # 窗口未全屏宽和高
    WIDTH, HEIGHT = 1100, 600

    init_width, init_height = WIDTH, HEIGHT

    # 字块大小,宽,高
    suface_height = 18
    # 字体大小
    font_size = 20

    # 创建一个窗口
    screen = pygame.display.set_mode((init_width, init_height))

    # 字体
    font = pygame.font.Font('MSYHMONO.ttf', font_size)

    # 创建一个图像对象
    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
    pygame.Surface.convert(bg_suface)
    bg_suface.fill(pygame.Color(0, 0, 0, 28))

    # 用纯色填充背景
    screen.fill((0, 0, 0))

    # 显示的字符
    letter = ['qe','q', '6w', 'e','3', 'r', '2','7t', '101y', 'u', '687i', 'o', 'p6', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x',
              'c',
              'v', 'b', 'n', 'm','1','0']
    texts = [
        font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
    ]

    # 也可以替换成0 1 显示
    # texts = [
    #     font.render('0',True,(0,255,0)),font.render('1',True,(0,255,0))
    # ]

    # 生成的列数
    column = int(init_width / suface_height)
    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:
                # 按F11切换全屏,或窗口
                if event.key == pygame.K_F11:
                    print("检测到按键F11")
                    fullscreen = not fullscreen
                    if fullscreen:
                        # 全屏效果,参数重设
                        size = init_width, init_height = pygame.display.list_modes()[0]
                        screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)

                    else:
                        init_width, init_height = WIDTH, HEIGHT
                        screen = pygame.display.set_mode((WIDTH, HEIGHT))

                    # 图像对象重新创建
                    bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
                    pygame.Surface.convert(bg_suface)
                    bg_suface.fill(pygame.Color(0, 0, 0, 28))
                    column = int(init_width / suface_height)
                    drops = [0 for i in range(column)]
                elif event.key == pygame.K_ESCAPE:
                    # 按ESC退出
                    exit()
        # 延时
        pygame.time.delay(14)

        # 图像对象放到窗口的原点坐标上
        screen.blit(bg_suface, (0, 0))

        for i in range(len(drops)):
            # 随机字符
            text = random.choice(texts)

            # 把字符画到该列的下雨的位置
            screen.blit(text, (i * suface_height, drops[i] * suface_height))

            # 更新下雨的坐标
            drops[i] += 1

            # 超过界面高度或随机数,下雨位置置0
            if drops[i] * suface_height > init_height or random.random() > 0.95:
                drops[i] = 0

        # 更新画面
        pygame.display.flip()


if __name__ == '__main__':
    main()

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tuokoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值