-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotsEnv.py
84 lines (59 loc) · 1.41 KB
/
DotsEnv.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
72
73
74
75
76
77
78
79
80
81
82
83
84
import pygame
import random
import time
pygame.init()
N=400
Steps=20
r = 10
SW=1000
SH=600
OBS = 50
screen = pygame.display.set_mode((SW,SH))
pygame.display.set_caption("DotsEnv")
clock = pygame.time.Clock()
Dots = []
for _ in range(N):
Dots.append([random.uniform(0,SW),random.uniform(0,SH),(random.randint(5,220),random.randint(5,220),random.randint(5,220))])
def isCollision(A,B,r):
d = (A[0]-B[0])**2 + (A[1]-B[1])**2
d = d**0.5
if(d <= 2*r):
return 1
def print_text(text,x,y):
img = pygame.font.SysFont("Arial",30).render(text,1,(255,255,255))
screen.blit(img,(x,y))
run = N
Q = 1
st = time.perf_counter()
while(run*Q):
clock.tick(60)
screen.fill((0,0,0))
print_text(f"Dots: {run}",SW-170,SH-100)
print_text(f"Time: {time.perf_counter()-st}",SW-166,SH-78)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Q = 0
for dot in Dots:
pygame.draw.circle(screen,dot[2],(dot[0],dot[1]),r)
dot[0]+= random.uniform(-Steps,Steps)
dot[1]+= random.uniform(-Steps,Steps)
if(dot[0] < 0+OBS):
dot[0] = 0+OBS
elif (dot[0] > SW-OBS):
dot[0] = SW-OBS
elif(dot[1]< 0+OBS):
dot[1] = 0+OBS
elif (dot[1] > SH-OBS):
dot[1] = SH-OBS
for b in Dots:
if( dot!=b and isCollision(dot,b,r)):
try:
Dots.remove(dot)
Dots.remove(b)
except:
pass
run = len(Dots)
pygame.display.flip()
et = time.perf_counter()
print(f"N={N} T={et-st}")
pygame.quit()