1
+ import pmma
2
+ import random
3
+ import time
4
+ import pygame
5
+ import math
6
+
7
+ pmma .init ()
8
+ pygame .init ()
9
+
10
+ display = pygame .display .set_mode ((1920 , 1080 ), pygame .FULLSCREEN )
11
+ clock = pygame .time .Clock ()
12
+
13
+ class Ball :
14
+ def __init__ (self , x ):
15
+ self .x = x
16
+ self .y = display .get_height () + 300
17
+ self .radius = random .randint (1 , 100 )
18
+ self .time = time .perf_counter ()
19
+ self .color_converter = pmma .ColorConverter ()
20
+ self .color = self .color_converter .generate_random_color ()
21
+ self .destroy = False
22
+ self .size_change_time = 3
23
+ self .distance_moved = 0
24
+ self .do_once = False
25
+
26
+ def collision (self , other_ball ):
27
+ # Distance between centers
28
+ distance = math .sqrt ((self .x - other_ball .x )** 2 + (self .y - other_ball .y )** 2 )
29
+ if distance <= self .radius + other_ball .radius :
30
+ # Adjust positions slightly to avoid overlap
31
+ overlap = self .radius + other_ball .radius - distance
32
+ angle = math .atan2 (other_ball .y - self .y , other_ball .x - self .x )
33
+ self .x -= math .cos (angle ) * overlap / 2
34
+ self .y -= math .sin (angle ) * overlap / 2
35
+ other_ball .x += math .cos (angle ) * overlap / 2
36
+ other_ball .y += math .sin (angle ) * overlap / 2
37
+
38
+ def compute (self ):
39
+ self .y -= 3
40
+
41
+ if self .y + self .radius <= 0 :
42
+ self .destroy = True
43
+
44
+ def to_destroy (self ):
45
+ return self .destroy
46
+
47
+ def render (self ):
48
+ if not self .destroy :
49
+ pygame .draw .circle (display , self .color , (self .x , int (self .y )), self .radius )
50
+
51
+ balls = []
52
+ for i in range (150 ):
53
+ balls .append (Ball (random .randint (0 , display .get_width ())))
54
+
55
+ while pmma .get_application_running ():
56
+ #display.fill(pygame.transform.average_color(display))
57
+
58
+ for event in pygame .event .get ():
59
+ if event .type == pygame .QUIT :
60
+ pmma .set_application_running (False )
61
+
62
+ for ball in balls :
63
+ ball .render ()
64
+ ball .compute ()
65
+ for other_ball in balls :
66
+ if ball != other_ball :
67
+ ball .collision (other_ball )
68
+ if ball .to_destroy ():
69
+ balls .remove (ball )
70
+
71
+ for i in range (0 , 150 - len (balls )):
72
+ balls .append (Ball (random .randint (0 , display .get_width ())))
73
+
74
+ pmma .compute ()
75
+
76
+ pygame .display .update ()
77
+ clock .tick (60 )
0 commit comments