I want to make solar system only has two properties that the earth moves around the sun and the moon moves around the earth continuously in pygame with python code. Here is my code:
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()
In this code, the earth moves around the sun. its looks fine. now i want to moves moon around the earth which continuously moves around the earth and sun. i set the earth moves sun in 365 days in a full round, in moon it will be moves around the earth in 27 days.
For Details (That's original idea i want to develop):
orbital speed: earth around the sun in 365 days, moon around the sun in 27 days,
spinning speed: the sun 1 round in 30 days, earth 1 round in i day, moon always facing the earth I have done this. But how moon moves around the sun the idea is still not clear to me.