Final exam version with mass and speed

This commit is contained in:
2026-06-25 14:05:11 +03:00
parent b03800ab63
commit b3c86d92eb
8 changed files with 205 additions and 48 deletions

View File

@@ -3,11 +3,12 @@ import random
class SpaceObject:
def __init__(self, x, y, radius, color):
def __init__(self, x, y, radius, color, mass=1.0):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.mass = mass
def update(self):
pass
@@ -17,8 +18,8 @@ class SpaceObject:
class Star(SpaceObject):
def __init__(self, x, y, radius=34, color=(255, 185, 40), name="Star"):
super().__init__(x, y, radius, color)
def __init__(self, x, y, radius=34, color=(255, 185, 40), name="Star", mass=1.989e30):
super().__init__(x, y, radius, color, mass)
self.name = name
self.planets = []
@@ -46,13 +47,15 @@ class Star(SpaceObject):
class Planet(SpaceObject):
def __init__(self, star, orbit_number, orbit_radius, angle, speed, direction, radius=None, color=None):
def __init__(self, star, orbit_number, orbit_radius, angle, speed, direction, radius=None, color=None, mass=None):
self.star = star
self.orbit_number = orbit_number
self.orbit_radius = orbit_radius
self.angle = angle
self.speed = speed
self.angular_speed = speed
self.direction = direction
self.linear_speed = abs(self.angular_speed * self.orbit_radius)
if radius is None:
radius = random.randint(8, 13)
@@ -64,8 +67,11 @@ class Planet(SpaceObject):
random.randint(90, 255),
)
if mass is None:
mass = random.uniform(3.0e24, 8.0e24)
x, y = self.calculate_position()
super().__init__(x, y, radius, color)
super().__init__(x, y, radius, color, mass)
def calculate_position(self):
x = self.star.x + math.cos(self.angle) * self.orbit_radius
@@ -73,7 +79,7 @@ class Planet(SpaceObject):
return x, y
def update(self):
self.angle += self.speed * self.direction
self.angle += self.angular_speed * self.direction
self.x, self.y = self.calculate_position()
def draw_orbit(self, screen, pygame):