|
@@ -0,0 +1,59 @@ |
|
|
|
|
|
# Import the pygame library and initialise the game engine |
|
|
|
|
|
import pygame |
|
|
|
|
|
import numpy as np |
|
|
|
|
|
pygame.init() |
|
|
|
|
|
|
|
|
|
|
|
# Initialize Pygame |
|
|
|
|
|
size = (800, 800) |
|
|
|
|
|
CENTER = np.array(size) / 2 |
|
|
|
|
|
WHITE = (255, 255, 255) |
|
|
|
|
|
screen = pygame.display.set_mode(size) |
|
|
|
|
|
pygame.display.set_caption("Patrick") |
|
|
|
|
|
running = True |
|
|
|
|
|
clock = pygame.time.Clock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Ball(object): |
|
|
|
|
|
def __init__(self, x, y, radius=10, mass=1): |
|
|
|
|
|
self.pos = np.array([x, y]) |
|
|
|
|
|
self.radius = radius |
|
|
|
|
|
self.mass = mass |
|
|
|
|
|
|
|
|
|
|
|
def render(self, screen): |
|
|
|
|
|
x, y = self.pos + CENTER |
|
|
|
|
|
pygame.draw.circle(screen, WHITE, [int(x), int(y)], self.radius, 2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Spring(object): |
|
|
|
|
|
def __init__(self, a, b, k=0.1): |
|
|
|
|
|
self.a = a |
|
|
|
|
|
self.b = b |
|
|
|
|
|
self.k = k |
|
|
|
|
|
|
|
|
|
|
|
def render(self, screen): |
|
|
|
|
|
x1, y1 = self.a.pos + CENTER |
|
|
|
|
|
x2, y2 = self.b.pos + CENTER |
|
|
|
|
|
pygame.draw.line(screen, WHITE, [x1, y1], [x2, y2]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
balls = [Ball(0, 0), Ball(100, 1)] |
|
|
|
|
|
springs = [Spring(balls[0], balls[1])] |
|
|
|
|
|
|
|
|
|
|
|
while running: |
|
|
|
|
|
for event in pygame.event.get(): |
|
|
|
|
|
if event.type == pygame.QUIT: |
|
|
|
|
|
running = False |
|
|
|
|
|
|
|
|
|
|
|
screen.fill((0, 0, 0)) |
|
|
|
|
|
|
|
|
|
|
|
for ball in balls: |
|
|
|
|
|
ball.render(screen) |
|
|
|
|
|
|
|
|
|
|
|
for spring in springs: |
|
|
|
|
|
spring.render(screen) |
|
|
|
|
|
|
|
|
|
|
|
pygame.display.flip() |
|
|
|
|
|
clock.tick(60) |
|
|
|
|
|
|
|
|
|
|
|
pygame.quit() |