Skip to content

Commit 155c52f

Browse files
committed
Updated.
1 parent a3b261c commit 155c52f

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

qt__pyqt__pyside__pyqode/full_black_screen_close_manual_with_animations.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
__author__ = "ipetrash"
55

66

7-
from random import randint
7+
import enum
8+
from random import randint, choice
89

910
from PyQt5.QtWidgets import QApplication, QWidget
1011
from PyQt5.QtGui import QPainter, QPaintEvent
1112
from PyQt5.QtCore import QTimer
1213

1314
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
1516

1617

1718
class Animation:
@@ -31,12 +32,34 @@ def draw(self, painter: QPainter):
3132
pass
3233

3334

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+
3447
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+
):
3656
super().__init__(owner)
3757

3858
self.balls: list[Ball] = []
3959
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
4063

4164
def prepare(self):
4265
for _ in range(self.number_balls):
@@ -47,13 +70,14 @@ def append_random_ball(self):
4770
y = self.owner.height() // 2 + randint(-self.owner.height() // 3, self.owner.height() // 3)
4871
v_x, v_y = get_random_vector()
4972
r, g, b = get_random_color()
73+
a = randint(self.min_ball_alpha_color, self.max_ball_alpha_color)
5074

5175
ball = Ball(
5276
x, y,
5377
r=randint(50, 70),
5478
v_x=v_x,
5579
v_y=v_y,
56-
color=(r, g, b, 15),
80+
color=(r, g, b, a),
5781
)
5882
self.balls.append(ball)
5983

@@ -69,6 +93,17 @@ def tick(self):
6993
if ball.top <= 0 or ball.bottom >= self.owner.height():
7094
ball.v_y = -ball.v_y
7195

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+
72107
def draw(self, painter: QPainter):
73108
for ball in self.balls:
74109
ball.draw(painter)

0 commit comments

Comments
 (0)