### 复杂的 Python 浪漫星空动态效果代码
以下是一个更加复杂的浪漫星空动态效果代码,它不仅包含了星星的闪烁和移动,还加入了行星轨道、彗星拖尾以及背景渐变等功能。该代码基于 `pygame` 和 `numpy` 库实现。
```python
import pygame
import numpy as np
import random
import sys
from math import sin, cos, radians
# 初始化 Pygame
pygame.init()
# 屏幕设置
screen_width, screen_height = 1200, 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Complex Romantic Starry Sky Animation")
# 颜色定义
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
# 背景渐变函数
def gradient_background(surface):
for y in range(screen_height):
r = int(y / screen_height * 255)
g = 0
b = int((screen_height - y) / screen_height * 255)
color = (r, g, b)
pygame.draw.line(surface, color, (0, y), (screen_width, y))
# 星星类
class Star:
def __init__(self):
self.x = random.randint(0, screen_width)
self.y = random.randint(0, screen_height)
self.size = random.choice([1, 2])
self.speed_x = random.uniform(-0.5, 0.5)
self.speed_y = random.uniform(-0.5, 0.5)
def update(self):
self.x += self.speed_x
self.y += self.speed_y
if self.x > screen_width or self.x < 0:
self.x = max(min(self.x, screen_width), 0)
self.speed_x *= -1
if self.y > screen_height or self.y < 0:
self.y = max(min(self.y, screen_height), 0)
self.speed_y *= -1
def draw(self, surface):
pygame.draw.circle(surface, white, (int(self.x), int(self.y)), self.size)
# 彗星类
class Comet:
def __init__(self):
self.angle = random.uniform(0, 360)
self.radius = random.uniform(100, 300)
self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
self.tail_length = 20
self.positions = []
def update(self):
x = screen_width // 2 + self.radius * cos(radians(self.angle))
y = screen_height // 2 + self.radius * sin(radians(self.angle))
self.positions.append((x, y))
if len(self.positions) > self.tail_length:
self.positions.pop(0)
self.angle -= 1
def draw(self, surface):
for i, pos in enumerate(self.positions[::-1]):
alpha = int(i / self.tail_length * 255)
s = pygame.Surface((4, 4)).convert_alpha()
s.fill((*self.color, alpha))
surface.blit(s, (pos[0]-2, pos[1]-2))
# 创建星星和彗星
num_stars = 200
stars = [Star() for _ in range(num_stars)]
comets = [Comet() for _ in range(5)]
# 主循环
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 绘制背景
gradient_background(screen)
# 更新和绘制星星
for star in stars:
star.update()
star.draw(screen)
# 更新和绘制彗星
for comet in comets:
comet.update()
comet.draw(screen)
pygame.display.flip()
clock.tick(30)
pygame.quit()
sys.exit()
```
---
#### 解释
1. **背景渐变**
使用 `gradient_background` 函数实现了从蓝色到黑色的垂直渐变背景,增强了星空的真实感[^1]。
2. **星星行为**
每颗星星都有自己的速度向量 (`speed_x`, `speed_y`),使其能够在屏幕上自由漂移,并在碰到边界时反弹[^2]。
3. **彗星拖尾效果**
彗星通过记录其历史位置并逐渐降低透明度的方式实现了自然的拖尾效果。每条尾巴由多个半透明的小方块组成,形成了平滑过渡的效果[^3]。
4. **性能优化**
将所有绘图操作集中在一个主循环中完成,减少了不必要的计算开销;同时控制帧率以保持流畅性[^4]。
---
#### 注意事项
- 运行此代码前需确保已安装 `pygame` 和 `numpy` 库。如果未安装,可使用以下命令进行安装:
```bash
pip install pygame numpy
```
- 可根据个人喜好调整参数(如星星数量、彗星长度等),从而获得不同风格的星空效果[^5]。
---
###