Python之代码雨代码实现001

Python之代码雨代码实现001


下面是一个简单的使用 Python 的 pygame 库实现的代码雨。这个示例只是一个简单的版本,没有很多高级功能,但它可以作为一个起点,你可以在此基础上添加更多功能。

import os
import sys

import pygame
import random
# 初始化
pygame.init()
# font = pygame.font.SysFont('宋体', 25)
# 默认不全屏
fullscreen = False
# 字块大小,宽,高
suface_height = 18
# 字体大小
font_size = 20
# 字体
font = pygame.font.SysFont('宋体', font_size)
# 窗口未全屏宽和高
WIDTH, HEIGHT = 1100, 600
init_width, init_height = WIDTH, HEIGHT
# 窗口
screen = None
# 图像对象
surface = None


def main(argv):
    global fullscreen,screen,surface
    # screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    # screenwidth = screen.get_width()
    # screenheight = screen.get_height()
    # surface = pygame.Surface((screenwidth, screenheight), pygame.SRCALPHA)
    # pygame.Surface.convert(surface)


    screen_setup()

    # 内容
    str = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(97, 123)] 
    texts = [font.render(i, True, (0, 255, 0)) for i in str]
    lst = list(range(99))
    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
                    screen_setup()
                elif event.key == pygame.K_ESCAPE:
                    # 按ESC退出
                    exit()

        pygame.time.delay(50)
        screen.blit(surface, (0, 0))
        for i in range(len(lst)):
            text = random.choice(texts)
            screen.blit(text, (i * 20, lst[i] * 20))
            lst[i] += 1
            if random.random() < 0.05:
                lst[i] = 0
        pygame.display.flip()



# 全屏效果,参数重设
def screen_setup():
    global fullscreen,screen,surface
    if fullscreen:
        # 全屏效果,参数重设
        size = init_width, init_height = pygame.display.list_modes()[0]
        screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
        # screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    else:
        init_width, init_height = WIDTH, HEIGHT
        screen = pygame.display.set_mode((WIDTH, HEIGHT))

    # 图像对象重新创建
    surface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)
    pygame.Surface.convert(surface)
    surface.fill((0, 0, 0, 10))
    screen.fill((0, 0, 0, 10))


if __name__ == "__main__":
    main(sys.argv[1:])


《AUTOSAR谱系分解(ETAS工具链)》之总目录

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值