VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • 用Python简单实现模拟太阳系运转

1. 代码实现

需要安装依赖包:pygame

篇幅原因,这里仅展示部分代码。

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
            # 背景颜色为黑色
    screen.fill(BLACK)
    # 画太阳
    pygame.draw.circle(screen, YELLOW, position, 60, 0)
    # 画地球
    roll_e += 0.01  # 假设地球每帧公转 0.01 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))
    pygame.draw.circle(screen, BLUE, (pos_e_x, pos_e_y), 15, 0)
    # 地球的轨迹线
    pos_e.append((pos_e_x, pos_e_y))
    if len(pos_e) > 255:
        pos_e.pop(0)
    for i in range(len(pos_e)):
        pygame.draw.circle(screen, SILVER, pos_e[i], 1, 0)
    # 画月球
    roll_m += 0.1
    pos_m_x = int(pos_e_x + size[1] // 20 * math.sin(roll_m))
    pos_m_y = int(pos_e_y + size[1] // 20 * math.cos(roll_m))
    pygame.draw.circle(screen, WHITE, (pos_m_x, pos_m_y), 8, 0)
    # 月球的轨迹线
    pos_mm.append((pos_m_x, pos_m_y))
    if len(pos_mm) > 255:
        pos_mm.pop(0)
    for i in range(len(pos_mm)):
        pygame.draw.circle(screen, SILVER, pos_mm[i], 1, 0)
    # 画金星
    roll_v += 0.015
    pos_v_x = int(size[0] // 2 + size[1] // 3 * math.sin(roll_v))
    pos_v_y = int(size[1] // 2 + size[1] // 3 * math.cos(roll_v))
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)
    # 金星的轨迹线
    pos_v.append((pos_v_x, pos_v_y))
    if len(pos_v) > 255:
        pos_v.pop(0)
    for i in range(len(pos_v)):
        pygame.draw.circle(screen, SILVER, pos_v[i], 1, 0)
    # 刷新
    pygame.display.flip()
    # 数值越大刷新越快,小球运动越快
    clock.tick(40)

2. 效果图

 

看着这公转的太阳,是不是很炫酷。

 

出  处:https://www.cnblogs.com/python147/p/14485828.html


相关教程