今天我们用Python来模拟一下太阳系行星运动轨迹玩玩~
先上成品图(运行效果含音乐的呦)
想要实现这样的效果并不难
1、准备材料
首先我们需要准备这样一些材料
宇宙背景图
背景透明的行星图
2:编写代码
代码分块详解
导入需要的模块
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&