太阳系行星运转示意图
前言
python代码、网络代码修改(改变字体、大小、添加运行轨迹)
一、pygame是什么?
pygame是一个设计用来开发游戏的python模块,在SDL库的基础上提供了各种接口,从而使用用户能够使用python语言创建各种各样的游戏或多媒体程序。
文章目录
- 太阳系行星运转示意图
- 前言
- 一、pygame是什么?
- 二、使用步骤
- 1.功能说明
- 2.改编说明
- 2.完整代码
- 代码中使用的图片
- 运行结果
- 参考
二、使用步骤
1.功能说明
1)导入模块
2)初始化
3)定义窗口大小、标题名称、字体设置、创建时钟(可以控制游戏循环频率)等
4)定义三个空列表
5)循环,达到万事万物永不停息的目的
6)刷新
2.改编说明
1)pygame在屏幕上显示中文的方法
在pygame中显示中文是一件较为麻烦的事,在要求不高的情况下可以使用统自带的字体:
my_font = pygame.font.SysFont("字体", 字体大小)
2)添加运行轨迹
根据原来代码的星球运行轨迹的计算方法以及图片的参数绘制圆轨道:
#绘制一个圆color = 255,255,0position = 612,392radius = 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 # 每帧公转pipos_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,0position = 612,392radius = 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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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 # 每帧公转pipos_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代码模拟太阳系