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
- 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
- class motion():
- def rotatebody(bodylist):
- for body in bodylist:
- if body.pname != 'none':
- for parent in bodylist: # get some parent data
- if body.pname == parent.bname:
- parentx = parent.x
- parenty = parent.y
- parentsize = parent.size
- # simple parametric equation
- 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
- dcenter = math.sqrt((body.x-parentx)**2+(body.y-parenty)**2) # pythagorean distance
- if dcenter < parentsize: # don't draw the circle if occluded
- body.hidden = 1
- else:
- body.hidden = 0
- class start(arcade.Window):
- # main app class
- def __init__(self):
- self.offsetx = 0
- self.offsety= 0
- 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 + self.offsetx, body.y + self.offsety, 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)
- self.offsetx = self.offsetx * 0.5
- self.offsety = self.offsety * 0.5
- start.bodychange(self)
- elif key == arcade.key.EQUAL: # double scale
- changescale(2)
- self.offsetx = self.offsetx * 2
- self.offsety = self.offsety * 2
- start.bodychange(self)
- elif key == arcade.key.COMMA: # halve speed
- changespeed(0.5)
- elif key == arcade.key.PERIOD: # double speed
- changespeed(2)
- elif key == arcade.key.UP: # move camera
- self.offsety += -50
- elif key == arcade.key.DOWN:
- self.offsety += 50
- elif key == arcade.key.LEFT:
- self.offsetx += 50
- elif key == arcade.key.RIGHT:
- self.offsetx += -50
- class orbitalparameters():
- # helps with 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__":
- # starting variables
- screenx = 800
- screeny = 800
- centerx = screenx / 2
- centery = screeny / 2
- speedmod = 0.25
- scale = 100
- # 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
- # next time I'll use a sqlite db or something
- 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