69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
import math
|
|
|
|
|
|
class SpaceObject:
|
|
def __init__(self, x, y, radius, color, mass):
|
|
self.x = x
|
|
self.y = y
|
|
self.radius = radius
|
|
self.color = color
|
|
self.mass = mass
|
|
|
|
def draw(self, screen, pygame):
|
|
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
|
|
|
|
|
|
class Star(SpaceObject):
|
|
def __init__(self, x, y, radius, color, name, mass):
|
|
super().__init__(x, y, radius, color, mass)
|
|
self.name = name
|
|
self.planets = []
|
|
|
|
def set_position(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
|
|
def add_planet(self, planet):
|
|
self.planets.append(planet)
|
|
|
|
def update(self):
|
|
for planet in self.planets:
|
|
planet.update()
|
|
|
|
def draw(self, screen, pygame, show_orbits=True):
|
|
if show_orbits:
|
|
for planet in self.planets:
|
|
pygame.draw.circle(screen, (80, 80, 95), (int(self.x), int(self.y)), int(planet.orbit_radius), 1)
|
|
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
|
|
for planet in self.planets:
|
|
planet.draw(screen, pygame)
|
|
|
|
|
|
class Planet(SpaceObject):
|
|
def __init__(self, star, orbit_number, orbit_radius, angle, angular_speed, direction, mass, radius=2):
|
|
self.star = star
|
|
self.orbit_number = orbit_number
|
|
self.orbit_radius = orbit_radius
|
|
self.angle = angle
|
|
self.angular_speed = angular_speed
|
|
self.direction = direction
|
|
self.linear_speed = self.angular_speed * self.orbit_radius
|
|
x, y = self.calculate_position()
|
|
super().__init__(x, y, radius, self.make_color(orbit_number), mass)
|
|
|
|
def make_color(self, orbit_number):
|
|
colors = [(120, 190, 255), (110, 240, 170), (255, 220, 120), (255, 150, 120), (190, 150, 255), (140, 230, 230)]
|
|
return colors[(orbit_number - 1) % len(colors)]
|
|
|
|
def calculate_position(self):
|
|
x = self.star.x + math.cos(self.angle) * self.orbit_radius
|
|
y = self.star.y + math.sin(self.angle) * self.orbit_radius
|
|
return x, y
|
|
|
|
def update(self):
|
|
self.angle += self.angular_speed * self.direction
|
|
self.x, self.y = self.calculate_position()
|
|
|
|
def draw(self, screen, pygame):
|
|
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
|