Advertisement
Guest User

Untitled

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