今天我们用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)
关于Python学习指南
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!
👉Python所有方向的学习路线👈
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取)
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python70个实战练手案例&源码👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉Python大厂面试资料👈
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
👉Python副业兼职路线&方法👈
学好 Python 不论是就业还是做副业赚钱都不错,但要学会兼职接单还是要有一个学习规划。
👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方CSDN官方认证二维码或者点击链接免费领取【保证100%免费
】
![](https://i-blog.csdnimg.cn/blog_migrate/088c24ab19db804823f6bd35b56c98e4.png)