-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 10.py
71 lines (59 loc) · 1.82 KB
/
Day 10.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
#!/usr/bin/env python3
## A simple Python port - You need: pip install pygame. Note the code here is not efficient but it's made to be educational and easy
import pygame
import random
atoms=[]
window_size = 300
pygame.init()
window = pygame.display.set_mode((window_size, window_size))
def draw(surface, x, y, color, size):
for i in range(0, size):
pygame.draw.line(surface, color, (x, y-1), (x, y+2), abs(size))
def atom(x, y, c):
return {"x": x, "y": y, "vx": 0, "vy": 0, "color": c}
def randomxy():
return round(random.random()*window_size + 1)
def create(number, color):
group = []
for i in range(number):
group.append(atom(randomxy(), randomxy(), color))
atoms.append((group[i]))
return group
def rule(atoms1, atoms2, g):
for i in range(len(atoms1)):
fx = 0
fy = 0
for j in range(len(atoms2)):
a = atoms1[i]
b = atoms2[j]
dx = a["x"] - b["x"]
dy = a["y"] - b["y"]
d = (dx*dx + dy*dy)**0.5
if( d > 0 and d < 80):
F = g/d
fx += F*dx
fy += F*dy
a["vx"] = (a["vx"] + fx)*0.5
a["vy"] = (a["vy"] + fy)*0.5
a["x"] += a["vx"]
a["y"] += a["vy"]
if(a["x"] <= 0 or a["x"] >= window_size):
a["vx"] *=-1
if(a["y"] <= 0 or a["y"] >= window_size):
a["vy"] *=-1
yellow = create(100, "yellow")
red = create(100, "red")
run = True
while run:
window.fill(0)
rule(red, red, 0.1)
rule(red, yellow, -0.15)
rule(yellow, yellow, -0.1)
for i in range(len(atoms)):
draw(window, atoms[i]["x"], atoms[i]["y"], atoms[i]["color"], 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
exit()