acoolrocket

Circle Orbital Simulation (Without Classes)

Feb 23rd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import simplegui
  2. import math
  3.  
  4. Width = 500
  5. Height = 500
  6.  
  7. player_sprite = simplegui.load_image("https://i.imglnx.com/fhN53R.png")
  8. sun_sprite_image = simplegui.load_image("https://i.imglnx.com/Vsd87P.png")
  9.  
  10. circle1_center = [50, 450]
  11. circle1_radius = 17
  12. velocity_circle1 = [0,0]
  13. acceleration1 = 2
  14. bounce_distance1 = 10
  15.  
  16. sun_hitbox_center = [Width/2,Height/2]
  17. sun_hitbox_radius = 40
  18.  
  19. def keydown1(key):
  20. global velocity_circle1, acceleration1
  21. if key == simplegui.KEY_MAP["left"]:
  22. velocity_circle1[0] -= acceleration1
  23. elif key == simplegui.KEY_MAP["up"]:
  24. velocity_circle1[1] -= acceleration1
  25. elif key == simplegui.KEY_MAP["right"]:
  26. velocity_circle1[0] += acceleration1
  27. elif key == simplegui.KEY_MAP["down"]:
  28. velocity_circle1[1] += acceleration1
  29.  
  30. def keyup1(key):
  31. global velocity_circle1
  32. if key == simplegui.KEY_MAP["left"]:
  33. velocity_circle1[0] = 0
  34. elif key == simplegui.KEY_MAP["up"]:
  35. velocity_circle1[1] = 0
  36. elif key == simplegui.KEY_MAP["right"]:
  37. velocity_circle1[0] = 0
  38. elif key == simplegui.KEY_MAP["down"]:
  39. velocity_circle1[1] = 0
  40.  
  41. def draw_sprites(canvas):
  42. global Width, Height, circle1_center, velocity_circle1, sun_hitbox_center, circle1_radius, sun_hitbox_radius, bounce_distance1
  43. canvas.draw_image(player_sprite, [1060/2, 1060/2], [1060, 1060], circle1_center, [40, 40])
  44. canvas.draw_image(sun_sprite_image, [3002/2, 3005/2], [3002, 3005], sun_hitbox_center, [70, 70])
  45.  
  46. circle1_center[0] = circle1_center[0] + velocity_circle1[0]
  47. circle1_center[1] = circle1_center[1] + velocity_circle1[1]
  48.  
  49. x = math.pow((sun_hitbox_center[0]-circle1_center[0]), 2)
  50. y = math.pow((sun_hitbox_center[1]-circle1_center[1]), 2)
  51. distance = math.sqrt(x + y)
  52.  
  53. if circle1_radius+sun_hitbox_radius >= distance:
  54. if circle1_center[0] > sun_hitbox_center[0]:
  55. circle1_center[0] += bounce_distance1
  56. elif circle1_center[1] > sun_hitbox_center[1]:
  57. circle1_center[1] += bounce_distance1
  58. elif circle1_center[0] < sun_hitbox_center[0]:
  59. circle1_center[0] -= bounce_distance1
  60. elif circle1_center[1] < sun_hitbox_center[1]:
  61. circle1_center[1] -= bounce_distance1
  62.  
  63. #Circle-border collision
  64. if circle1_center[0] <= circle1_radius:
  65. circle1_center[0] += bounce_distance1
  66. elif circle1_center[0] >= Width-circle1_radius:
  67. circle1_center[0] -= bounce_distance1
  68. elif circle1_center[1] <= circle1_radius:
  69. circle1_center[1] += bounce_distance1
  70. elif circle1_center[1] >= Height-circle1_radius:
  71. circle1_center[1] -= bounce_distance1
  72.  
  73. frame = simplegui.create_frame("Circle Orbital Simulation", Width, Height)
  74.  
  75. frame.set_draw_handler(draw_sprites)
  76.  
  77. frame.set_keydown_handler(keydown1)
  78. frame.set_keyup_handler(keyup1)
  79.  
  80. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment