-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
71 lines (59 loc) · 1.74 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
import random
import math
pygame.init()
screen = pygame.display.set_mode((800, 800))
clock = pygame.time.Clock()
pygame.event.set_allowed([])
white = (255, 255, 255)
black = (0, 0, 0)
n = 20
vel = 5
radius = 20
randcolor = False
balls = []
for i in range(n):
z = (
random.randrange(radius, 800 - radius)
+ random.randrange(radius, 800 - radius) * 1j
)
t = random.random() * math.tau
v = vel * (math.cos(t) + math.sin(t) * 1j)
if randcolor:
color = (
random.randrange(128, 256),
random.randrange(128, 256),
random.randrange(128, 256),
)
else:
color = white
balls.append([z, v, color])
balls = tuple(balls)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
for ball in balls:
ball[0] += ball[1]
if ball[0].real < radius:
ball[1] = abs(ball[1].real) + ball[1].imag * 1j
elif ball[0].real > 800 - radius:
ball[1] = -abs(ball[1].real) + ball[1].imag * 1j
if ball[0].imag < radius:
ball[1] = ball[1].real + abs(ball[1].imag) * 1j
elif ball[0].imag > 800 - radius:
ball[1] = ball[1].real - abs(ball[1].imag) * 1j
for ball1 in balls:
for ball2 in balls:
if ball1 != ball2:
z1, v1, _ = ball1
z2, _, _ = ball2
if abs(z1 - z2) < 2 * radius:
v1 = -((z1 - z2) ** 2) / v1 * vel**2 / abs(z1 - z2) ** 2
ball1[1] = v1
screen.fill(black)
for ball in balls:
pygame.draw.circle(screen, ball[2], (ball[0].real, ball[0].imag), radius)
clock.tick(60)
pygame.display.flip()