Advertisement
acoolrocket

Circle Orbital Simulation (Manipulating Coordinates)

Feb 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 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, timer
  110. #Distance
  111. dx = math.pow((self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]), 2)
  112. dy = math.pow((self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]), 2)
  113. distance = math.sqrt(dx + dy)
  114.  
  115. if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
  116. timer.start()
  117. self.sun_atmosp_time += 1
  118. else:
  119. timer.stop()
  120. self.sun_atmosp_time = 0
  121.  
  122. if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
  123. #Force and Mass
  124. Force_Scalar = Call_player_1.circle1_mass * self.sun_atmosp_mass/(distance**2)
  125. theta = math.atan2 (dy, dx)
  126. Fx = math.cos(theta) * Force_Scalar
  127. Fy = math.sin(theta) * Force_Scalar
  128.  
  129. #Acceleration
  130. ax = Fx/Call_player_1.circle1_mass
  131. ay = Fy/Call_player_1.circle1_mass
  132.  
  133. #Velocity
  134. Call_player_1.velocity_circle1[0] = Call_player_1.velocity_circle1[0] + ax*self.sun_atmosp_time
  135. Call_player_1.velocity_circle1[1] = Call_player_1.velocity_circle1[1] + ay*self.sun_atmosp_time
  136.  
  137. #Final Speed Add
  138. if Call_player_1.circle1_center[0] > self.sun_atmosp_center[0]:
  139. Fx = +Fx
  140. elif Call_player_1.circle1_center[1] > self.sun_atmosp_center[1]:
  141. Fy = +Fy
  142. elif Call_player_1.circle1_center[0] < self.sun_atmosp_center[0]:
  143. Fx = -Fx
  144. elif Call_player_1.circle1_center[1] < self.sun_atmosp_center[1]:
  145. Fy = -Fy
  146.  
  147. class reset_position:
  148. def reset_button():
  149. global Call_player_1
  150. Call_player_1.circle1_center = [100, 800]
  151. Call_player_1.velocity_circle1 = [0, 0]
  152.  
  153. Call_player_1 = player_1([100, 800], 17, [0,0], 4, 10, 6)
  154. Call_sun_sprite = sun_sprite([Width/2,Height/2], 57)
  155. Call_sun_atmosphere = sun_atmosphere([Width/2,Height/2], 240, 15, 0)
  156. Call_reset_position = reset_position
  157.  
  158. def draw_sprites(canvas):
  159. global Call_player_1, Call_sun_sprite, boost_message_color, boost_message
  160. Call_player_1.Movement()
  161. Call_player_1.Border_collisions()
  162.  
  163. #Draw Layer 1
  164. Call_sun_atmosphere.draw_atmosphere(canvas)
  165. Call_sun_atmosphere.influence_speed()
  166.  
  167. #Draw Layer 2
  168. Call_sun_sprite.draw_sun(canvas)
  169. Call_sun_sprite.sun_orbit_collisions()
  170.  
  171. #Draw Layer 3
  172. Call_player_1.draw_player_1_sprite(canvas)
  173.  
  174. #Draw Layer 4
  175. canvas.draw_text("Time spent in atmosphere:" + " " + str(Call_sun_atmosphere.sun_atmosp_time), [10,25], 18, "White")
  176. canvas.draw_text(boost_message, [10,70], 14, boost_message_color)
  177.  
  178. def keydown(key):
  179. global Call_player_1
  180. Call_player_1.keydown1(key)
  181.  
  182. def keyup(key):
  183. global Call_player_1
  184. Call_player_1.keyup1(key)
  185.  
  186. frame = simplegui.create_frame("Circle Orbital Simulation", Width, Height)
  187.  
  188. frame.set_draw_handler(draw_sprites)
  189.  
  190. frame.set_keydown_handler(keydown)
  191. frame.set_keyup_handler(keyup)
  192.  
  193. frame.add_button("Reset Position of Player", Call_reset_position.reset_button, 200)
  194.  
  195. timer = simplegui.create_timer(100, Call_sun_atmosphere.influence_speed)
  196.  
  197. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement