【问题标题】:Earth and moon orbit arround the sun in pygame with python地球和月球轨道在pygame中用python围绕太阳
【发布时间】:2018-03-15 10:50:34
【问题描述】:

我想用python代码在pygame中使太阳系只有两个属性,即地球围绕太阳旋转和月亮围绕地球连续旋转。这是我的代码:

import sys, random, math
import pygame
from pygame.locals import *
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
grey = 125, 125, 125
red = 255, 0, 0
blue = 0, 0, 255
width = 500
height = 500
screen = pygame.display.set_mode((width,height))
screen.fill(white)
clock = pygame.time.Clock()
font = pygame.font.Font(None, 15)
def rotate2D(x,y,angle):
    angle = 0.0174532925*angle
    x_r = x*math.cos(angle) - y*math.sin(angle)
    y_r = x*math.sin(angle) + y*math.cos(angle)
    return (x_r, y_r)
def translate2D(x,y,tx,ty):
    x_t = x+tx
    y_t = y+ty
    return (x_t, y_t)
def show_FPS():
    text = font.render('day = ' + '{:.2f}'.format(day) + ', ' + str(int(clock.get_fps())) + ' FPS', True, black, white)
    textRect = text.get_rect()
    textRect.bottomright = screen.get_rect().bottomright
    screen.blit(text, textRect)
sun_radius = 70
earth_radius = 15
ex = 0
ey = 0
moon_radius = 10
mx = 0
my = 0
sun2earth_dis = 200
moon2earth_dis = 90
sun2earth_orbit = 0
moon2earth_orbit = 0
day = 0
flag = 1
while flag:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            flag = 0
        if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
            flag = 0
    screen.fill(white)
    day = day+0.2
    sun2earth_orbit = -(day*360.0)/365
    moon2earth_orbit = -(day*360.0)/27
    ex_, ey_ = translate2D(ex, ey, sun2earth_dis, 0)
    ex_, ey_ = translate2D(ex_, ey_, width/2, height/2) 
    mx_, my_ = translate2D(mx, my, moon2earth_dis, 0)
    mx_, my_ = rotate2D(mx_, my_, moon2earth_orbit)
    mx_, my_ = translate2D(mx_, my_, width/2, height/2)
    pygame.draw.circle(screen, red, (int(width/2), int(height/2)), sun_radius)
    pygame.draw.circle(screen, blue, (int(ex_), int(ey_)), earth_radius)
    pygame.draw.circle(screen, black, (int(mx_), int(my_)), moon_radius)
    show_FPS()
    pygame.display.flip()
pygame.quit()

在这段代码中,地球围绕太阳运行。它看起来不错。现在我想围绕地球移动月球,它不断地围绕地球和太阳移动。我设定地球绕太阳一周转一圈是 365 天,月球绕地球转一圈是 27 天。

详情(这是我想开发的原始想法):

轨道速度: 地球绕太阳一周,365天, 27天绕太阳一周,

纺纱速度: 太阳在 30 天内转一圈, 第 i 天地球 1 圈, 月亮总是对着地球 我已经做到了。但是月亮如何围绕太阳运行这个想法我仍然不清楚。

【问题讨论】:

  • 在这种情况下,您可能需要学习一些matrix math!秘诀是认识到您可以通过将多个单独的变换矩阵相乘来将多个旋转/缩放/变换组合在一起。如果不清楚,我可以在答案中详细说明。

标签: python python-3.x pygame


【解决方案1】:

尝试计算月球与地球在 [0, 0] 处的坐标,然后将其添加到地球的当前坐标中。

【讨论】:

  • 请您解释一下。
  • 所以,暂时忽略太阳,只考虑中心的地球模型。在这种情况下,您将描述月球相对于地球的位置变化 [0,0]。如果您不考虑太阳对月球的任何引力,您似乎没有考虑,并且只打算将其描述为一个简单的模型,那么您要做的就是 1.) 计算月球的位置,就像地球在中心 [0,0] 和 2。)将其移动到当前坐标(将地球实际 [x,y] 添加到它)。
  • 并在某种程度上扩展@CodeSurgeon 所说的内容。对于这个简单的系统,您不需要矩阵。他们实际上使这样一个简单的系统过于复杂。但是,如果您打算描述更复杂的系统,您应该研究一下。它们不仅使描述更容易,而且在计算速度方面也更快,特别是如果您可以使用专为快速计算向量和矩阵而设计的向量处理器。这允许您在合理的时间内通常无法进行的快速计算。
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
相关资源
最近更新 更多