Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jan 25 16:56:52 2019
- @author: Ranged
- """
- import arcade
- import math
- screenx = 800
- screeny = 800
- centerx = screenx / 2
- centery = screeny / 2
- speedmod = 0.25
- scale = 100
- def changescale(mod):
- global scale
- scale = scale * mod
- def changespeed(mod):
- global speedmod
- speedmod = speedmod * mod
- class body(): #data container for celestial bodies
- def __init__(self, data):
- self.bname, self.pname, self.type, self.size, self.orbitradius, self.x,\
- self.y, self.r, self.t, self.theta, self.hidden, self.period, self.color = data
- def __str__(self):
- return f'Name:\t{self.bname}\nSize:\t{self.size}\n' \
- f'Orbit Radius:\t{self.orbitradius}\nPosition:\t{self.x}, {self.y}\nColor:\t{self.color}'
- class solarsystem():
- def __init__(self, solsystem):
- self.bodies = dict()
- for data in solsystem:
- b = body(data)
- self.bodies[b.bname] = b
- class motion():
- def rotatebody(bodylist): #simple polar equation
- for body in bodylist:
- if body.pname != 'none':
- for parent in bodylist: #parentchecks for position and hidden
- if body.pname == parent.bname:
- parentx = parent.x
- parenty = parent.y
- if body.r < parent.size:
- body.hidden = 1
- else:
- body.hidden = 0
- body.x = parentx + body.r * math.cos(math.radians(body.theta))
- body.y = parenty + body.r * math.sin(math.radians(body.theta))
- body.theta += speedmod * body.t
- """for body in bodylist:
- if body.type != 0:
- body.x = centerx + body.orbitradius * math.cos(math.radians(body.theta))
- body.y = centery + body.orbitradius * math.sin(math.radians(body.theta))
- body.theta += speedmod * body.period"""
- class start(arcade.Window):
- #main app class
- def __init__(self):
- super().__init__(screenx, screeny, "Solar System")
- self.bodylist = []
- for data in solsystem:
- b = body(data)
- self.bodylist.append(b)
- start.bodychange(self)
- def bodychange(self): #for when the scale or speed changes as the program runs
- orbitalparameters.orbitalperiod(self.bodylist)
- orbitalparameters.orbitaldistance(self.bodylist)
- def on_draw(self):
- arcade.start_render()
- for body in self.bodylist:
- if body.hidden == 0:
- arcade.draw_circle_filled(body.x, body.y, body.size, body.color)
- def update(self, delta_time):
- motion.rotatebody(self.bodylist)
- def on_key_press(self, key, mods):
- if key == arcade.key.MINUS: #halve scale
- changescale(0.5)
- start.bodychange(self)
- elif key == arcade.key.EQUAL: #double scale
- changescale(2)
- start.bodychange(self)
- elif key == arcade.key.COMMA: #halve speed
- changespeed(0.5)
- elif key == arcade.key.PERIOD: #double speed
- changespeed(2)
- class orbitalparameters():
- #aids in converting various parameters
- def orbitalperiod(bodylist):
- for body in bodylist:
- if body.bname != 'sun':
- body.t = 1 / body.period
- def orbitaldistance(bodylist):
- for body in bodylist:
- if body.bname != 'sun':
- body.r = scale * body.orbitradius
- if __name__ == "__main__":
- #shaped as: name, parent, type, size, orbital radius (AU), x, y, r, t, hidden, theta, orbitalperiod (y), color
- #for type 0=sun, 1=planet, 2=moon, 3=asteroid (unused)
- #x, y, r and t start as 0 and get assigned values later on. hidden is 0 or 1, if obscured by body
- solsystem = [('sun', 'none', 0, 20, 0, centerx, centery, 0, 0, 0, 0, 0, (255, 255, 0)),
- ('earth', 'sun', 1, 2, 1, 0, 0, 0, 0, 0, 0, 1, (0, 0, 255)),
- ('luna', 'earth', 1, 1, 0.04, 0, 0, 0, 0, 0, 0, 0.075, (169,169,169)), #actual radius is 0.00254
- ('venus', 'sun', 1, 2, 0.675, 0, 0, 0, 0, 0, 0, 0.616, (255,255,0)),
- ('mercury', 'sun', 1, 2, 0.387, 0, 0, 0, 0, 0, 0, 0.24, (169,169,169)),
- ('mars', 'sun', 1, 2, 1.524, 0, 0, 0, 0, 0, 0, 1.88, (255, 0, 0)),
- ('jupiter', 'sun', 1, 4, 5.20, 0, 0, 0, 0, 0, 0, 11.86, (255, 0, 0)),
- ('io', 'jupiter', 1, 1, 0.08, 0, 0, 0, 0, 0, 0, 0.00484, (169,169,169)), #different radiuses for moons to keep visibility
- ('europa', 'jupiter', 1, 1, 0.12, 0, 0, 0, 0, 0, 0, 0.0097, (169,169,169)),
- ('ganymede', 'jupiter', 1, 1, 0.16, 0, 0, 0, 0, 0, 0, 0.0195, (169,169,169)),
- ('callisto', 'jupiter', 1, 1, 0.2, 0, 0, 0, 0, 0, 0, 0.0456, (169,169,169))]
- start()
- arcade.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement