太阳系行星运转示意图

太阳系行星运转示意图

前言

python代码、网络代码修改(改变字体、大小、添加运行轨迹)

一、pygame是什么?

pygame是一个设计用来开发游戏的python模块,在SDL库的基础上提供了各种接口,从而使用用户能够使用python语言创建各种各样的游戏或多媒体程序。

二、使用步骤

1.功能说明

1)导入模块
2)初始化
3)定义窗口大小、标题名称、字体设置、创建时钟(可以控制游戏循环频率)等
4)定义三个空列表
5)循环,达到万事万物永不停息的目的
6)刷新

2.改编说明

1)pygame在屏幕上显示中文的方法
在pygame中显示中文是一件较为麻烦的事,在要求不高的情况下可以使用统自带的字体:

 my_font = pygame.font.SysFont("字体", 字体大小)

2)添加运行轨迹
根据原来代码的星球运行轨迹的计算方法以及图片的参数绘制圆轨道:

#绘制一个圆
   color = 255,255,0
   position = 612,392
   radius = int(size[1]//8*1.0)
   width = 1
   #screen.blit(textImage,(100,100))#进行绘制
   pygame.draw.circle(screen,color,position,radius,width) 

2.完整代码

代码如下(示例):

# 导入模块
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
 
 
# 循环,达到万事万物永不停息的目的
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
 
    # 宇宙
    background = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\星空.jpg")
    screen.blit(background, (0, 0))
 
    # 显示文字及星球
    my_font = pygame.font.SysFont("SimHei", 35)
    text_surface = my_font.render("太阳系行星运转示意图", True, (255, 255, 0))           # 太阳系
    screen.blit(text_surface, (100, 100))
    my_font = pygame.font.SysFont("SimHei", 25)
    text_surface = my_font.render("太阳", True,(230,230,230), (0, 0, 0))       # 太阳
    screen.blit(text_surface, (1020, 30))
    sun = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\太阳.jpg")
    screen.blit(pygame.transform.scale(sun, (27, 27)), (1090, 25))
    
    text_surface = my_font.render("水星", True, (230,230,230), (0, 0, 0))   # 水星
    screen.blit(text_surface, (1020, 70))
    
    Mercury = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\水星.jpg")
    screen.blit(pygame.transform.scale(Mercury, (27, 27)), (1090, 65))
    text_surface = my_font.render("金星", True, (230,230,230), (0, 0, 0))     # 金星
    screen.blit(text_surface, (1020, 110))
   
    spark = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\金星.jpg")
    screen.blit(pygame.transform.scale(spark, (27, 27)), (1090, 105))
    text_surface = my_font.render("地球", True, (230,230,230), (0, 0, 0))     # 地球
    screen.blit(text_surface, (1020, 150))
   
    earth = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\地球.jpg")
    screen.blit(pygame.transform.scale(earth, (27, 27)), (1090, 145))
    text_surface = my_font.render("月球", True, (230,230,230), (0, 0, 0))      # 月球
    screen.blit(text_surface, (1020, 190))
    
    moon = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\月球.jpg")
    screen.blit(pygame.transform.scale(moon, (27, 27)), (1090, 185))
    text_surface = my_font.render("火星", True, (230,230,230), (0, 0, 0))      # 火星
    screen.blit(text_surface, (1020, 230))
    Mars = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\火星.jpg")
    screen.blit(pygame.transform.scale(Mars, (27, 27)), (1090, 225))
    
    text_surface = my_font.render("木星", True, (230,230,230), (0, 0, 0))   # 木星
    screen.blit(text_surface, (1020, 270))
    Jupiter = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\木星.jpg")
    screen.blit(pygame.transform.scale(Jupiter, (27, 27)), (1090, 265))
  
    text_surface = my_font.render("土星", True, (230,230,230), (0, 0, 0))    # 土星
    screen.blit(text_surface, (1020, 300))
    Saturn = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\土星.jpg")
    screen.blit(pygame.transform.scale(Saturn, (30, 30)), (1090, 305))
   
    text_surface = my_font.render("天王星", True, (230,230,230), (0, 0, 0))    # 天王星
    screen.blit(text_surface, (1020, 340))
    Uranus = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\天王星.jpg")
    screen.blit(pygame.transform.scale(Uranus, (27, 27)), (1090, 345))
    
    text_surface = my_font.render("海王星", True, (230,230,230), (0, 0, 0))   # 海王星
    screen.blit(text_surface, (1020, 380))
    Neptune = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\海王星.jpg")
    screen.blit(pygame.transform.scale(Neptune, (27, 27)), (1090, 385))
 
    # 太阳
    sun = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\太阳.jpg")
    screen.blit(pygame.transform.scale(sun, (170, 170)), (527,307))

  
    # 水星
    roll_3 += 0.077  # 每帧公转pi
    pos_3_x = size[0] // 2 + size[1] // 8 * math.sin(roll_3)
    pos_3_y = size[1] // 2 + size[1] // 8 * math.cos(roll_3)
    mercury = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\水星.jpg")
    screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos_3_x, pos_3_y))
    #绘制一个圆
    color = 255,255,0
    position = 612,392
    radius = int(size[1]//8*1.0)
    width = 1
    #screen.blit(textImage,(100,100))#进行绘制
    pygame.draw.circle(screen,color,position,radius,width)
    
    # 金星
    roll_2 += 0.069  # 每帧公转pi
    pos_2_x = size[0] // 2 + size[1] // 7 * math.sin(roll_2)
    pos_2_y = size[1] // 2 + size[1] // 7 * math.cos(roll_2)
    venus = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\金星.jpg")
    screen.blit(pygame.transform.scale(venus, (10, 10)), (pos_2_x, pos_2_y))
    radius = int(size[1]//7*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
    
    # 地球
    roll_e += 0.060  # 每帧公转pi
    pos_e_x = size[0] // 2 + size[1] // 6 * math.sin(roll_e)
    pos_e_y = size[1] // 2 + size[1] // 6 * math.cos(roll_e)
    earth = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\地球.jpg")
    screen.blit(pygame.transform.scale(earth, (15, 15)), (pos_e_x, pos_e_y))
    radius = int(size[1]//6*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
    

    # 月球
    roll_m += 0.2  # 每帧公转pi
    pos_m_x = pos_e_x + size[1] // 50 * math.sin(roll_m)
    pos_m_y = pos_e_y + size[1] // 50 * math.cos(roll_m)
    mouth = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\月球.jpg")
    screen.blit(pygame.transform.scale(mouth, (6, 6)), (pos_m_x, pos_m_y))
 
    # 火星
    roll_4 += 0.053  # 每帧公转pi
    pos_4_x = size[0] // 2 + size[1] // 5 * math.sin(roll_4)
    pos_4_y = size[1] // 2 + size[1] // 5 * math.cos(roll_4)
    venus = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\火星.jpg")
    screen.blit(pygame.transform.scale(venus, (13, 13)), (pos_4_x, pos_4_y))
    radius = int(size[1]//5*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
    
    # 木星
    roll_5 += 0.045  # 每帧公转pi
    pos_5_x = size[0] // 2 + size[1] // 4 * math.sin(roll_5)
    pos_5_y = size[1] // 2 + size[1] // 4 * math.cos(roll_5)
    mouth = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\木星.jpg")
    screen.blit(pygame.transform.scale(mouth, (25, 25)), (pos_5_x, pos_5_y))
    radius = int(size[1]//4*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
    
    # 土星
    roll_6 += 0.037  # 每帧公转pi
    pos_6_x = size[0] // 2 + size[1] // 3.5 * math.sin(roll_6)
    pos_6_y = size[1] // 2 + size[1] // 3.5 * math.cos(roll_6)
    saturn = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\土星.jpg")
    screen.blit(pygame.transform.scale(saturn, (25, 25)), (pos_6_x, pos_6_y))
    radius = int(size[1]//3.5*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
 
    # 天王星
    roll_7 += 0.031  # 每帧公转pi
    pos_7_x = size[0] // 2 + size[1] // 2.7 * math.sin(roll_7)
    pos_7_y = size[1] // 2 + size[1] // 2.7 * math.cos(roll_7)
    uranus = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\天王星.jpg")
    screen.blit(pygame.transform.scale(uranus, (25, 25)), (pos_7_x, pos_7_y))
    radius = int(size[1]//2.7*1.0)
    pygame.draw.circle(screen,color,position,radius,width)

 
    # 海王星
    roll_8 += 0.025  # 每帧公转pi
    pos_8_x = size[0] // 2 + size[1] // 2 * math.sin(roll_8)
    pos_8_y = size[1] // 2 + size[1] // 2 * math.cos(roll_8)
    neptune = pygame.image.load(r"C:\Users\Administrator\Pictures\Saved Pictures\海王星.jpg")
    screen.blit(pygame.transform.scale(neptune, (25, 25)), (pos_8_x, pos_8_y))
    radius = int(size[1]//2*1.0)
    pygame.draw.circle(screen,color,position,radius,width)
    

    # 刷新
    pygame.display.flip()
    # 数值越大刷新越快,小球运动越快
    clock.tick(50)

代码中使用的图片

图片:
冥王星冥王星

在这里插入图片描述太阳
在这里插入图片描述星空

在这里插入图片描述月亮

在这里插入图片描述地球
在这里插入图片描述水星
在这里插入图片描述土星
在这里插入图片描述天王星

在这里插入图片描述木星
在这里插入图片描述金星
在这里插入图片描述海王星
在这里插入图片描述火星

运行结果

在这里插入图片描述太阳系行星运转示意图

参考

[1]网址:https://blog.csdn.net/flyingpig2016/article/details/54021088
[2]何崇崇.知乎文章:用86行python代码模拟太阳系

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值