python圣诞树代码动态粒子特效
时间: 2023-12-26 13:28:31 浏览: 125
以下是使用Python实现动态粒子特效的圣诞树代码:
```python
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("Christmas Tree")
# 定义颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 定义圣诞树参数
tree_width = 200
tree_height = 300
tree_x = screen_width // 2 - tree_width // 2
tree_y = screen_height - tree_height
# 定义粒子参数
particle_radius = 2
particle_speed = 2
particle_count = 100
# 创建粒子列表
particles = []
for _ in range(particle_count):
x = random.randint(tree_x, tree_x + tree_width)
y = random.randint(tree_y, screen_height)
particles.append([x, y])
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新粒子位置
for particle in particles:
particle[1] += particle_speed
if particle[1] > screen_height:
particle[0] = random.randint(tree_x, tree_x + tree_width)
particle[1] = random.randint(tree_y, screen_height)
# 绘制背景
screen.fill(WHITE)
# 绘制圣诞树
pygame.draw.rect(screen, GREEN, (tree_x, tree_y, tree_width, tree_height))
# 绘制粒子
for particle in particles:
pygame.draw.circle(screen, RED, particle, particle_radius)
# 更新屏幕
pygame.display.flip()
# 退出游戏
pygame.quit()
```
阅读全文