今天我们用Python来模拟一下太阳系行星运动轨迹玩玩~
先上成品图(运行效果含音乐的呦)
想要实现这样的效果并不难
准备材料
首先我们需要准备这样一些材料
宇宙背景图
背景透明的行星图
编写代码
代码分块详解
导入需要的模块
import pygame
import sys
import math
from pygame.locals import \*
定义窗口大小、标题名称、字体设置、创建时钟
size = width, height = 1206, 780
screen = pygame.display.set\_mode(size)
pygame.display.set\_caption("太阳系行星运转示意图")
myfont = pygame.font.Font(None,60)
clock = pygame.time.Clock()
宇宙背景初始化
background = pygame.image.load(r" 这里填背景图片本地路径 ")
screen.blit(background, (0, 0))
右侧文字及星球显示
textImage = myfont.render("Solar System", True, (255, 255, 0)) # 太阳系
screen.blit(textImage, (100, 100))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Sun", True, (255, 0, 0), (0, 0, 0)) # 太阳
screen.blit(text\_surface, (1020, 30))
sun = pygame.image.load(r"F:\\solar-system\\image\\sun\_bg.png")
screen.blit(pygame.transform.scale(sun, (27, 27)), (1090, 25))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Mercury", True, (255, 0, 0), (0, 0, 0)) # 水星
screen.blit(text\_surface, (1020, 70))
my\_font = pygame.font.SysFont("arial", 15)
Mercury = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(Mercury, (27, 27)), (1090, 65))
text\_surface = my\_font.render("Venus", True, (255, 0, 0), (0, 0, 0)) # 金星
screen.blit(text\_surface, (1020, 110))
my\_font = pygame.font.SysFont("arial", 15)
spark = pygame.image.load(r"F:\\solar-system\\image\\spark\_bg.png")
screen.blit(pygame.transform.scale(spark, (27, 27)), (1090, 105))
text\_surface = my\_font.render("Earth", True, (255, 0, 0), (0, 0, 0)) # 地球
screen.blit(text\_surface, (1020, 150))
my\_font = pygame.font.SysFont("arial", 15)
earth = pygame.image.load(r"F:\\solar-system\\image\\earth\_min\_bg.png")
screen.blit(pygame.transform.scale(earth, (27, 27)), (1090, 145))
text\_surface = my\_font.render("Moon", True, (255, 0, 0), (0, 0, 0)) # 月球
screen.blit(text\_surface, (1020, 190))
my\_font = pygame.font.SysFont("arial", 15)
moon = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(moon, (27, 27)), (1090, 185))
text\_surface = my\_font.render("Mars", True, (255, 0, 0), (0, 0, 0)) # 火星
screen.blit(text\_surface, (1020, 230))
Mars = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(Mars, (27, 27)), (1090, 225))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Jupiter", True, (255, 0, 0), (0, 0, 0)) # 木星
screen.blit(text\_surface, (1020, 270))
Jupiter = pygame.image.load(r"F:\\solar-system\\image\\jupiter\_min\_bg.png")
screen.blit(pygame.transform.scale(Jupiter, (27, 27)), (1090, 265))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Saturn", True, (255, 0, 0), (0, 0, 0)) # 土星
screen.blit(text\_surface, (1020, 300))
Saturn = pygame.image.load(r"F:\\solar-system\\image\\saturn\_bg.png")
screen.blit(pygame.transform.scale(Saturn, (30, 30)), (1090, 305))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Uranus", True, (255, 0, 0), (0, 0, 0)) # 天王星
screen.blit(text\_surface, (1020, 340))
Uranus = pygame.image.load(r"F:\\solar-system\\image\\uranus\_bg.png")
screen.blit(pygame.transform.scale(Uranus, (27, 27)), (1090, 345))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Neptune", True, (255, 0, 0), (0, 0, 0)) # 海王星
screen.blit(text\_surface, (1020, 380))
Neptune = pygame.image.load(r"F:\\solar-system\\image\\neptune\_bg.png")
screen.blit(pygame.transform.scale(Neptune, (27, 27)), (1090, 385))
设定太阳初始位置
sun = pygame.image.load(r"F:/solar-system/image/sun\_bg.png")
screen.blit(pygame.transform.scale(sun, (170, 170)), (527,307))
实现各个星球绕着太阳转
# 水星
roll\_3 += 0.077 # 每帧公转pi
pos\_3\_x = int(size\[0\] // 2 + size\[1\] // 8 \* math.sin(roll\_3))
pos\_3\_y = int(size\[1\] // 2 + size\[1\] // 8 \* math.cos(roll\_3))
mercury = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos\_3\_x, pos\_3\_y))
# 金星
roll\_2 += 0.069 # 每帧公转pi
pos\_2\_x = int(size\[0\] // 2 + size\[1\] // 7 \* math.sin(roll\_2))
pos\_2\_y = int(size\[1\] // 2 + size\[1\] // 7 \* math.cos(roll\_2))
venus = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(venus, (10, 10)), (pos\_2\_x, pos\_2\_y))
# 地球
roll\_e += 0.060 # 每帧公转pi
pos\_e\_x = int(size\[0\] // 2 + size\[1\] // 6 \* math.sin(roll\_e))
pos\_e\_y = int(size\[1\] // 2 + size\[1\] // 6 \* math.cos(roll\_e))
earth = pygame.image.load(r"F:\\solar-system\\image\\earth\_min\_bg.png")
screen.blit(pygame.transform.scale(earth, (15, 15)), (pos\_e\_x, pos\_e\_y))
# 火星
roll\_4 += 0.053 # 每帧公转pi
pos\_4\_x = int(size\[0\] // 2 + size\[1\] // 5 \* math.sin(roll\_4))
pos\_4\_y = int(size\[1\] // 2 + size\[1\] // 5 \* math.cos(roll\_4))
venus = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(venus, (13, 13)), (pos\_4\_x, pos\_4\_y))
# 木星
roll\_5 += 0.045 # 每帧公转pi
pos\_5\_x = int(size\[0\] // 2 + size\[1\] // 4 \* math.sin(roll\_5))
pos\_5\_y = int(size\[1\] // 2 + size\[1\] // 4 \* math.cos(roll\_5))
mouth = pygame.image.load(r"F:\\solar-system\\image\\jupiter\_bg.png")
screen.blit(pygame.transform.scale(mouth, (70, 70)), (pos\_5\_x, pos\_5\_y))
# 土星
roll\_6 += 0.037 # 每帧公转pi
pos\_6\_x = int(size\[0\] // 2 + size\[1\] // 3.5 \* math.sin(roll\_6))
pos\_6\_y = int(size\[1\] // 2 + size\[1\] // 3.5 \* math.cos(roll\_6))
saturn = pygame.image.load(r"F:\\solar-system\\image\\saturn\_bg.png")
screen.blit(pygame.transform.scale(saturn, (50, 50)), (pos\_6\_x, pos\_6\_y))
# 天王星
roll\_7 += 0.031 # 每帧公转pi
pos\_7\_x = int(size\[0\] // 2 + size\[1\] // 2.7 \* math.sin(roll\_7))
pos\_7\_y = int(size\[1\] // 2 + size\[1\] // 2.7 \* math.cos(roll\_7))
uranus = pygame.image.load(r"F:\\solar-system\\image\\uranus\_bg.png")
screen.blit(pygame.transform.scale(uranus, (45, 45)), (pos\_7\_x, pos\_7\_y))
# 海王星
roll\_8 += 0.025 # 每帧公转pi
pos\_8\_x = int(size\[0\] // 2 + size\[1\] // 2 \* math.sin(roll\_8))
pos\_8\_y = int(size\[1\] // 2 + size\[1\] // 2 \* math.cos(roll\_8))
neptune = pygame.image.load(r"F:\\solar-system\\image\\neptune\_bg.png")
screen.blit(pygame.transform.scale(neptune, (37, 37)), (pos\_8\_x, pos\_8\_y))
实现月亮绕着地球转
roll\_m += 0.2 # 每帧公转pi
pos\_m\_x = int(pos\_e\_x + size\[1\] // 50 \* math.sin(roll\_m))
pos\_m\_y = int(pos\_e\_y + size\[1\] // 50 \* math.cos(roll\_m))
mouth = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(mouth, (6, 6)), (pos\_m\_x, pos\_m\_y))
如果你很有情调的话,还可以在循环体之前加一段代码,配曲music
注:此处音乐力求达到宏伟效果,因此选的是 Victory.mp3
# 添加背景音乐
pygame.mixer.music.load('F:/music.mp3')
pygame.mixer.music.play(-1, 40)
pygame.mixer.music.set\_volume(0.5)
总观整体代码
# 导入模块
import pygame
import sys
import math
from pygame.locals import \*
# 初始化
pygame.init()
# 定义窗口大小、标题名称、字体设置、创建时钟(可以控制游戏循环频率)等
size = width, height = 1206, 780
screen = pygame.display.set\_mode(size)
pygame.display.set\_caption("太阳系行星运转示意图")
myfont = pygame.font.Font(None,60)
clock = pygame.time.Clock()
# 定义三个空列表
pos\_e = pos\_mm = \[\]
# 地球和月球等其他行星的公转过的角度
roll\_e = roll\_m = 0
roll\_2 = roll\_3 = roll\_4 = roll\_5 = roll\_6 = roll\_7 = roll\_8 = 0
# 添加背景音乐
pygame.mixer.music.load('F:/music.mp3')
pygame.mixer.music.play(-1, 40)
pygame.mixer.music.set\_volume(0.5)
# 循环,达到万事万物永不停息的目的
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 宇宙
background = pygame.image.load(r"E:/3.PNG")
screen.blit(background, (0, 0))
# 显示文字及星球
textImage = myfont.render("Solar System", True, (255, 255, 0)) # 太阳系
screen.blit(textImage, (100, 100))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Sun", True, (255, 0, 0), (0, 0, 0)) # 太阳
screen.blit(text\_surface, (1020, 30))
sun = pygame.image.load(r"F:\\solar-system\\image\\sun\_bg.png")
screen.blit(pygame.transform.scale(sun, (27, 27)), (1090, 25))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Mercury", True, (255, 0, 0), (0, 0, 0)) # 水星
screen.blit(text\_surface, (1020, 70))
my\_font = pygame.font.SysFont("arial", 15)
Mercury = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(Mercury, (27, 27)), (1090, 65))
text\_surface = my\_font.render("Venus", True, (255, 0, 0), (0, 0, 0)) # 金星
screen.blit(text\_surface, (1020, 110))
my\_font = pygame.font.SysFont("arial", 15)
spark = pygame.image.load(r"F:\\solar-system\\image\\spark\_bg.png")
screen.blit(pygame.transform.scale(spark, (27, 27)), (1090, 105))
text\_surface = my\_font.render("Earth", True, (255, 0, 0), (0, 0, 0)) # 地球
screen.blit(text\_surface, (1020, 150))
my\_font = pygame.font.SysFont("arial", 15)
earth = pygame.image.load(r"F:\\solar-system\\image\\earth\_min\_bg.png")
screen.blit(pygame.transform.scale(earth, (27, 27)), (1090, 145))
text\_surface = my\_font.render("Moon", True, (255, 0, 0), (0, 0, 0)) # 月球
screen.blit(text\_surface, (1020, 190))
my\_font = pygame.font.SysFont("arial", 15)
moon = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(moon, (27, 27)), (1090, 185))
text\_surface = my\_font.render("Mars", True, (255, 0, 0), (0, 0, 0)) # 火星
screen.blit(text\_surface, (1020, 230))
Mars = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(Mars, (27, 27)), (1090, 225))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Jupiter", True, (255, 0, 0), (0, 0, 0)) # 木星
screen.blit(text\_surface, (1020, 270))
Jupiter = pygame.image.load(r"F:\\solar-system\\image\\jupiter\_min\_bg.png")
screen.blit(pygame.transform.scale(Jupiter, (27, 27)), (1090, 265))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Saturn", True, (255, 0, 0), (0, 0, 0)) # 土星
screen.blit(text\_surface, (1020, 300))
Saturn = pygame.image.load(r"F:\\solar-system\\image\\saturn\_bg.png")
screen.blit(pygame.transform.scale(Saturn, (30, 30)), (1090, 305))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Uranus", True, (255, 0, 0), (0, 0, 0)) # 天王星
screen.blit(text\_surface, (1020, 340))
Uranus = pygame.image.load(r"F:\\solar-system\\image\\uranus\_bg.png")
screen.blit(pygame.transform.scale(Uranus, (27, 27)), (1090, 345))
my\_font = pygame.font.SysFont("arial", 15)
text\_surface = my\_font.render("Neptune", True, (255, 0, 0), (0, 0, 0)) # 海王星
screen.blit(text\_surface, (1020, 380))
Neptune = pygame.image.load(r"F:\\solar-system\\image\\neptune\_bg.png")
screen.blit(pygame.transform.scale(Neptune, (27, 27)), (1090, 385))
# 太阳
sun = pygame.image.load(r"F:/solar-system/image/sun\_bg.png")
screen.blit(pygame.transform.scale(sun, (170, 170)), (527,307))
# 水星
roll\_3 += 0.077 # 每帧公转pi
pos\_3\_x = int(size\[0\] // 2 + size\[1\] // 8 \* math.sin(roll\_3))
pos\_3\_y = int(size\[1\] // 2 + size\[1\] // 8 \* math.cos(roll\_3))
mercury = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos\_3\_x, pos\_3\_y))
# 金星
roll\_2 += 0.069 # 每帧公转pi
pos\_2\_x = int(size\[0\] // 2 + size\[1\] // 7 \* math.sin(roll\_2))
pos\_2\_y = int(size\[1\] // 2 + size\[1\] // 7 \* math.cos(roll\_2))
venus = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(venus, (10, 10)), (pos\_2\_x, pos\_2\_y))
# 地球
roll\_e += 0.060 # 每帧公转pi
pos\_e\_x = int(size\[0\] // 2 + size\[1\] // 6 \* math.sin(roll\_e))
pos\_e\_y = int(size\[1\] // 2 + size\[1\] // 6 \* math.cos(roll\_e))
earth = pygame.image.load(r"F:\\solar-system\\image\\earth\_min\_bg.png")
screen.blit(pygame.transform.scale(earth, (15, 15)), (pos\_e\_x, pos\_e\_y))
# 月球
roll\_m += 0.2 # 每帧公转pi
pos\_m\_x = int(pos\_e\_x + size\[1\] // 50 \* math.sin(roll\_m))
pos\_m\_y = int(pos\_e\_y + size\[1\] // 50 \* math.cos(roll\_m))
mouth = pygame.image.load(r"F:\\solar-system\\image\\mercury\_bg.png")
screen.blit(pygame.transform.scale(mouth, (6, 6)), (pos\_m\_x, pos\_m\_y))
# 火星
roll\_4 += 0.053 # 每帧公转pi
pos\_4\_x = int(size\[0\] // 2 + size\[1\] // 5 \* math.sin(roll\_4))
pos\_4\_y = int(size\[1\] // 2 + size\[1\] // 5 \* math.cos(roll\_4))
venus = pygame.image.load(r"F:\\solar-system\\image\\venus\_bg.png")
screen.blit(pygame.transform.scale(venus, (13, 13)), (pos\_4\_x, pos\_4\_y))
# 木星
roll\_5 += 0.045 # 每帧公转pi
pos\_5\_x = int(size\[0\] // 2 + size\[1\] // 4 \* math.sin(roll\_5))
pos\_5\_y = int(size\[1\] // 2 + size\[1\] // 4 \* math.cos(roll\_5))
mouth = pygame.image.load(r"F:\\solar-system\\image\\jupiter\_bg.png")
screen.blit(pygame.transform.scale(mouth, (70, 70)), (pos\_5\_x, pos\_5\_y))
# 土星
roll\_6 += 0.037 # 每帧公转pi
pos\_6\_x = int(size\[0\] // 2 + size\[1\] // 3.5 \* math.sin(roll\_6))
pos\_6\_y = int(size\[1\] // 2 + size\[1\] // 3.5 \* math.cos(roll\_6))
saturn = pygame.image.load(r"F:\\solar-system\\image\\saturn\_bg.png")
screen.blit(pygame.transform.scale(saturn, (50, 50)), (pos\_6\_x, pos\_6\_y))
# 天王星
roll\_7 += 0.031 # 每帧公转pi
pos\_7\_x = int(size\[0\] // 2 + size\[1\] // 2.7 \* math.sin(roll\_7))
pos\_7\_y = int(size\[1\] // 2 + size\[1\] // 2.7 \* math.cos(roll\_7))
uranus = pygame.image.load(r"F:\\solar-system\\image\\uranus\_bg.png")
screen.blit(pygame.transform.scale(uranus, (45, 45)), (pos\_7\_x, pos\_7\_y))
# 海王星
roll\_8 += 0.025 # 每帧公转pi
pos\_8\_x = int(size\[0\] // 2 + size\[1\] // 2 \* math.sin(roll\_8))
pos\_8\_y = int(size\[1\] // 2 + size\[1\] // 2 \* math.cos(roll\_8))
neptune = pygame.image.load(r"F:\\solar-system\\image\\neptune\_bg.png")
screen.blit(pygame.transform.scale(neptune, (37, 37)), (pos\_8\_x, pos\_8\_y))
# 刷新
pygame.display.flip()
# 数值越大刷新越快,小球运动越快
clock.tick(50)
**点击下方安全链接前往获取**
**[CSDN大礼包:《Python入门&进阶学习资源包》免费分享](https://mp.weixin.qq.com/s/b5jwyLC8FsKhg6O8ynyPUA)**
### 👉**Python实战案例**👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FicibaTNbiaaf5GZ01GhIvmG8eqmrXtRDHVyqEcxswMgOmYDTmNdDrWWyaDsNEvG5areslHPV3tJLqVsY6fSziaVkeA%2F640%3Fwx_fmt%3Djpeg%26wxfrom%3D5%26wx_lazy%3D1%26wx_co%3D1&pos_id=img-AZS0Hiky-1710576210090)
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_png%2FicibaTNbiaaf5GZ01GhIvmG8eqmrXtRDHVy3pViaRsib6Pibjl3LIJpqpJTPehla4icET8dy1XDhVzV0zUUDJB1JDlgEA%2F640%3Fwx_fmt%3Dpng%26wxfrom%3D5%26wx_lazy%3D1%26wx_co%3D1&pos_id=img-z1qbxTBG-1710576210090)
### **👉Python书籍和视频合集**👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_png%2FicibaTNbiaaf5GZ01GhIvmG8eqmrXtRDHVyI7ovQLLPjfM60RlRiaPEcuBXIxh14kdoEOQmoph6pJupz2f5N0zvazA%2F640%3Fwx_fmt%3Dpng%26wxfrom%3D5%26wx_lazy%3D1%26wx_co%3D1&pos_id=img-VN1JmBVQ-1710576210091)
### 👉**Python副业创收路线**👈
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_png%2FicibaTNbiaaf5GZ01GhIvmG8eqmrXtRDHVyx3KOraNbYAYc8P5exNVFQAS72SpiaygqiaDzsFUsYS0w9LRp4WI4vrJw%2F640%3Fwx_fmt%3Dpng%26wxfrom%3D5%26wx_lazy%3D1%26wx_co%3D1&pos_id=img-fm1yXfsa-1710576210091)
**这些资料都是非常不错的**,朋友们如果有需要《Python学习路线&学习资料》,**点击下方安全链接前往获取**
**[CSDN大礼包:《Python入门&进阶学习资源包》免费分享](https://mp.weixin.qq.com/s/b5jwyLC8FsKhg6O8ynyPUA)**
本文转自网络,如有侵权,请联系删除。