帮我写一个用python模拟太阳系行星运动的二维运动的代码
时间: 2024-06-12 16:08:27 浏览: 9
你可以使用Python来模拟太阳系行星的二维运动。下面是一个简单的示例代码:
```python
import pygame
import math
pygame.init()
# 定义窗口尺寸和背景颜色
screen = pygame.display.set_mode((800, 800))
background_color = (0, 0, 0)
# 定义太阳和行星的属性
sun_radius = 50
sun_color = (255, 255, 0)
planet_radius = [30, 40, 50, 60] # 行星半径
planet_distance = [200, 300, 400, 500] # 行星距离太阳的距离
planet_color = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 0, 255)] # 行星颜色
planet_angle = [0, 0, 0, 0] # 行星初始角度
planet_speed = [0.01, 0.02, 0.03, 0.04] # 行星运动速度
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 清空窗口
screen.fill(background_color)
# 绘制太阳
pygame.draw.circle(screen, sun_color, (400, 400), sun_radius)
# 绘制行星
for i in range(len(planet_radius)):
x = int(math.cos(planet_angle[i]) * planet_distance[i] + 400)
y = int(math.sin(planet_angle[i]) * planet_distance[i] + 400)
pygame.draw.circle(screen, planet_color[i], (x, y), planet_radius[i])
planet_angle[i] += planet_speed[i]
pygame.display.flip()
pygame.quit()
```
请注意,这只是一个简单的示例代码,仅仅模拟了太阳和几颗行星的二维运动。你可以根据需要自定义行星的数量、半径、距离和速度等属性来进行模拟。