Planet sim and Etch added, working properly AFAIK
This commit is contained in:
commit
88f86dc04f
19 changed files with 638 additions and 0 deletions
3
Turtle/PlanetSim/README.md
Normal file
3
Turtle/PlanetSim/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
### Planet Simulator
|
||||
|
||||
A very basic planet simulator. Really just a demo of using classes with turtle.
|
56
Turtle/PlanetSim/planet.py
Normal file
56
Turtle/PlanetSim/planet.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
import math
|
||||
import turtle
|
||||
|
||||
|
||||
class Planet:
|
||||
def __init__(self, name, radius, mass, distanceFromSun, velocity, color):
|
||||
"""
|
||||
initializes the Planet
|
||||
:param name: name of planet
|
||||
:param radius: radius of planet
|
||||
:param mass: mass of planet
|
||||
:param distanceFromSun: distance of planet from the sun
|
||||
:param velocity: tuple containing the velocity of the planet in the format (x_velocity, y_velocity)
|
||||
:param color: color of planet
|
||||
"""
|
||||
self.__name = name
|
||||
self.__radius = radius
|
||||
self.__mass = mass
|
||||
self.__distance = distanceFromSun
|
||||
self.__velocity = velocity
|
||||
self.__color = color
|
||||
self.__coords = [distanceFromSun, 0]
|
||||
|
||||
self.__pT = turtle.Turtle()
|
||||
self.__pT.pensize(1)
|
||||
self.__pT.color(self.__color)
|
||||
if name == 'earth' or name == 'Earth':
|
||||
self.__pT.shape('turtle')
|
||||
else:
|
||||
self.__pT.shape('circle')
|
||||
self.__pT.up()
|
||||
self.__pT.goto(self.__coords[0], self.__coords[1])
|
||||
self.__pT.down()
|
||||
|
||||
def moveTo(self, new_coords):
|
||||
self.__coords = new_coords
|
||||
self.__pT.goto(self.__coords[0], self.__coords[1])
|
||||
|
||||
@property
|
||||
def velocity(self):
|
||||
return self.__velocity
|
||||
|
||||
@velocity.setter
|
||||
def velocity(self, vel):
|
||||
self.__velocity = vel
|
||||
|
||||
@property
|
||||
def coords(self):
|
||||
return self.__coords
|
||||
|
||||
@coords.setter
|
||||
def coords(self, new_coords):
|
||||
self.__coords = new_coords
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.__name} at distance {self.__distance}'
|
29
Turtle/PlanetSim/planet_sim.py
Normal file
29
Turtle/PlanetSim/planet_sim.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from sun import Sun
|
||||
from solar_system import SolarSystem
|
||||
from planet import Planet
|
||||
|
||||
|
||||
# Create a SolarSystem
|
||||
solar_sys = SolarSystem(2, 2)
|
||||
|
||||
# Create sun
|
||||
sun = Sun('Sun', 5000, 40, 1000)
|
||||
solar_sys.add_sun(sun)
|
||||
|
||||
# Create planets
|
||||
p = Planet('Mercury', 1, 500, 0.15, (0, 2), 'black')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Venus', 1, 500, 0.25, (0, 2), 'purple')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Earth', 1, 700, 0.5, (0, 2), '#2152a6')
|
||||
solar_sys.add_planet(p)
|
||||
p = Planet('Mars', 1, 700, 0.6, (0, 2), '#b32b10')
|
||||
solar_sys.add_planet(p)
|
||||
|
||||
solar_sys.show_planets()
|
||||
|
||||
num_time_periods = 1000
|
||||
for _ in range(num_time_periods):
|
||||
solar_sys.move_planets()
|
||||
|
||||
solar_sys.freeze()
|
50
Turtle/PlanetSim/solar_system.py
Normal file
50
Turtle/PlanetSim/solar_system.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import turtle
|
||||
import math
|
||||
|
||||
|
||||
class SolarSystem:
|
||||
def __init__(self, width, height):
|
||||
"""
|
||||
initializes SolarSystem
|
||||
:param width: width of world
|
||||
:param height: height of world
|
||||
"""
|
||||
self.__theSun = None
|
||||
self.__planets = []
|
||||
self.__screen = turtle.Screen()
|
||||
self.__screen.setup(width=950, height=950)
|
||||
self.__screen.setworldcoordinates(-width / 2, -height / 2, width / 2, height / 2)
|
||||
|
||||
def move_planets(self):
|
||||
G = 0.025
|
||||
dt = 0.001
|
||||
|
||||
for p in self.__planets:
|
||||
p.moveTo((p.coords[0] + dt * p.velocity[0], p.coords[1] + dt * p.velocity[1]))
|
||||
|
||||
radius_x = self.__theSun.x - p.coords[0]
|
||||
radius_y = self.__theSun.y - p.coords[1]
|
||||
radius = math.sqrt((radius_x ** 2) + (radius_y ** 2)) # Uses pythagorean theorem to find radius
|
||||
|
||||
acc_x = G * self.__theSun.mass * radius_x / radius ** 3 # I don't know,
|
||||
acc_y = G * self.__theSun.mass * radius_y / radius ** 3 # I haven't taken physics
|
||||
|
||||
p.velocity = (p.velocity[0] + dt * acc_x, p.velocity[1] + dt * acc_y)
|
||||
|
||||
def add_sun(self, sun):
|
||||
self.__theSun = sun
|
||||
|
||||
def add_planet(self, planet):
|
||||
self.__planets.append(planet)
|
||||
|
||||
def remove_planet(self, planet_name):
|
||||
for p in self.__planets:
|
||||
if p.name == planet_name:
|
||||
self.__planets.remove(p)
|
||||
|
||||
def show_planets(self):
|
||||
for p in self.__planets:
|
||||
print(p)
|
||||
|
||||
def freeze(self):
|
||||
self.__screen.exitonclick()
|
67
Turtle/PlanetSim/sun.py
Normal file
67
Turtle/PlanetSim/sun.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
import math
|
||||
import turtle
|
||||
|
||||
|
||||
class Sun:
|
||||
def __init__(self, name, radius, mass, temp):
|
||||
"""
|
||||
initializes Sun
|
||||
:param name: name of Sun
|
||||
:param radius: radius of Sun
|
||||
:param mass: mass of Sun
|
||||
:param temp: temperature of Sun
|
||||
"""
|
||||
self.__name = name
|
||||
self.__radius = radius
|
||||
self.__mass = mass
|
||||
self.__temp = temp
|
||||
# List of coordinates in order of [x, y]
|
||||
self.__coords = (0, 0)
|
||||
|
||||
self.__sunT = turtle.Turtle()
|
||||
self.__sunT.shape('circle')
|
||||
self.__sunT.color('orange')
|
||||
self.__sunT.shapesize(2)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.__name
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.__coords[0]
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.__coords[1]
|
||||
|
||||
@property
|
||||
def mass(self):
|
||||
return self.__mass
|
||||
|
||||
@property
|
||||
def radius(self):
|
||||
return self.__radius
|
||||
|
||||
@property
|
||||
def temperature(self):
|
||||
return self.__temp
|
||||
|
||||
@property
|
||||
def volume(self):
|
||||
return (4 / 3) * math.pi * (self.__radius ** 3)
|
||||
|
||||
@property
|
||||
def surface_area(self):
|
||||
return 4 * math.pi * (self.__radius ** 2)
|
||||
|
||||
@property
|
||||
def density(self):
|
||||
return self.__mass / self.volume
|
||||
|
||||
@name.setter
|
||||
def name(self, name):
|
||||
self.__name = name
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.__name} at ({self.__coords[0]}, {self.__coords[1]})'
|
Loading…
Add table
Add a link
Reference in a new issue