Verlet integrator, to deal with an argument from the pub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.3KB

  1. # Import the pygame library and initialise the game engine
  2. import pygame
  3. import numpy as np
  4. pygame.init()
  5. # Initialize Pygame
  6. size = (800, 800)
  7. CENTER = np.array(size) / 2
  8. WHITE = (255, 255, 255)
  9. screen = pygame.display.set_mode(size)
  10. pygame.display.set_caption("Patrick")
  11. running = True
  12. clock = pygame.time.Clock()
  13. class Ball(object):
  14. def __init__(self, x, y, radius=10, mass=1):
  15. self.pos = np.array([x, y])
  16. self.radius = radius
  17. self.mass = mass
  18. def render(self, screen):
  19. x, y = self.pos + CENTER
  20. pygame.draw.circle(screen, WHITE, [int(x), int(y)], self.radius, 2)
  21. class Spring(object):
  22. def __init__(self, a, b, k=0.1):
  23. self.a = a
  24. self.b = b
  25. self.k = k
  26. def render(self, screen):
  27. x1, y1 = self.a.pos + CENTER
  28. x2, y2 = self.b.pos + CENTER
  29. pygame.draw.line(screen, WHITE, [x1, y1], [x2, y2])
  30. if __name__ == '__main__':
  31. balls = [Ball(0, 0), Ball(100, 1)]
  32. springs = [Spring(balls[0], balls[1])]
  33. while running:
  34. for event in pygame.event.get():
  35. if event.type == pygame.QUIT:
  36. running = False
  37. screen.fill((0, 0, 0))
  38. for ball in balls:
  39. ball.render(screen)
  40. for spring in springs:
  41. spring.render(screen)
  42. pygame.display.flip()
  43. clock.tick(60)
  44. pygame.quit()