-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncy balls.py
115 lines (88 loc) · 4.08 KB
/
bouncy balls.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import random
import pmma
import time
import pygame
import traceback
display = pmma.Display()
try:
display.create(1280, 720)
except Exception:
print(traceback.format_exc())
#display.create(1280, 720)
events = pmma.Events()
math = pmma.Math()
class Ball:
def __init__(self):
self.x = random.randint(0, display.get_width())
self.y = random.randint(0, display.get_height())
self.dir_x = (random.random()*2)-1
self.dir_y = (random.random()*2)-1
self.seed = pmma.Perlin()
self.mass = self.seed.generate_2D_perlin_noise(0, 0, new_range=[0, 20])
self.mag_x = random.random()*5
self.mag_y = random.random()*5
self.last_collision_time = time.perf_counter()
self.color = [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)]
def compute(self, balls, now_time):
self.mass = self.seed.generate_2D_perlin_noise(now_time/100, 0, new_range=[0, 20])
self.x += self.dir_x * self.mag_x
self.y += self.dir_y * self.mag_y
if self.x > display.get_width()-self.mass:
self.x = display.get_width()-self.mass
self.dir_x *= -1
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (self.x, self.y), self.mass*2, width=1)
elif self.x < self.mass:
self.x = self.mass
self.dir_x *= -1
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (self.x, self.y), self.mass*2, width=1)
if self.y > display.get_height()-self.mass:
self.y = display.get_height()-self.mass
self.dir_y *= -1
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (self.x, self.y), self.mass*2, width=1)
elif self.y < self.mass:
self.y = self.mass
self.dir_y *= -1
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (self.x, self.y), self.mass*2, width=1)
if time.perf_counter()-self.last_collision_time > 1/15:
for ball in balls:
if ball is self:
continue
if abs(self.x-ball.x) < self.mass+ball.mass and abs(self.y-ball.y) < self.mass+ball.mass:
#self.x += (self.x-ball.x)/2
#self.y += (self.y-ball.y)/2
self.dir_x *= -1
self.dir_y *= -1
ball.dir_x *= -1
ball.dir_y *= -1
self.last_collision_time = time.perf_counter()
ball.last_collision_time = time.perf_counter()
self.color = [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)]
ball.color = [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)]
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (self.x, self.y), self.mass*2, width=1)
pygame.draw.circle(Surface, [random.randint(100, 255), random.randint(100, 255), random.randint(100, 255)], (ball.x, ball.y), ball.mass*2, width=1)
def render(self):
pygame.draw.circle(Surface, self.color, (self.x, self.y), self.mass)
balls = []
n_balls = 50
for i in range(n_balls):
balls.append(Ball())
start = time.perf_counter()
now_time = 0
while pmma.get_application_running():
window_x, window_y = display.get_size()
window_size = (window_x, window_y)
Surface = pygame.Surface(window_size).convert()
Surface.fill((0, 0, 0))
Surface.set_colorkey((0, 0, 0))
Surface2 = pygame.Surface(window_size)
Surface2.fill((0, 0, 0))
events.handle()
for ball in balls:
ball.compute(balls, now_time)
ball.render()
Surface2.set_alpha(5) # 10
display.blit(Surface2, (0, 0))
display.blit(Surface, (0, 0))
pmma.compute()
display.refresh()
now_time = time.perf_counter() - start