Python——代码雨

import pygame
import random

# 初始化Pygame
pygame.init()

# 设置屏幕宽度和高度
width, height = 1920, 1000
screen = pygame.display.set_mode((width, height))

# 定义颜色
BLACK = (0, 0, 0)

# 定义字符宽度和高度
char_width, char_height = 20, 30

# 定义代码雨的速度
speed = 15

# 定义代码雨的数量
num_chars = int(width / char_width)

# 创建字符列表
chars = []
for _ in range(num_chars):
    chars.append(chr(random.randint(33, 126)))

# 创建每个字符的位置列表
char_positions = []
for i in range(num_chars):
    char_positions.append(random.randint(-height, 10))

clock = pygame.time.Clock()

# 游戏循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 清屏
    screen.fill(BLACK)

    # 更新字符位置
    for i in range(num_chars):
        char_positions[i] += speed
        if char_positions[i] > height:
            char_positions[i] = random.randint(-height, 10)

        # 绘制字符
        char = chars[i]
        font = pygame.font.Font(None, char_height)
        text = font.render(char, True, (100, 255, 100))
        screen.blit(text, (i * char_width, char_positions[i]))

    # 更新屏幕
    pygame.display.flip()

    # 设置帧率
    clock.tick(300)

# 退出游戏
pygame.quit()

运行以上这段代码,你将看到屏幕上出现代码雨的效果。

每个字符都是随机生成的,并以给定的速度向下移动。

当一个字符移动到屏幕底部时,它将被重新放置在屏幕顶部,创建连续的代码雨效果。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值