Advertisement
acoolrocket

Circle Orbital Simulation (Final/With Explosion)

Mar 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 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. explosion_image = simplegui.load_image("")
  14. game_over_message = ("")
  15.  
  16. class player_1:
  17. def __init__(self, circle1_center, circle1_radius, velocity_circle1, acceleration1, bounce_distance1, circle1_mass):
  18. self.circle1_center = circle1_center
  19. self.circle1_radius = circle1_radius
  20. self.velocity_circle1 = velocity_circle1
  21. self.acceleration1 = acceleration1
  22. self.bounce_distance1 = bounce_distance1
  23. self.circle1_mass = circle1_mass
  24.  
  25. def keydown1(self, key):
  26. global boost_message, boost_message_color
  27. if key == simplegui.KEY_MAP["left"]:
  28. self.velocity_circle1[0] -= self.acceleration1
  29. elif key == simplegui.KEY_MAP["up"]:
  30. self.velocity_circle1[1] -= self.acceleration1
  31. elif key == simplegui.KEY_MAP["right"]:
  32. self.velocity_circle1[0] += self.acceleration1
  33. elif key == simplegui.KEY_MAP["down"]:
  34. self.velocity_circle1[1] += self.acceleration1
  35. elif key == simplegui.KEY_MAP["space"]:
  36. self.acceleration1 *= 3
  37. boost_message = "BOOST ACTIVATED"
  38. boost_message_color = "Red"
  39.  
  40. def keyup1(self, key):
  41. global boost_message, boost_message_color
  42. if key == simplegui.KEY_MAP["left"]:
  43. self.velocity_circle1[0] = 0
  44. elif key == simplegui.KEY_MAP["up"]:
  45. self.velocity_circle1[1] = 0
  46. elif key == simplegui.KEY_MAP["right"]:
  47. self.velocity_circle1[0] = 0
  48. elif key == simplegui.KEY_MAP["down"]:
  49. self.velocity_circle1[1] = 0
  50. elif key == simplegui.KEY_MAP["space"]:
  51. self.acceleration1 /= 3
  52. boost_message = "Hold spacebar to boost"
  53. boost_message_color = "Yellow"
  54.  
  55. def draw_player_1_sprite(self, canvas):
  56. global Width, Height
  57. canvas.draw_image(player_sprite, [1060/2, 1060/2], [1060, 1060], self.circle1_center, [40, 40])
  58.  
  59. def Movement(self):
  60. self.circle1_center[0] = self.circle1_center[0] + self.velocity_circle1[0]
  61. self.circle1_center[1] = self.circle1_center[1] + self.velocity_circle1[1]
  62.  
  63. def Border_collisions(self):
  64. global Width, Height
  65. if self.circle1_center[0] <= self.circle1_radius:
  66. self.circle1_center[0] += self.bounce_distance1
  67. elif self.circle1_center[0] >= Width-self.circle1_radius:
  68. self.circle1_center[0] -= self.bounce_distance1
  69. elif self.circle1_center[1] <= self.circle1_radius:
  70. self.circle1_center[1] += self.bounce_distance1
  71. elif self.circle1_center[1] >= Height-self.circle1_radius:
  72. self.circle1_center[1] -= self.bounce_distance1
  73.  
  74. class sun_sprite:
  75. def __init__(self, sun_hitbox_center, sun_hitbox_radius):
  76. self.sun_hitbox_center = sun_hitbox_center
  77. self.sun_hitbox_radius = sun_hitbox_radius
  78.  
  79. def draw_sun(self, canvas):
  80. global Width, Height
  81. canvas.draw_image(sun_sprite_image, [3002/2, 3005/2], [3002, 3005], self.sun_hitbox_center, [100, 100])
  82.  
  83. class sun_atmosphere:
  84. def __init__(self, sun_atmosp_center, sun_atmosp_radius, sun_atmosp_mass, sun_atmosp_time):
  85. self.sun_atmosp_center = sun_atmosp_center
  86. self.sun_atmosp_radius = sun_atmosp_radius
  87. self.sun_atmosp_mass = sun_atmosp_mass
  88. self.sun_atmosp_time = sun_atmosp_time
  89.  
  90. def draw_atmosphere(self, canvas):
  91. global Width, Height
  92. canvas.draw_image(sun_atmosphere_image, [2136/2, 2136/2], [2136, 2136], self.sun_atmosp_center, [550, 550])
  93.  
  94. def influence_speed(self):
  95. global Call_player_1
  96. #Distance
  97. dx = self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]
  98. dy = self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]
  99. distance = math.sqrt(dx**2 + dy**2)
  100.  
  101. #Force and Mass
  102. Force_Scalar = Call_player_1.circle1_mass * self.sun_atmosp_mass/(distance**2)
  103. theta = math.atan2 (dy, dx)
  104. Fx = math.cos(theta) * Force_Scalar
  105. Fy = math.sin(theta) * Force_Scalar
  106.  
  107. #If Statements
  108. #X-Axis Right Side
  109. if Call_player_1.circle1_center[0] > self.sun_atmosp_center[0]:
  110. theta = -theta
  111. #X-Axis Left Side
  112. elif Call_player_1.circle1_center[0] < self.sun_atmosp_center[0]:
  113. theta = math.fabs(theta)
  114. #Y-Axis Top
  115. elif Call_player_1.circle1_center[1] > self.sun_atmosp_center[1]:
  116. theta = math.fabs(theta)
  117. #Y-Axis Bottom
  118. elif Call_player_1.circle1_center[1] < self.sun_atmosp_center[1]:
  119. theta = -theta
  120.  
  121. #Acceleration
  122. ax = Fx/Call_player_1.circle1_mass
  123. ay = Fy/Call_player_1.circle1_mass
  124.  
  125. #Velocity
  126. Call_player_1.velocity_circle1[0] = Call_player_1.velocity_circle1[0] + ax*20
  127. Call_player_1.velocity_circle1[1] = Call_player_1.velocity_circle1[1] + ay*20
  128.  
  129. #Final Speed Add
  130. Call_player_1.circle1_center[0] += Call_player_1.velocity_circle1[0]
  131. Call_player_1.circle1_center[1] += Call_player_1.velocity_circle1[1]
  132.  
  133. def sun_atmosp_timer(self):
  134. global Call_player_1, timer, Call_sun_sprite
  135. dx = self.sun_atmosp_center[0]-Call_player_1.circle1_center[0]
  136. dy = self.sun_atmosp_center[1]-Call_player_1.circle1_center[1]
  137. distance = math.sqrt(dx**2 + dy**2)
  138.  
  139. if Call_player_1.circle1_radius+self.sun_atmosp_radius >= distance:
  140. timer.start()
  141. self.sun_atmosp_time += 1
  142. if Call_player_1.circle1_radius+Call_sun_sprite.sun_hitbox_radius >= distance:
  143. self.sun_atmosp_time -= 1
  144. else:
  145. timer.stop()
  146. self.sun_atmosp_time = 0
  147.  
  148. class sun_explosion:
  149. def __init__(self, sun_explosion_time, explosion_teller):
  150. self.sun_explosion_time = sun_explosion_time
  151. self.explosion_teller = explosion_teller
  152.  
  153. def draw_explosion(self, canvas):
  154. global Call_sun_sprite
  155. canvas.draw_image(explosion_image, [765/2, 489/2], [765, 489], Call_sun_sprite.sun_hitbox_center, [600, 383])
  156.  
  157. def sun_explosion_collision(self):
  158. global Call_player_1, explosion_image, Call_sun_sprite
  159. x = math.pow((Call_sun_sprite.sun_hitbox_center[0]-Call_player_1.circle1_center[0]), 2)
  160. y = math.pow((Call_sun_sprite.sun_hitbox_center[1]-Call_player_1.circle1_center[1]), 2)
  161. distance = math.sqrt(x + y)
  162. #Collisions:
  163. if Call_player_1.circle1_radius+Call_sun_sprite.sun_hitbox_radius >= distance:
  164. explosion_image = simplegui.load_image("https://i.imgrpost.com/imgr/2018/03/09/explosion.png")
  165.  
  166. def sun_explosion_timer(self):
  167. global Call_player_1, timer_explosion, explosion_image, Call_sun_sprite
  168. dx = Call_sun_sprite.sun_hitbox_center[0]-Call_player_1.circle1_center[0]
  169. dy = Call_sun_sprite.sun_hitbox_center[1]-Call_player_1.circle1_center[1]
  170. distance = math.sqrt(dx**2 + dy**2)
  171.  
  172. if Call_player_1.circle1_radius+Call_sun_sprite.sun_hitbox_radius >= distance:
  173. timer.start()
  174. self.sun_explosion_time += 1
  175. Call_player_1.velocity_circle1[0] = 0
  176. Call_player_1.velocity_circle1[1] = 0
  177. self.explosion_teller = 1
  178. else:
  179. timer.stop()
  180. self.sun_explosion_time = 0
  181.  
  182. if self.sun_explosion_time > 100:
  183. Call_player_1.circle1_center = [100, 800]
  184. Call_player_1.velocity_circle1 = [0, 0]
  185. explosion_image = simplegui.load_image("")
  186.  
  187. class player_lives:
  188. def __init__(self, lives):
  189. self.lives = lives
  190.  
  191. def lives_deduction(self):
  192. global Call_sun_explosion
  193. if Call_sun_explosion.explosion_teller == 1:
  194. self.lives = 0
  195. Call_sun_explosion.explosion_teller = 0
  196.  
  197. def game_over(self):
  198. global Call_sun_explosion, game_over_message
  199. if self.lives == 0:
  200. game_over_message = "GAME OVER"
  201.  
  202. if Call_sun_explosion.sun_explosion_time > 100:
  203. frame.stop()
  204.  
  205. class reset_position:
  206. def reset_button():
  207. global Call_player_1
  208. Call_player_1.circle1_center = [100, 800]
  209. Call_player_1.velocity_circle1 = [0, 0]
  210.  
  211. Call_player_1 = player_1([100, 800], 17, [0,0], 4, 10, 6)
  212. Call_sun_sprite = sun_sprite([Width/2,Height/2], 57)
  213. Call_sun_atmosphere = sun_atmosphere([Width/2,Height/2], 240, 15, 0)
  214. Call_reset_position = reset_position
  215. Call_sun_explosion = sun_explosion(0, 0)
  216. Call_player_lives = player_lives(1)
  217.  
  218. def draw_sprites(canvas):
  219. global Call_player_1, Call_sun_sprite, boost_message_color, boost_message, game_over_message
  220. Call_player_1.Movement()
  221. Call_player_1.Border_collisions()
  222.  
  223. #Draw Layer 5 - Atmosphere
  224. Call_sun_atmosphere.draw_atmosphere(canvas)
  225. Call_sun_atmosphere.influence_speed()
  226. Call_sun_atmosphere.sun_atmosp_timer()
  227.  
  228. #Draw Layer 4 - Sun
  229. Call_sun_sprite.draw_sun(canvas)
  230.  
  231. #Draw Layer 3 - Player
  232. Call_player_1.draw_player_1_sprite(canvas)
  233.  
  234. #Draw Layer 2 - Explosion
  235. Call_sun_explosion.draw_explosion(canvas)
  236. Call_sun_explosion.sun_explosion_collision()
  237. Call_sun_explosion.sun_explosion_timer()
  238.  
  239. #Draw Layer 1 - Text
  240. canvas.draw_text("Time spent in atmosphere:" + " " + str(Call_sun_atmosphere.sun_atmosp_time), [10,25], 18, "White")
  241. canvas.draw_text(boost_message, [10,70], 14, boost_message_color)
  242. Call_player_lives.lives_deduction()
  243. canvas.draw_text("Lives left:" + " " + str(Call_player_lives.lives), [10,50], 18, "White")
  244. canvas.draw_text(game_over_message, [150 ,(Height/2 + 25)], 100, "Red")
  245. Call_player_lives.game_over()
  246.  
  247. def keydown(key):
  248. global Call_player_1
  249. Call_player_1.keydown1(key)
  250.  
  251. def keyup(key):
  252. global Call_player_1
  253. Call_player_1.keyup1(key)
  254.  
  255. frame = simplegui.create_frame("Circle Orbital Simulation", Width, Height)
  256.  
  257. frame.set_draw_handler(draw_sprites)
  258.  
  259. frame.set_keydown_handler(keydown)
  260. frame.set_keyup_handler(keyup)
  261.  
  262. frame.add_button("Reset Position of Player", Call_reset_position.reset_button, 200)
  263.  
  264. timer = simplegui.create_timer(100, Call_sun_atmosphere.sun_atmosp_timer)
  265. timer_explosion = simplegui.create_timer(100, Call_sun_explosion.sun_explosion_timer)
  266.  
  267. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement