4
4
__author__ = "ipetrash"
5
5
6
6
7
- from random import randint
7
+ import enum
8
+ from random import randint , choice
8
9
9
10
from PyQt5 .QtWidgets import QApplication , QWidget
10
11
from PyQt5 .QtGui import QPainter , QPaintEvent
11
12
from PyQt5 .QtCore import QTimer
12
13
13
14
from full_black_screen_close_manual import MainWindow as BaseMainWindow
14
- from pyq5__simple_balls__with_part_transparent_body import Ball , get_random_vector , get_random_color
15
+ from pyq5__simple_balls__with_part_transparent_body import Ball as BaseBall , get_random_vector , get_random_color
15
16
16
17
17
18
class Animation :
@@ -31,12 +32,34 @@ def draw(self, painter: QPainter):
31
32
pass
32
33
33
34
35
+ class DirectionEnum (enum .IntEnum ):
36
+ UP = 1
37
+ DOWN = - 1
38
+
39
+
40
+ class Ball (BaseBall ):
41
+ def __init__ (self , x , y , r , v_x , v_y , color ):
42
+ super ().__init__ (x , y , r , v_x , v_y , color )
43
+
44
+ self .animation_alpha_direction = choice (list (DirectionEnum ))
45
+
46
+
34
47
class AnimationBalls (Animation ):
35
- def __init__ (self , owner : QWidget = None , number_balls : int = 50 ):
48
+ def __init__ (
49
+ self ,
50
+ owner : QWidget = None ,
51
+ number_balls : int = 50 ,
52
+ min_ball_alpha_color : int = 35 ,
53
+ max_ball_alpha_color : int = 255 ,
54
+ animation_ball_alpha_color : bool = True ,
55
+ ):
36
56
super ().__init__ (owner )
37
57
38
58
self .balls : list [Ball ] = []
39
59
self .number_balls = number_balls
60
+ self .min_ball_alpha_color = min_ball_alpha_color
61
+ self .max_ball_alpha_color = max_ball_alpha_color
62
+ self .animation_ball_alpha_color = animation_ball_alpha_color
40
63
41
64
def prepare (self ):
42
65
for _ in range (self .number_balls ):
@@ -47,13 +70,14 @@ def append_random_ball(self):
47
70
y = self .owner .height () // 2 + randint (- self .owner .height () // 3 , self .owner .height () // 3 )
48
71
v_x , v_y = get_random_vector ()
49
72
r , g , b = get_random_color ()
73
+ a = randint (self .min_ball_alpha_color , self .max_ball_alpha_color )
50
74
51
75
ball = Ball (
52
76
x , y ,
53
77
r = randint (50 , 70 ),
54
78
v_x = v_x ,
55
79
v_y = v_y ,
56
- color = (r , g , b , 15 ),
80
+ color = (r , g , b , a ),
57
81
)
58
82
self .balls .append (ball )
59
83
@@ -69,6 +93,17 @@ def tick(self):
69
93
if ball .top <= 0 or ball .bottom >= self .owner .height ():
70
94
ball .v_y = - ball .v_y
71
95
96
+ if self .animation_ball_alpha_color :
97
+ alpha = ball .color [3 ] + ball .animation_alpha_direction
98
+
99
+ if alpha >= self .max_ball_alpha_color :
100
+ ball .animation_alpha_direction = DirectionEnum .DOWN
101
+
102
+ if alpha <= self .min_ball_alpha_color :
103
+ ball .animation_alpha_direction = DirectionEnum .UP
104
+
105
+ ball .color = ball .color [:3 ] + (alpha ,)
106
+
72
107
def draw (self , painter : QPainter ):
73
108
for ball in self .balls :
74
109
ball .draw (painter )
0 commit comments