def index(): """Home screen""" title = 'Solar System Generator' desc = Markup( 'Star and planet data<br>(Orbital Period is measured in earth years)') form = Markup(make_system()) if request.method == "POST": planets = request.form['planets'] log("User entered " + planets + " planets.") form = Markup(make_system(int(request.form['planets']))) return render_template('base.html', title=title, desc=desc, form=form)
def make_system(planets=None): """Create a solar system""" if planets: if planets > 0: log("Generating solar system from user input.") output = '' system = Star(planets=planets) for planet in system.planets: output = output + 'Planet ' + planet.name + '<br>' +\ hum_mass(planet.mass) + '<br>' +\ "Orbital Period: " +\ str(round(planet.orbit_time, 2)) + ' Earth years<br>' +\ "Orbit Distance: " +\ distance_calc(planet.orbit_distance) + " AU<br><br>" log("Returning list of planet names to user.") return output elif planets == 0: log("Making fun of user for having a lack of imagination.") return 'Did you really create a solar system with zero '\ 'planets!? How boring can you get!? Where is your imagination!?'\ '<br><br>Something made you this way, you need to go sit in a '\ 'corner and rethink your life. Figure out what made you such '\ 'a wet blanket and fix it.<br><br>Become the kind of fun '\ 'imaginative person that puts planets in their solar system.' else: log("Displaying main test page.") planets = "<br><input type=\"number\" "\ + style_form + " name=\"planets\" min=\"0\" "\ "max=\"25\" required><br>" return "<form action=\"/\" method=\"POST\" class=\"form\">" \ "How many planets would you like to have in your solar system?: " \ + planets + \ "<input type=\"submit\" class=\"btn btn-default\" " \ "value=\"Submit\" " + style_button + "></form>"
def __init__(self, mass=0, planets=0, name=None): log("A new solar system has been requested") self.radius = radius_gen(mass) log("Radius: " + str(self.radius)) if name: self.name = name log("Star name: " + self.name + "\n") else: self.name = namer() log("Star name randomly generated as: " + self.name + "\n") if mass == 0: self.mass = randint((2 * (10**29)), (3 * (10**32))) log("Star mass randomly generated as: " + str(self.mass) + "\n") else: self.mass = mass log("Star mass: " + str(self.mass) + "\n") if planets == 0: self.planets = planet_pop(self.mass, num=randint(1, 15), star_name=self.name) log(str(len(self.planets)) + " planets randomly generated.") else: self.planets = planet_pop(self.mass, num=planets) log(str(len(self.planets)) + " planets generated.")
def planet_pop(mass, num=None, star_name=None): """ Takes in a number, star mass, and the star's name Returns a list of planet objects Roche Limit describes min distance between two orbital bodies The min distance will be derived from the roche limit roche limit = 1.26 * radius of planet * (( mass of sun / mass of planet) ** (1/3)) The max distance a planet will be from the sun is 10 ** 10 km """ log("Populating the solar system with planets.") planets = [] distance_max = 10**10 for x in range(0, num): log("Creating planet " + str(x + 1)) if star_name: name = str(star_name) + ' ' + toRoman(x + 1) else: name = namer() log("Planet's name is " + name) planet_mass = randrange((8 * (10**22)), (2 * (10**29))) log(name + "'s mass is " + str(planet_mass)) planet_radius = radius_gen(planet_mass) log(name + "'s radius is " + str(planet_radius)) distance_min = int(1.26 * planet_radius * ((mass / planet_mass)**(1 / 3))) log("Minimum distance is " + str(distance_min)) # Test for planetary roche limits while True: collision = False distance = randrange(distance_min, distance_max) if len(planets) > 0: for planet in planets: # Find and assign large and small planet stats if planet.mass > planet_mass: lplanet_mass = planet.mass splanet_mass = planet_mass splanet_radius = planet_radius else: lplanet_mass = planet_mass splanet_mass = planet.mass splanet_radius = planet.radius # Use large and small planet stats to calculate roche limit roche_limit = 1.26 * \ splanet_radius * \ ((lplanet_mass / splanet_mass) ** (1 / 3)) if abs(distance - planet.orbit_distance) < roche_limit: log("Planetary collision detected, calculating new " "distance.") collision = True # If no collision has been found break leaving distance intact if not collision: log("Planetary orbits do not overlap, keeping orbit distance.") break planets.append(Planet(distance, mass, name, planet_mass, planet_radius)) log("Planet " + name + " Created") return planets
log("A new solar system has been requested") self.radius = radius_gen(mass) log("Radius: " + str(self.radius)) if name: self.name = name log("Star name: " + self.name + "\n") else: self.name = namer() log("Star name randomly generated as: " + self.name + "\n") if mass == 0: self.mass = randint((2 * (10**29)), (3 * (10**32))) log("Star mass randomly generated as: " + str(self.mass) + "\n") else: self.mass = mass log("Star mass: " + str(self.mass) + "\n") if planets == 0: self.planets = planet_pop(self.mass, num=randint(1, 15), star_name=self.name) log(str(len(self.planets)) + " planets randomly generated.") else: self.planets = planet_pop(self.mass, num=planets) log(str(len(self.planets)) + " planets generated.") log("star.py loaded")
self.name = name self.radius = radius self.orbit_distance = orbit_distance self.star_mass = star_mass self.mass = planet_mass self.orbit_time = orbital_period(star_mass, orbit_distance) if self.mass > 6 * 10**25: self.type = 'Gas Giant' else: self.type = 'Terrestrial' def location(self, time, orbit_time): """ Takes in time (In earth years) Returns planet location (x, y co-ordinates) based in time given """ if time > self.orbit_time: time = time - self.orbit_time angle = radians(time / self.orbit_time * 360) x = int(round(self.orbit_distance * cos(angle))) y = int(round(self.orbit_distance * sin(angle))) return x, y log("planet.py loaded")
""" (ToDo) Add terrain and atmosphere features to planet object (ToDo) Tie planet mass and planet size together loosely (Is there an algorithm for estimating density based on gravity?) (Radius = 394.15 * mass ** 0.35651) """ from flask import Flask, render_template, Markup, request from html_builder import make_system from solar_calc import log log("Starting Tharumec website.") app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): """Home screen""" title = 'Solar System Generator' desc = Markup( 'Star and planet data<br>(Orbital Period is measured in earth years)') form = Markup(make_system()) if request.method == "POST": planets = request.form['planets'] log("User entered " + planets + " planets.") form = Markup(make_system(int(request.form['planets']))) return render_template('base.html', title=title, desc=desc, form=form)
str(round(planet.orbit_time, 2)) + ' Earth years<br>' +\ "Orbit Distance: " +\ distance_calc(planet.orbit_distance) + " AU<br><br>" log("Returning list of planet names to user.") return output elif planets == 0: log("Making fun of user for having a lack of imagination.") return 'Did you really create a solar system with zero '\ 'planets!? How boring can you get!? Where is your imagination!?'\ '<br><br>Something made you this way, you need to go sit in a '\ 'corner and rethink your life. Figure out what made you such '\ 'a wet blanket and fix it.<br><br>Become the kind of fun '\ 'imaginative person that puts planets in their solar system.' else: log("Displaying main test page.") planets = "<br><input type=\"number\" "\ + style_form + " name=\"planets\" min=\"0\" "\ "max=\"25\" required><br>" return "<form action=\"/\" method=\"POST\" class=\"form\">" \ "How many planets would you like to have in your solar system?: " \ + planets + \ "<input type=\"submit\" class=\"btn btn-default\" " \ "value=\"Submit\" " + style_button + "></form>" log("html_builder.py loaded")