|
- # Import the pygame library and initialise the game engine
- import pygame
- import numpy as np
- from verlet import Ball, Spring
- pygame.init()
-
- # Initialize Pygame
- SIZE = (800, 800)
- WHITE = (255, 255, 255)
- screen = pygame.display.set_mode(SIZE)
- pygame.display.set_caption("Patrick")
- running = True
- clock = pygame.time.Clock()
-
- data = []
-
-
- def physics():
- for ball in balls:
- ball.update()
-
- for spring in springs:
- spring.update()
-
-
- def draw():
- for ball in balls:
- ball.render(screen)
-
- for spring in springs:
- spring.render(screen)
-
-
- def boost():
- vector = springs[0].b.pos - springs[0].a.pos
- length = springs[0].compute_length()
- vector /= length
- norm = np.array([vector[1], -vector[0]])
- balls[1].velocity += norm * .5
-
- def modulate():
- x = pygame.mouse.get_pos()[0]
- fraction = x / SIZE[0]
- springs[-1].k = fraction*3 + 0.05
- springs[-2].k = fraction*3 + 0.05
- pygame.draw.rect(screen, (30, 30, 30), (0, 0, x, SIZE[1]))
-
-
- if __name__ == '__main__':
- balls = [Ball(0., 0., fixed=True),
- Ball(0., 100.),
- Ball(20., 120.),
- Ball(-20., 120.),
- Ball(0., 80.),
- Ball(40., 100.),
- Ball(-40., 100.)]
- springs = [
- Spring(balls[0], balls[1], length=100, k=3),
- Spring(balls[1], balls[2], k=5),
- Spring(balls[1], balls[3], k=5),
- Spring(balls[2], balls[3], k=5),
- Spring(balls[1], balls[4], k=5),
- Spring(balls[2], balls[4], k=5),
- Spring(balls[3], balls[4], k=5),
- Spring(balls[3], balls[5], k=.1, color=(255, 0, 0)),
- Spring(balls[2], balls[6], k=.1, color=(255, 0, 0))
- ]
-
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
-
- screen.fill((0, 0, 0))
- physics()
- physics()
- boost()
- modulate()
- draw()
-
- pygame.display.flip()
- clock.tick(60)
-
- pygame.quit()
|