Advertisement
Guest User

Untitled

a guest
May 17th, 2019
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. """This example spawns (bouncing) balls randomly on a L-shape constructed of
  2. two segment shapes. Not interactive.
  3. """
  4.  
  5. __version__ = "$Id:$"
  6. __docformat__ = "reStructuredText"
  7.  
  8. # Python imports
  9. import random
  10.  
  11. # Library imports
  12. import pygame
  13. from pygame.key import *
  14. from pygame.locals import *
  15. from pygame.color import *
  16.  
  17. # pymunk imports
  18. import pymunk
  19. import pymunk.pygame_util
  20. import os
  21.  
  22.  
  23. class BouncyBalls(object):
  24. """
  25. This class implements a simple scene in which there is a static platform (made up of a couple of lines)
  26. that don't move. Balls appear occasionally and drop onto the platform. They bounce around.
  27. """
  28. def __init__(self, line1, line2):
  29. # Space
  30. self._space = pymunk.Space()
  31. self._space.gravity = (0.0, -900.0)
  32.  
  33. self._line1 = line1
  34. self._line2 = line2
  35.  
  36. #print(self._line1,self._line2)
  37.  
  38. # Physics
  39. # Time step
  40. self._dt = 1.0 / 60.0
  41. # Number of physics steps per screen frame
  42. self._physics_steps_per_frame = 1
  43.  
  44. # pygame
  45. pygame.init()
  46. self._screen = pygame.display.set_mode((600, 600))
  47. self._clock = pygame.time.Clock()
  48.  
  49. self._draw_options = pymunk.pygame_util.DrawOptions(self._screen)
  50.  
  51. # Static barrier walls (lines) that the balls bounce off of
  52. self._add_static_scenery()
  53.  
  54. # Balls that exist in the world
  55. self._balls = []
  56.  
  57. # Execution control and time until the next ball spawns
  58. self._running = True
  59. self._ticks_to_next_ball = 10
  60.  
  61. def run(self):
  62. """
  63. The main loop of the game.
  64. :return: None
  65. """
  66. # Main loop
  67. while self._running:
  68. # Progress time forward
  69. for x in range(self._physics_steps_per_frame):
  70. self._space.step(self._dt)
  71.  
  72. self._process_events()
  73. self._update_balls()
  74. self._clear_screen()
  75. self._draw_objects()
  76. pygame.display.flip()
  77. # Delay fixed time between frames
  78. self._clock.tick(50)
  79. pygame.display.set_caption("fps: " + str(self._clock.get_fps()))
  80.  
  81. if pygame.time.get_ticks() > 60000:#120000:
  82. self._running = False
  83. return len(self._balls)
  84.  
  85. def _add_static_scenery(self):
  86. """
  87. Create the static bodies.
  88. :return: None
  89. """
  90. static_body = self._space.static_body
  91. static_lines = [pymunk.Segment(static_body, (int(self._line1[0]),int(self._line1[1])), \
  92. (int(self._line1[2]),int(self._line1[3])), 0.0),
  93. pymunk.Segment(static_body, (int(self._line2[0]),int(self._line2[1])), \
  94. (int(self._line2[2]),int(self._line2[3])), 0.0)]
  95. #static_lines = [pymunk.Segment(static_body, (111.0, 280.0), (407.0, 246.0), 0.0),
  96. # pymunk.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0)]
  97. for line in static_lines:
  98. line.elasticity = 0.95
  99. line.friction = 0.9
  100. self._space.add(static_lines)
  101.  
  102. def _process_events(self):
  103. """
  104. Handle game and events like keyboard input. Call once per frame only.
  105. :return: None
  106. """
  107. for event in pygame.event.get():
  108. if event.type == QUIT:
  109. self._running = False
  110. elif event.type == KEYDOWN and event.key == K_ESCAPE:
  111. self._running = False
  112. elif event.type == KEYDOWN and event.key == K_p:
  113. pygame.image.save(self._screen, "bouncing_balls.png")
  114.  
  115. def _update_balls(self):
  116. """
  117. Create/remove balls as necessary. Call once per frame only.
  118. :return: None
  119. """
  120. self._ticks_to_next_ball -= 1
  121. if self._ticks_to_next_ball <= 0:
  122. self._create_ball()
  123. self._ticks_to_next_ball = 100
  124. # Remove balls that fall below 100 vertically
  125. balls_to_remove = [ball for ball in self._balls if ball.body.position.y < 100]
  126. for ball in balls_to_remove:
  127. self._space.remove(ball, ball.body)
  128. self._balls.remove(ball)
  129.  
  130. def _create_ball(self):
  131. """
  132. Create a ball.
  133. :return:
  134. """
  135. mass = 10
  136. radius = 25
  137. inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
  138. body = pymunk.Body(mass, inertia)
  139. x = random.randint(115, 350)
  140. body.position = x, 400
  141. shape = pymunk.Circle(body, radius, (0, 0))
  142. shape.elasticity = 0.95
  143. shape.friction = 0.9
  144. self._space.add(body, shape)
  145. self._balls.append(shape)
  146.  
  147. def _clear_screen(self):
  148. """
  149. Clears the screen.
  150. :return: None
  151. """
  152. self._screen.fill(THECOLORS["white"])
  153.  
  154. def _draw_objects(self):
  155. """
  156. Draw the objects.
  157. :return: None
  158. """
  159. self._space.debug_draw(self._draw_options)
  160.  
  161. if __name__ == '__main__':
  162. game = BouncyBalls()
  163. game.run()
  164. print('BouncyBalls - Done.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement