模拟沙尘暴

模拟沙尘暴:运用 Pygame,创建许多随机移动的小颗粒,模拟沙尘暴中沙尘弥漫的场景,可调整颗粒的数量和移动速度。

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("沙尘暴模拟")

# 初始沙尘颗粒的数量和移动速度
particle_count = 500
particle_speed = 2
wind_direction = [1, 1]

# 创建沙尘颗粒列表
particles = []
for _ in range(particle_count):
    x = random.randint(0, screen_width)
    y = random.randint(0, screen_height)
    particles.append([x, y])


# 按钮类
class Button:
    def __init__(self, x, y, width, height, text, action):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.action = action
        # 指定支持中文的字体,这里使用系统默认中文字体
        self.font = pygame.font.SysFont("simhei", 36)
        self.text_surface = self.font.render(text, True, (255, 255, 255))

    def draw(self, screen):
        pygame.draw.rect(screen, (100, 100, 100), self.rect)
        screen.blit(self.text_surface, (self.rect.x + 10, self.rect.y + 10))

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                self.action()


# 增加颗粒数量
def increase_particles():
    global particle_count
    particle_count = min(1000, particle_count + 100)
    for _ in range(100):
        x = random.randint(0, screen_width)
        y = random.randint(0, screen_height)
        particles.append([x, y])


# 减少颗粒数量
def decrease_particles():
    global particle_count
    particle_count = max(100, particle_count - 100)
    while len(particles) > particle_count:
        particles.pop()


# 增加速度
def increase_speed():
    global particle_speed
    particle_speed = min(5, particle_speed + 1)


# 减少速度
def decrease_speed():
    global particle_speed
    particle_speed = max(1, particle_speed - 1)


# 改变风的方向
def change_wind_direction():
    global wind_direction
    directions = [[1, 1], [-1, 1], [1, -1], [-1, -1]]
    current_index = directions.index(wind_direction)
    new_index = (current_index + 1) % len(directions)
    wind_direction = directions[new_index]


# 创建按钮
buttons = [
    Button(10, 10, 150, 50, "增加颗粒数量", increase_particles),
    Button(170, 10, 150, 50, "减少颗粒数量", decrease_particles),
    Button(330, 10, 150, 50, "增加速度", increase_speed),
    Button(490, 10, 150, 50, "减少速度", decrease_speed),
    Button(650, 10, 150, 50, "改变风的方向", change_wind_direction)
]

# 主循环
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        for button in buttons:
            button.handle_event(event)

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

    # 更新和绘制沙尘颗粒
    for particle in particles:
        # 根据风的方向和速度移动
        particle[0] += wind_direction[0] * random.randint(1, particle_speed)
        particle[1] += wind_direction[1] * random.randint(1, particle_speed)

        # 边界检查
        if particle[0] < 0:
            particle[0] = screen_width
        elif particle[0] > screen_width:
            particle[0] = 0
        if particle[1] < 0:
            particle[1] = screen_height
        elif particle[1] > screen_height:
            particle[1] = 0

        # 绘制颗粒
        pygame.draw.circle(screen, (255, 255, 0), (int(particle[0]), int(particle[1])), 1)

    # 绘制按钮
    for button in buttons:
        button.draw(screen)

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出 Pygame
pygame.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值