Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jan 25 16:56:52 2019
  4.  
  5. @author: Ranged
  6. """
  7.  
  8. import arcade
  9. import math
  10.  
  11. screenx = 800
  12. screeny = 800
  13. centerx = screenx / 2
  14. centery = screeny / 2
  15. speedmod = 0.25
  16. scale = 100
  17.  
  18. def changescale(mod):
  19. global scale
  20. scale = scale * mod
  21.  
  22. def changespeed(mod):
  23. global speedmod
  24. speedmod = speedmod * mod
  25.  
  26.  
  27. class body(): #data container for celestial bodies
  28. def __init__(self, data):
  29. self.bname, self.pname, self.type, self.size, self.orbitradius, self.x,\
  30. self.y, self.r, self.t, self.theta, self.hidden, self.period, self.color = data
  31.  
  32. def __str__(self):
  33. return f'Name:\t{self.bname}\nSize:\t{self.size}\n' \
  34. f'Orbit Radius:\t{self.orbitradius}\nPosition:\t{self.x}, {self.y}\nColor:\t{self.color}'
  35.  
  36.  
  37. class solarsystem():
  38.  
  39. def __init__(self, solsystem):
  40. self.bodies = dict()
  41. for data in solsystem:
  42. b = body(data)
  43. self.bodies[b.bname] = b
  44.  
  45. class motion():
  46. def rotatebody(bodylist): #simple polar equation
  47. for body in bodylist:
  48. if body.pname != 'none':
  49.  
  50. for parent in bodylist: #parentchecks for position and hidden
  51. if body.pname == parent.bname:
  52. parentx = parent.x
  53. parenty = parent.y
  54.  
  55. if body.r < parent.size:
  56. body.hidden = 1
  57. else:
  58. body.hidden = 0
  59.  
  60.  
  61. body.x = parentx + body.r * math.cos(math.radians(body.theta))
  62. body.y = parenty + body.r * math.sin(math.radians(body.theta))
  63. body.theta += speedmod * body.t
  64.  
  65.  
  66. """for body in bodylist:
  67. if body.type != 0:
  68. body.x = centerx + body.orbitradius * math.cos(math.radians(body.theta))
  69. body.y = centery + body.orbitradius * math.sin(math.radians(body.theta))
  70. body.theta += speedmod * body.period"""
  71.  
  72.  
  73. class start(arcade.Window):
  74. #main app class
  75. def __init__(self):
  76. super().__init__(screenx, screeny, "Solar System")
  77. self.bodylist = []
  78.  
  79. for data in solsystem:
  80. b = body(data)
  81. self.bodylist.append(b)
  82.  
  83. start.bodychange(self)
  84.  
  85. def bodychange(self): #for when the scale or speed changes as the program runs
  86. orbitalparameters.orbitalperiod(self.bodylist)
  87. orbitalparameters.orbitaldistance(self.bodylist)
  88.  
  89.  
  90. def on_draw(self):
  91. arcade.start_render()
  92.  
  93. for body in self.bodylist:
  94. if body.hidden == 0:
  95. arcade.draw_circle_filled(body.x, body.y, body.size, body.color)
  96.  
  97. def update(self, delta_time):
  98. motion.rotatebody(self.bodylist)
  99.  
  100. def on_key_press(self, key, mods):
  101.  
  102. if key == arcade.key.MINUS: #halve scale
  103. changescale(0.5)
  104. start.bodychange(self)
  105. elif key == arcade.key.EQUAL: #double scale
  106. changescale(2)
  107. start.bodychange(self)
  108. elif key == arcade.key.COMMA: #halve speed
  109. changespeed(0.5)
  110. elif key == arcade.key.PERIOD: #double speed
  111. changespeed(2)
  112.  
  113.  
  114.  
  115. class orbitalparameters():
  116. #aids in converting various parameters
  117. def orbitalperiod(bodylist):
  118. for body in bodylist:
  119. if body.bname != 'sun':
  120. body.t = 1 / body.period
  121.  
  122. def orbitaldistance(bodylist):
  123. for body in bodylist:
  124. if body.bname != 'sun':
  125. body.r = scale * body.orbitradius
  126.  
  127.  
  128.  
  129.  
  130. if __name__ == "__main__":
  131. #shaped as: name, parent, type, size, orbital radius (AU), x, y, r, t, hidden, theta, orbitalperiod (y), color
  132. #for type 0=sun, 1=planet, 2=moon, 3=asteroid (unused)
  133. #x, y, r and t start as 0 and get assigned values later on. hidden is 0 or 1, if obscured by body
  134. solsystem = [('sun', 'none', 0, 20, 0, centerx, centery, 0, 0, 0, 0, 0, (255, 255, 0)),
  135. ('earth', 'sun', 1, 2, 1, 0, 0, 0, 0, 0, 0, 1, (0, 0, 255)),
  136. ('luna', 'earth', 1, 1, 0.04, 0, 0, 0, 0, 0, 0, 0.075, (169,169,169)), #actual radius is 0.00254
  137. ('venus', 'sun', 1, 2, 0.675, 0, 0, 0, 0, 0, 0, 0.616, (255,255,0)),
  138. ('mercury', 'sun', 1, 2, 0.387, 0, 0, 0, 0, 0, 0, 0.24, (169,169,169)),
  139. ('mars', 'sun', 1, 2, 1.524, 0, 0, 0, 0, 0, 0, 1.88, (255, 0, 0)),
  140. ('jupiter', 'sun', 1, 4, 5.20, 0, 0, 0, 0, 0, 0, 11.86, (255, 0, 0)),
  141. ('io', 'jupiter', 1, 1, 0.08, 0, 0, 0, 0, 0, 0, 0.00484, (169,169,169)), #different radiuses for moons to keep visibility
  142. ('europa', 'jupiter', 1, 1, 0.12, 0, 0, 0, 0, 0, 0, 0.0097, (169,169,169)),
  143. ('ganymede', 'jupiter', 1, 1, 0.16, 0, 0, 0, 0, 0, 0, 0.0195, (169,169,169)),
  144. ('callisto', 'jupiter', 1, 1, 0.2, 0, 0, 0, 0, 0, 0, 0.0456, (169,169,169))]
  145.  
  146.  
  147. start()
  148. arcade.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement