acoolrocket

Circle Orbital Simulation (Final)

Mar 7th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. import simplegui
  2. import math
  3.  
  4. Width = 900
  5. Height = 900
  6.  
  7. boost_message = "Hold spacebar to boost"
  8. boost_message_color = "Yellow"
  9.  
  10. player_sprite = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/fhN53R.png")
  11. sun_sprite_image = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/Vsd87P.png")
  12. sun_atmosphere_image = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/01/bf7wqA.png")
  13.  
  14. class player_1:
  15. def __init__(self, circle1_center, circle1_radius, velocity_circle1, acceleration1, bounce_distance1, circle1_mass):
  16. self.circle1_center = circle1_center
  17. self.circle1_radius = circle1_radius
  18. self.velocity_circle1 = velocity_circle1
  19. self.acceleration1 = acceleration1
  20. self.bounce_distance1 = bounce_distance1
  21. self.circle1_mass = circle1_mass
  22.  
  23. def keydown1(self, key):
  24. global boost_message, boost_message_color
  25. if key == simplegui.KEY_MAP["left"]:
  26. self.velocity_circle1[0] -= self.acceleration1
  27. elif key == simplegui.KEY_MAP["up"]:
  28. self.velocity_circle1[1] -= self.acceleration1
  29. elif key == simplegui.KEY_MAP["right"]:
  30. self.velocity_circle1[0] += self.acceleration1
  31. elif key == simplegui.KEY_MAP["down"]:
  32. self.velocity_circle1[1] += self.acceleration1
  33. elif key == simplegui.KEY_MAP["space"]:
  34. self.acceleration1 *= 3
  35. boost_message = "BOOST ACTIVATED"
  36. boost_message_color = "Red"
  37.  
  38. def keyup1(self, key):
  39. global boost_message, boost_message_color
  40. if key == simplegui.KEY_MAP["left"]:
  41. self.velocity_circle1[0] = 0
  42. elif key == simplegui.KEY_MAP["up"]:
  43. self.velocity_circle1[1] = 0
  44. elif key == simplegui.KEY_MAP["right"]:
  45. self.velocity_circle1[0] = 0
  46. elif key == simplegui.KEY_MAP["down"]:
  47. self.velocity_circle1[1] = 0
  48. elif key == simplegui.KEY_MAP["space"]:
  49. self.acceleration1 /= 3
  50. boost_message = "Hold spacebar to boost"
  51. boost_message_color = "Yellow"
  52.  
  53. def draw_player_1_sprite(self, canvas):
  54. global Width, Height
  55. canvas.draw_image(player_sprite, [1060/2, 1060/2], [1060, 1060], self.circle1_center, [40, 40])
  56.  
  57. def Movement(self):
  58. self.circle1_center[0] = self.circle1_center[0] + self.velocity_circle1[0]
  59. self.circle1_center[1] = self.circle1_center[1] + self.velocity_circle1[1]
  60.  
  61. def Border_collisions(self):
  62. global Width, Height
  63. if self.circle1_center[0] <= self.circle1_radius:
  64. self.circle1_center[0] += self.bounce_distance1
  65. elif self.circle1_center[0] >= Width-self.circle1_radius:
  66. self.circle1_center[0] -= self.bounce_distance1
  67. elif self.circle1_center[1] <= self.circle1_radius:
  68. self.circle1_center[1] += self.bounce_distance1
  69. elif self.circle1_center[1] >= Height-self.circle1_radius:
  70. self.circle1_center[1] -= self.bounce_distance1
  71.  
  72. class sun_sprite:
  73. def __init__(self, sun_hitbox_center, sun_hitbox_radius):
  74. self.sun_hitbox_center = sun_hitbox_center
  75. self.sun_hitbox_radius = sun_hitbox_radius
  76.  
  77. def draw_sun(self, canvas):
  78. global Width, Height
  79. canvas.draw_image(sun_sprite_image, [3002/2, 3005/2], [3002, 3005], self.sun_hitbox_center, [100, 100])
  80.  
  81. def sun_orbit_collisions(self):
  82. global Call_player_1
  83. x = math.pow((self.sun_hitbox_center[0]-Call_player_1.circle1_center[0]), 2)
  84. y = math.pow((self.sun_hitbox_center[1]-Call_player_1.circle1_center[1]), 2)
  85. distance = math.sqrt(x + y)
  86. #Collisions:
  87. if Call_player_1.circle1_radius+self.sun_hitbox_radius >= distance:
  88. if Call_player_1.circle1_center[0] > self.sun_hitbox_center[0]:
  89. Call_player_1.circle1_center[0] += Call_player_1.bounce_distance1
  90. elif Call_player_1.circle1_center[1] > self.sun_hitbox_center[1]:
  91. Call_player_1.circle1_center[1] += Call_player_1.bounce_distance1
  92. elif Call_player_1.circle1_center[0] < self.sun_hitbox_center[0]:
  93. Call_player_1.circle1_center[0] -= Call_player_1.bounce_distance1
  94. elif Call_player_1.circle1_center[1] < self.sun_hitbox_center[1]:
  95. Call_player_1.circle1_center[1] -= Call_player_1.bounce_distance1
  96.  
  97. class sun_atmosphere:
  98. def __init__(self, sun_atmosp_center, sun_atmosp_radius, sun_atmosp_mass, sun_atmosp_time):
  99. self.sun_atmosp_center = sun_atmosp_center
  100. self.sun_atmosp_radius = sun_atmosp_radius
  101. self.sun_atmosp_mass = sun_atmosp_mass
  102. self.sun_atmosp_time = sun_atmosp_time
  103.  
  104. def draw_atmosphere(self, canvas):
  105. global Width, Height
  106. canvas.draw_image(sun_atmosphere_image, [2136/2, 2136/2], [2136, 2136], self.sun_atmosp_center, [550, 550])
  107.  
  108. def influence_speed(self):
  109. global Call_player_1
  110. #Distance
  111. dx = self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]
  112. dy = self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]
  113. distance = math.sqrt(dx**2 + dy**2)
  114.  
  115. #Force and Mass
  116. Force_Scalar = Call_player_1.circle1_mass * self.sun_atmosp_mass/(distance**2)
  117. theta = math.atan2 (dy, dx)
  118. Fx = math.cos(theta) * Force_Scalar
  119. Fy = math.sin(theta) * Force_Scalar
  120.  
  121. #If Statements
  122. #X-Axis Right Side
  123. if Call_player_1.circle1_center[0] > self.sun_atmosp_center[0]:
  124. theta = -theta
  125. #X-Axis Left Side
  126. elif Call_player_1.circle1_center[0] < self.sun_atmosp_center[0]:
  127. theta = math.fabs(theta)
  128. #Y-Axis Top
  129. elif Call_player_1.circle1_center[1] > self.sun_atmosp_center[1]:
  130. theta = math.fabs(theta)
  131. #Y-Axis Bottom
  132. elif Call_player_1.circle1_center[1] < self.sun_atmosp_center[1]:
  133. theta = -theta
  134.  
  135. #Acceleration
  136. ax = Fx/Call_player_1.circle1_mass
  137. ay = Fy/Call_player_1.circle1_mass
  138.  
  139. #Velocity
  140. Call_player_1.velocity_circle1[0] = Call_player_1.velocity_circle1[0] + ax*20
  141. Call_player_1.velocity_circle1[1] = Call_player_1.velocity_circle1[1] + ay*20
  142.  
  143. #Final Speed Add
  144. Call_player_1.circle1_center[0] += Call_player_1.velocity_circle1[0]
  145. Call_player_1.circle1_center[1] += Call_player_1.velocity_circle1[1]
  146.  
  147. def sun_atmosp_timer(self):
  148. global Call_player_1, timer
  149. dx = self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]
  150. dy = self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]
  151. distance = math.sqrt(dx**2 + dy**2)
  152.  
  153. if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
  154. timer.start()
  155. self.sun_atmosp_time += 1
  156. else:
  157. timer.stop()
  158. self.sun_atmosp_time = 0
  159.  
  160. class reset_position:
  161. def reset_button():
  162. global Call_player_1
  163. Call_player_1.circle1_center = [100, 800]
  164. Call_player_1.velocity_circle1 = [0, 0]
  165.  
  166. Call_player_1 = player_1([100, 800], 17, [0,0], 4, 10, 6)
  167. Call_sun_sprite = sun_sprite([Width/2,Height/2], 57)
  168. Call_sun_atmosphere = sun_atmosphere([Width/2,Height/2], 240, 15, 0)
  169. Call_reset_position = reset_position
  170.  
  171. def draw_sprites(canvas):
  172. global Call_player_1, Call_sun_sprite, boost_message_color, boost_message
  173. Call_player_1.Movement()
  174. Call_player_1.Border_collisions()
  175.  
  176. #Draw Layer 1
  177. Call_sun_atmosphere.draw_atmosphere(canvas)
  178. Call_sun_atmosphere.influence_speed()
  179. Call_sun_atmosphere.sun_atmosp_timer()
  180.  
  181. #Draw Layer 2
  182. Call_sun_sprite.draw_sun(canvas)
  183. Call_sun_sprite.sun_orbit_collisions()
  184.  
  185. #Draw Layer 3
  186. Call_player_1.draw_player_1_sprite(canvas)
  187.  
  188. #Draw Layer 4
  189. canvas.draw_text("Time spent in atmosphere:" + " " + str(Call_sun_atmosphere.sun_atmosp_time), [10,25], 18, "White")
  190. canvas.draw_text(boost_message, [10,70], 14, boost_message_color)
  191.  
  192. def keydown(key):
  193. global Call_player_1
  194. Call_player_1.keydown1(key)
  195.  
  196. def keyup(key):
  197. global Call_player_1
  198. Call_player_1.keyup1(key)
  199.  
  200. frame = simplegui.create_frame("Circle Orbital Simulation", Width, Height)
  201.  
  202. frame.set_draw_handler(draw_sprites)
  203.  
  204. frame.set_keydown_handler(keydown)
  205. frame.set_keyup_handler(keyup)
  206.  
  207. frame.add_button("Reset Position of Player", Call_reset_position.reset_button, 200)
  208.  
  209. timer = simplegui.create_timer(100, Call_sun_atmosphere.sun_atmosp_timer)
  210.  
  211. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment