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

Binary file not shown.

View File

@@ -1,57 +1,75 @@
@startuml
skinparam classAttributeIconSize 0
class SpaceObject {
x
y
radius
color
update()
draw()
x
y
radius
color
mass
update()
draw()
}
class Star {
name
planets
add_planet()
set_position()
update()
draw()
name
planets
add_planet()
set_position()
update()
draw()
}
class Planet {
star
orbit_number
orbit_radius
angle
speed
direction
calculate_position()
update()
draw_orbit()
draw()
star
orbit_number
orbit_radius
angle
speed
angular_speed
linear_speed
direction
calculate_position()
update()
draw_orbit()
draw()
}
class SolarSystem {
stars
show_orbits
paused
create_system()
resize()
update()
draw()
toggle_pause()
toggle_orbits()
width
height
stars
show_orbits
paused
create_system()
get_direction()
resize()
update()
draw()
toggle_pause()
toggle_orbits()
get_all_objects()
get_total_mass()
get_planet_count()
}
class SolarApp {
system
handle_events()
draw_panel()
run()
screen
clock
system
save_callback
draw_panel()
handle_events()
run()
}
class save_system_to_file
SpaceObject <|-- Star
SpaceObject <|-- Planet
Star "1" o-- "many" Planet
SolarSystem "1" o-- "2" Star
SolarSystem *-- Star
Star *-- Planet
SolarApp --> SolarSystem
SolarApp --> save_system_to_file
@enduml

45
UML_objects_end.puml Normal file
View File

@@ -0,0 +1,45 @@
@startuml
object system {
paused = True/False
show_orbits = True/False
}
object star1 {
name = Star 1
x = current_x
y = current_y
mass = 1.989e30
}
object star2 {
name = Star 2
x = current_x
y = current_y
mass = 1.750e30
}
object planet1_end {
orbit_number = 1
angle = changed_angle
x = current_x
y = current_y
direction = 1
angular_speed = 0.0097
linear_speed = angular_speed * orbit_radius
}
object planet2_end {
orbit_number = 2
angle = changed_angle
x = current_x
y = current_y
direction = -1
angular_speed = 0.0104
linear_speed = angular_speed * orbit_radius
}
system -- star1
system -- star2
star1 -- planet1_end
star2 -- planet2_end
@enduml

39
UML_objects_start.puml Normal file
View File

@@ -0,0 +1,39 @@
@startuml
object system {
paused = False
show_orbits = True
}
object star1 {
name = Star 1
planets = 11
mass = 1.989e30
}
object star2 {
name = Star 2
planets = 12
mass = 1.750e30
}
object planet1_start {
orbit_number = 1
angle = start_angle
direction = 1
angular_speed = 0.0097
mass = 3e24..8e24
}
object planet2_start {
orbit_number = 2
angle = start_angle
direction = -1
angular_speed = 0.0104
mass = 3e24..8e24
}
system -- star1
system -- star2
star1 -- planet1_start
star2 -- planet2_start
@enduml

10
build_linux.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
python3 -m venv venv
source venv/bin/activate
pip install pygame pyinstaller
rm -rf build dist *.spec
pyinstaller --onefile --windowed --name SolarSystem main.py
cp dist/SolarSystem .
chmod +x SolarSystem
echo "Done: ./SolarSystem"

View File

@@ -5,14 +5,29 @@ def save_system_to_file(system, filename="solar_state.txt"):
with open(filename, "w", encoding="utf-8") as file:
file.write(f"Saved: {datetime.now()}\n")
file.write(f"Paused: {system.paused}\n")
file.write(f"Show orbits: {system.show_orbits}\n\n")
file.write(f"Show orbits: {system.show_orbits}\n")
file.write(f"Total stars: {len(system.stars)}\n")
file.write(f"Total planets: {system.get_planet_count()}\n")
file.write(f"Total objects: {len(system.get_all_objects())}\n")
file.write(f"Total mass: {system.get_total_mass():.3e}\n\n")
for star in system.stars:
file.write(f"{star.name}: x={round(star.x, 2)}, y={round(star.y, 2)}, radius={star.radius}\n")
file.write(
f"{star.name}: "
f"x={round(star.x, 2)}, y={round(star.y, 2)}, "
f"radius={star.radius}, mass={star.mass:.3e}\n"
)
for planet in star.planets:
file.write(
f" Planet orbit={planet.orbit_number}, "
f"x={round(planet.x, 2)}, y={round(planet.y, 2)}, "
f"radius={planet.radius}, speed={round(planet.speed, 4)}\n"
f"radius={planet.radius}, "
f"mass={planet.mass:.3e}, "
f"angular_speed={round(planet.angular_speed, 4)}, "
f"linear_speed={round(planet.linear_speed, 4)}, "
f"direction={planet.direction}, "
f"angle={round(planet.angle, 4)}, "
f"orbit_radius={planet.orbit_radius}\n"
)
file.write("\n")

View File

@@ -18,6 +18,7 @@ TICKET_CONFIG = {
"first_orbit": 48,
"orbit_step": 24,
"color": (255, 190, 40),
"mass": 1.989e30,
},
{
"name": "Star 2",
@@ -26,6 +27,7 @@ TICKET_CONFIG = {
"first_orbit": 58,
"orbit_step": 38,
"color": (255, 150, 55),
"mass": 1.750e30,
},
],
"rules": {
@@ -50,7 +52,14 @@ class SolarSystem:
self.stars = []
for star_data in TICKET_CONFIG["stars"]:
star = Star(0, 0, radius=34, color=star_data["color"], name=star_data["name"])
star = Star(
0,
0,
radius=34,
color=star_data["color"],
name=star_data["name"],
mass=star_data["mass"],
)
planet_count = star_data["planet_count"]
planets_per_orbit = star_data["planets_per_orbit"]
orbit_count = math.ceil(planet_count / planets_per_orbit)
@@ -67,7 +76,16 @@ class SolarSystem:
break
angle = base_angle + place * (2 * math.pi / planets_per_orbit)
planet = Planet(star, orbit_number, orbit_radius, angle, speed, direction)
planet_mass = random.uniform(3.0e24, 8.0e24)
planet = Planet(
star,
orbit_number,
orbit_radius,
angle,
speed,
direction,
mass=planet_mass,
)
star.add_planet(planet)
created += 1
@@ -115,3 +133,9 @@ class SolarSystem:
objects.append(star)
objects.extend(star.planets)
return objects
def get_total_mass(self):
return sum(obj.mass for obj in self.get_all_objects())
def get_planet_count(self):
return sum(len(star.planets) for star in self.stars)

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):